/__w/smoldot/smoldot/repo/full-node/src/util.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Smoldot |
2 | | // Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. |
3 | | // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
4 | | |
5 | | // This program is free software: you can redistribute it and/or modify |
6 | | // it under the terms of the GNU General Public License as published by |
7 | | // the Free Software Foundation, either version 3 of the License, or |
8 | | // (at your option) any later version. |
9 | | |
10 | | // This program is distributed in the hope that it will be useful, |
11 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | // GNU General Public License for more details. |
14 | | |
15 | | // You should have received a copy of the GNU General Public License |
16 | | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | |
18 | | use std::fmt::{self, Write as _}; |
19 | | |
20 | | /// Returns an opaque object implementing the `fmt::Display` trait. Truncates the given `char` |
21 | | /// yielding iterator to the given number of elements, and if the limit is reached adds a `…` at |
22 | | /// the end. |
23 | 0 | pub fn truncated_str<'a>( |
24 | 0 | input: impl Iterator<Item = char> + Clone + 'a, |
25 | 0 | limit: usize, |
26 | 0 | ) -> impl fmt::Display + 'a { |
27 | 0 | struct Iter<I>(I, usize); |
28 | 0 |
|
29 | 0 | impl<I: Iterator<Item = char> + Clone> fmt::Display for Iter<I> { |
30 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
31 | 0 | let mut counter = 0; |
32 | 0 | for c in self.0.clone() { |
33 | 0 | f.write_char(c)?; |
34 | 0 |
|
35 | 0 | counter += 1; |
36 | 0 | if counter >= self.1 { |
37 | 0 | f.write_char('…')?; |
38 | 0 | break; |
39 | 0 | } |
40 | 0 | } |
41 | 0 |
|
42 | 0 | Ok(()) |
43 | 0 | } Unexecuted instantiation: _RNvXNvNtCsiUjFBJteJ7x_17smoldot_full_node4util13truncated_strINtB2_4IterINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB1h_3str4iter5CharsNCNCNCNvNtB6_16json_rpc_service20spawn_client_io_task000EENtNtB1h_3fmt7Display3fmtB6_ Unexecuted instantiation: _RNvXNvNtCsiUjFBJteJ7x_17smoldot_full_node4util13truncated_strINtB2_4IterINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB1h_3str4iter5CharsNCNCNCNvNtB6_16json_rpc_service20spawn_client_io_task0s_00EENtNtB1h_3fmt7Display3fmtB6_ Unexecuted instantiation: _RNvXININvNtCshBwayKnNXDT_17smoldot_full_node4util13truncated_str0pEINtB5_4IterpENtNtCsaYZPK01V26L_4core3fmt7Display3fmtB9_ |
44 | 0 | } |
45 | 0 |
|
46 | 0 | Iter(input, limit) |
47 | 0 | } Unexecuted instantiation: _RINvNtCsiUjFBJteJ7x_17smoldot_full_node4util13truncated_strINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB14_3str4iter5CharsNCNCNCNvNtB4_16json_rpc_service20spawn_client_io_task000EEB4_ Unexecuted instantiation: _RINvNtCsiUjFBJteJ7x_17smoldot_full_node4util13truncated_strINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB14_3str4iter5CharsNCNCNCNvNtB4_16json_rpc_service20spawn_client_io_task0s_00EEB4_ Unexecuted instantiation: _RINvNtCshBwayKnNXDT_17smoldot_full_node4util13truncated_strpEB4_ |