Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! strfmt crate
2
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#;
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#;
9
10 mod fmtstrrust-analyzer cargo strfmt 0.2.4 fmtstr/;
11 mod formatterrust-analyzer cargo strfmt 0.2.4 formatter/;
12 #[cfg(test)]
13 mod testsrust-analyzer cargo strfmt 0.2.4 tests/;
14 mod typesrust-analyzer cargo strfmt 0.2.4 types/;
15
16 #[macro_use]
17 mod fmtnumrust-analyzer cargo strfmt 0.2.4 fmtnum/;
18 mod macrosrust-analyzer cargo strfmt 0.2.4 macros/;
19
20 pub use fmtstrrust-analyzer cargo strfmt 0.2.4 fmtstr/::strfmt_maprust-analyzer cargo strfmt 0.2.4 fmtstr/strfmt_map().;
21 pub use formatterrust-analyzer cargo strfmt 0.2.4 formatter/::Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#;
22 pub use typesrust-analyzer cargo strfmt 0.2.4 types/::{Alignmentrust-analyzer cargo strfmt 0.2.4 types/Alignment#, FmtErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#, Resultrust-analyzer cargo strfmt 0.2.4 types/Result#, Signrust-analyzer cargo strfmt 0.2.4 types/Sign#};
23
24 // u128 & i128 unstable (see https://github.com/rust-lang/rust/issues/35118)
25 fmtintrust-analyzer cargo strfmt 0.2.4 fmtnum/fmtint!!(u8rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]u8(). i8rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]i8(). u16rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]u16(). i16rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]i16(). u32rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]u32(). i32rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]i32(). u64rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]u64(). i64rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]i64(). usizerust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]usize(). isizerust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]isize().);
26 fmtfloatrust-analyzer cargo strfmt 0.2.4 fmtnum/fmtfloat!!(f32rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]f32(). f64rust-analyzer cargo strfmt 0.2.4 impl#[`Formatter<'a, 'b>`]f64().);
27
28 /// Rust-style format a string given a `HashMap` of the variables.
29 /// # Arguments
30 ///
31 /// * `fmtstr` - A string defining the format
32 /// * `vars` - A `HashMap` holding the variables to use
33 ///
34 /// # Exceptions
35 ///
36 /// * [FmtError::Invalid] - The format string is structured incorrectly
37 /// * [FmtError::KeyError] - `vars` contains an invalid key
38 /// * [FmtError::TypeError] - the given format code for a section contains an unexpected option
39 ///
40 /// # Examples
41 ///
42 /// ```
43 /// use std::collections::HashMap;
44 /// use std::f64::consts::PI;
45 /// use strfmt::strfmt;
46 ///
47 /// let mut my_vars: HashMap<String, f64> = HashMap::new();
48 /// my_vars.insert("Alpha".to_string(),42.0);
49 /// my_vars.insert("Beta".to_string(),PI);
50 ///
51 /// println!("{}", strfmt("{Alpha} {Beta:<5.2}",&my_vars).unwrap());
52 /// ```
53 pub fn strfmtrust-analyzer cargo strfmt 0.2.4 strfmt().<'a, K, T: DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr#>(fmtstr: &str, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, T>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
54 where
55 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
56 {
57 let formatter = |mut fmt: Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#| {
58 let k: K = match fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() {
59 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(k) => k,
60 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
61 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(new_key_errorrust-analyzer cargo strfmt 0.2.4 new_key_error().(fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key.));
62 }
63 };
64 let v = match vars.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&k) {
65 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v) => v,
66 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
67 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(new_key_errorrust-analyzer cargo strfmt 0.2.4 new_key_error().(fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key.));
68 }
69 };
70 v.display_strrust-analyzer cargo strfmt 0.2.4 DisplayStr#display_str().(&mut fmt)
71 };
72 strfmt_maprust-analyzer cargo strfmt 0.2.4 fmtstr/strfmt_map().(fmtstr, &formatter)
73 }
74
75 /// Rust-style format a string given a `HashMap` of the variables.
76 /// see [strfmt] for details
77 #[deprecated(
78 since = "0.2.0",
79 note = "This function contains a bug when formatting numbers. Use strfmt instead"
80 )]
81 pub fn strfmt_displayrust-analyzer cargo strfmt 0.2.4 strfmt_display().<'a, K, T: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#>(fmtstr: &str, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, T>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
82 where
83 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
84 {
85 let formatter = |mut fmt: Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#| {
86 let k: K = match fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() {
87 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(k) => k,
88 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
89 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(new_key_errorrust-analyzer cargo strfmt 0.2.4 new_key_error().(fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key.));
90 }
91 };
92 let v = match vars.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&k) {
93 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v) => v,
94 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
95 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(new_key_errorrust-analyzer cargo strfmt 0.2.4 new_key_error().(fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key.));
96 }
97 };
98 fmt.strrust-analyzer cargo strfmt 0.2.4 fmtstr/impl#[`Formatter<'a, 'b>`]str().(v.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().())
99 };
100 strfmt_maprust-analyzer cargo strfmt 0.2.4 fmtstr/strfmt_map().(fmtstr, &formatter)
101 }
102
103 macro_rules! display_str_implrust-analyzer cargo strfmt 0.2.4 display_str_impl! {
104 ($($t:ident)*) => ($(
105 impl DisplayStr for $t {
106 fn display_str(&self,f:&mut Formatter) -> Result<()> {
107 f.$t(*self)
108 }
109 }
110 )*)
111 }
112
113 display_str_implrust-analyzer cargo strfmt 0.2.4 display_str_impl!!(u8 i8 u16 i16 u32 i32 u64 i64 usize isize);
114 display_str_implrust-analyzer cargo strfmt 0.2.4 display_str_impl!!(f32 f64);
115
116 impl DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr# for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
117 fn display_strrust-analyzer cargo strfmt 0.2.4 impl#[String][DisplayStr]display_str().(&self, f: &mut Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<()> {
118 f.strrust-analyzer cargo strfmt 0.2.4 fmtstr/impl#[`Formatter<'a, 'b>`]str().(self.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().())
119 }
120 }
121
122 impl DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr# for &str {
123 fn display_strrust-analyzer cargo strfmt 0.2.4 impl#[`&str`][DisplayStr]display_str().(&self, f: &mut Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<()> {
124 f.strrust-analyzer cargo strfmt 0.2.4 fmtstr/impl#[`Formatter<'a, 'b>`]str().(self)
125 }
126 }
127
128 impl DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr# for &dyn DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr# {
129 fn display_strrust-analyzer cargo strfmt 0.2.4 impl#[`&(dyn DisplayStr + 'static)`][DisplayStr]display_str().(&self, f: &mut Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<()> {
130 (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self).display_strrust-analyzer cargo strfmt 0.2.4 DisplayStr#display_str().(f)
131 }
132 }
133
134 impl DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr# for Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr#> {
135 fn display_strrust-analyzer cargo strfmt 0.2.4 impl#[`Box<dyn DisplayStr + 'static>`][DisplayStr]display_str().(&self, f: &mut Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<()> {
136 self.as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][`AsRef<T>`]as_ref().().display_strrust-analyzer cargo strfmt 0.2.4 DisplayStr#display_str().(f)
137 }
138 }
139
140 /// This trait is effectively an re-implementation for [std::fmt::Display]
141 /// It is used to disguise between the value types that should be formatted
142 pub trait DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr# {
143 fn display_strrust-analyzer cargo strfmt 0.2.4 DisplayStr#display_str().(&self, f: &mut Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<()>;
144 }
145
146 /// This trait is a shortcut for [strfmt]
147 /// for an example see [Format::format]
148 pub trait Formatrust-analyzer cargo strfmt 0.2.4 Format# {
149 /// format a string using strfmt
150 /// # Arguments
151 ///
152 /// * `vars` - A `HashMap` holding the variables to use
153 ///
154 /// # Errors
155 /// Errors are passed directly from strfmt, for details see [strfmt]
156 ///
157 /// # Examples
158 ///
159 /// ```
160 /// use std::collections::HashMap;
161 /// use std::f64::consts::PI;
162 /// use strfmt::Format;
163 ///
164 /// let mut my_vars: HashMap<String, f64> = HashMap::new();
165 /// my_vars.insert("Alpha".to_string(),42.0);
166 /// my_vars.insert("Beta".to_string(),PI);
167 ///
168 /// println!("{}", "|{Alpha}|{Beta:<5.2}|".format(&my_vars).unwrap());
169 /// ```
170 fn formatrust-analyzer cargo strfmt 0.2.4 Format#format().<K, D: DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr#>(&self, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, D>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
171 where
172 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
173
174 /// format a string using strfmt_display
175 /// see [Format::format] for usage
176 #[deprecated(
177 since = "0.2.0",
178 note = "This function contains a bug when formatting numbers. Use format instead"
179 )]
180 fn format_displayrust-analyzer cargo strfmt 0.2.4 Format#format_display().<K, D: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#>(&self, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, D>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
181 where
182 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
183 }
184
185 impl Formatrust-analyzer cargo strfmt 0.2.4 Format# for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
186 fn formatrust-analyzer cargo strfmt 0.2.4 impl#[String][Format]format().<'a, K, D: DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr#>(&self, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, D>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
187 where
188 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
189 {
190 strfmtrust-analyzer cargo strfmt 0.2.4 strfmt().(self.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), vars)
191 }
192 fn format_displayrust-analyzer cargo strfmt 0.2.4 impl#[String][Format]format_display().<'a, K, D: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#>(&self, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, D>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
193 where
194 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
195 {
196 #[allow(deprecated)]
197 strfmt_displayrust-analyzer cargo strfmt 0.2.4 strfmt_display().(self.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), vars)
198 }
199 }
200
201 impl Formatrust-analyzer cargo strfmt 0.2.4 Format# for str {
202 fn formatrust-analyzer cargo strfmt 0.2.4 impl#[str][Format]format().<K, D: DisplayStrrust-analyzer cargo strfmt 0.2.4 DisplayStr#>(&self, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, D>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
203 where
204 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
205 {
206 strfmtrust-analyzer cargo strfmt 0.2.4 strfmt().(self, vars)
207 }
208 fn format_displayrust-analyzer cargo strfmt 0.2.4 impl#[str][Format]format_display().<'a, K, D: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#>(&self, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, D>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>
209 where
210 K: Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
211 {
212 #[allow(deprecated)]
213 strfmt_displayrust-analyzer cargo strfmt 0.2.4 strfmt_display().(self, vars)
214 }
215 }
216
217 fn new_key_errorrust-analyzer cargo strfmt 0.2.4 new_key_error().(key: &str) -> FmtErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError# {
218 let mut msg = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
219 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(msg, "Invalid key: {}", key).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
220 FmtErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#::KeyErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#KeyError#(msg)
221 }