| 1 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/; |
| 2 | |
| 3 | use craterust-analyzer cargo runtime-format 0.1.3 crate/::{parserust-analyzer cargo runtime-format 0.1.3 parse/::ParseSegmentrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#, FormatArgsrust-analyzer cargo runtime-format 0.1.3 FormatArgs#, FormatErrorrust-analyzer cargo runtime-format 0.1.3 FormatError#, FormatKeyrust-analyzer cargo runtime-format 0.1.3 FormatKey#, ToFormatParserrust-analyzer cargo runtime-format 0.1.3 ToFormatParser#}; |
| 4 | |
| 5 | /// Preparsed formatting terms. |
| 6 | /// |
| 7 | /// This is faster if you will be using the same format string again and again with |
| 8 | /// different inputs. |
| 9 | /// |
| 10 | /// ``` |
| 11 | /// use runtime_format::{FormatArgs, FormatKey, FormatKeyError, ParsedFmt}; |
| 12 | /// use core::fmt; |
| 13 | /// # struct DateTime; |
| 14 | /// # impl DateTime { fn now() -> Self { Self } } |
| 15 | /// # impl DateTime { fn day(&self) -> i32 { 25 } fn short_month_name(&self) -> &'static str { "Jan" } fn year(&self) -> i32 { 2023 } } |
| 16 | /// # impl DateTime { fn hours(&self) -> i32 { 16 } fn minutes(&self) -> i32 { 27 } fn seconds(&self) -> i32 { 53 } } |
| 17 | /// impl FormatKey for DateTime { |
| 18 | /// fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> { |
| 19 | /// // ... |
| 20 | /// # use core::fmt::Write; |
| 21 | /// # match key { |
| 22 | /// # "year" => write!(f, "{}", self.year()).map_err(FormatKeyError::Fmt), |
| 23 | /// # "month" => write!(f, "{}", self.short_month_name()).map_err(FormatKeyError::Fmt), |
| 24 | /// # "day" => write!(f, "{}", self.day()).map_err(FormatKeyError::Fmt), |
| 25 | /// # "hours" => write!(f, "{}", self.hours()).map_err(FormatKeyError::Fmt), |
| 26 | /// # "minutes" => write!(f, "{}", self.minutes()).map_err(FormatKeyError::Fmt), |
| 27 | /// # "seconds" => write!(f, "{}", self.seconds()).map_err(FormatKeyError::Fmt), |
| 28 | /// # _ => Err(FormatKeyError::UnknownKey), |
| 29 | /// # } |
| 30 | /// } |
| 31 | /// } |
| 32 | /// |
| 33 | /// let now = DateTime::now(); |
| 34 | /// let fmt = ParsedFmt::new("{month} {day} {year} {hours}:{minutes}:{seconds}").unwrap(); |
| 35 | /// let args = FormatArgs::new(&fmt, &now); |
| 36 | /// let expected = "Jan 25 2023 16:27:53"; |
| 37 | /// assert_eq!(args.to_string(), expected); |
| 38 | pub struct ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#<'a> { |
| 39 | segmentsrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#segments.: tinyvecrust-analyzer cargo tinyvec 1.12.0 crate/::TinyVecrust-analyzer cargo tinyvec 1.12.0 tinyvec/TinyVec#<[ParseSegmentrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#<'a>; 8]>, |
| 40 | } |
| 41 | |
| 42 | impl<'a> ToFormatParserrust-analyzer cargo runtime-format 0.1.3 ToFormatParser#<'a> for ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#<'a> { |
| 43 | type Parserrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`ToFormatParser<'a>`]Parser# = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::Copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/copied/Copied#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/::Iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/Iter#<'a, ParseSegmentrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#<'a>>>; |
| 44 | |
| 45 | fn to_parserrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`ToFormatParser<'a>`]to_parser().(&'a self) -> Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`ToFormatParser<'a>`]::Parserrust-analyzer cargo runtime-format 0.1.3 ToFormatParser#Parser# { |
| 46 | self.segmentsrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#segments..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().() |
| 47 | } |
| 48 | |
| 49 | fn unparsedrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`ToFormatParser<'a>`]unparsed().(_: Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`ToFormatParser<'a>`]::Parserrust-analyzer cargo runtime-format 0.1.3 ToFormatParser#Parser#) -> &'a str { |
| 50 | "" |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#<'_> { |
| 55 | fn fmtrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'_>`][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 56 | f.debug_structrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_struct().("CompiledFormatter") |
| 57 | .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]field().("segments", &self.segmentsrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#segments.) |
| 58 | .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugStruct<'a, 'b>`]finish().() |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl<'a> ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#<'a> { |
| 63 | /// Parse the given format string. |
| 64 | /// |
| 65 | /// # Errors |
| 66 | /// If the string could not be parsed, or there is a key that is unacceptable. |
| 67 | pub fn newrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]new().(s: &'a str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`], FormatErrorrust-analyzer cargo runtime-format 0.1.3 FormatError#<'a>> { |
| 68 | let mut segments = s.to_parserrust-analyzer cargo runtime-format 0.1.3 parse/impl#[str][`ToFormatParser<'a>`]to_parser().(); |
| 69 | let this = Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`] { |
| 70 | segmentsrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#segments.: segments.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 71 | }; |
| 72 | |
| 73 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().segments.srust-analyzer cargo runtime-format 0.1.3 parse/FromStr#s..is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 74 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(FormatErrorrust-analyzer cargo runtime-format 0.1.3 FormatError#::Parserust-analyzer cargo runtime-format 0.1.3 FormatError#Parse#(segments.srust-analyzer cargo runtime-format 0.1.3 parse/FromStr#s.)) |
| 75 | } else { |
| 76 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(this) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// Return the keys that will be used when formatting. |
| 81 | /// |
| 82 | /// ``` |
| 83 | /// # use runtime_format::ParsedFmt; |
| 84 | /// let fmt = "Hello, {recipient}. Hope you are having a nice {time_descriptor}."; |
| 85 | /// let parsed = ParsedFmt::new(fmt).unwrap(); |
| 86 | /// let keys: Vec<_> = parsed.keys().collect(); |
| 87 | /// assert_eq!(keys, ["recipient", "time_descriptor"]); |
| 88 | /// ``` |
| 89 | pub fn keysrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]keys().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &'_ str> { |
| 90 | self.segmentsrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#segments..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|segment| match segment { |
| 91 | ParseSegmentrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#::Literalrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#Literal#(_) => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 92 | ParseSegmentrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#::Keyrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#Key#(key) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().key), |
| 93 | }) |
| 94 | } |
| 95 | |
| 96 | /// Combine this parsed format with the given values into a [`FormatArgs`] |
| 97 | pub fn with_argsrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]with_args().<'fs, 'fk, F: FormatKeyrust-analyzer cargo runtime-format 0.1.3 FormatKey#>( |
| 98 | &'fs self, |
| 99 | fmt: &'fk F, |
| 100 | ) -> FormatArgsrust-analyzer cargo runtime-format 0.1.3 FormatArgs#<'fs, 'fk, Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`], F> { |
| 101 | FormatArgsrust-analyzer cargo runtime-format 0.1.3 FormatArgs#::newrust-analyzer cargo runtime-format 0.1.3 impl#[`FormatArgs<'fs, 'fk, FS, FK>`]new().(self, fmt) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl<'a> TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<&'a str> for ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#<'a> { |
| 106 | type Errorrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`TryFrom<&'a str>`]Error# = FormatErrorrust-analyzer cargo runtime-format 0.1.3 FormatError#<'a>; |
| 107 | |
| 108 | fn try_fromrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`TryFrom<&'a str>`]try_from().(value: &'a str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`TryFrom<&'a str>`], Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`TryFrom<&'a str>`]::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> { |
| 109 | Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`TryFrom<&'a str>`]::newrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]new().(value) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | impl<'a> FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#<ParseSegmentrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#<'a>> for ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#<'a> { |
| 114 | fn from_iterrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`FromIterator<ParseSegment<'a>>`]from_iter().<T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = ParseSegmentrust-analyzer cargo runtime-format 0.1.3 parse/ParseSegment#<'a>>>(iter: T) -> Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`FromIterator<ParseSegment<'a>>`] { |
| 115 | Selfrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`][`FromIterator<ParseSegment<'a>>`] { |
| 116 | segmentsrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#segments.: FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#::from_iterrust-analyzer cargo tinyvec 1.12.0 tinyvec/impl#[`TinyVec<A>`][`FromIterator<<A as Array>::Item>`]from_iter().(iter), |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | #[cfg(test)] |
| 122 | mod testsrust-analyzer cargo runtime-format 0.1.3 compiled/tests/ { |
| 123 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/; |
| 124 | |
| 125 | use craterust-analyzer cargo runtime-format 0.1.3 crate/::{FormatErrorrust-analyzer cargo runtime-format 0.1.3 FormatError#, FormatKeyrust-analyzer cargo runtime-format 0.1.3 FormatKey#, FormatKeyErrorrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#}; |
| 126 | |
| 127 | use superrust-analyzer cargo runtime-format 0.1.3 compiled/::ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#; |
| 128 | |
| 129 | struct Messagerust-analyzer cargo runtime-format 0.1.3 compiled/tests/Message#; |
| 130 | impl FormatKeyrust-analyzer cargo runtime-format 0.1.3 FormatKey# for Messagerust-analyzer cargo runtime-format 0.1.3 compiled/tests/Message# { |
| 131 | fn fmtrust-analyzer cargo runtime-format 0.1.3 compiled/tests/impl#[Message][FormatKey]fmt().(&self, key: &str, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), FormatKeyErrorrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#> { |
| 132 | match key { |
| 133 | "recipient" => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("World").map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(FormatKeyErrorrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#::Fmtrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#Fmt#), |
| 134 | "time_descriptor" => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("morning").map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(FormatKeyErrorrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#::Fmtrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#Fmt#), |
| 135 | _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(FormatKeyErrorrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#::UnknownKeyrust-analyzer cargo runtime-format 0.1.3 FormatKeyError#UnknownKey#), |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 141 | fn compiled_happy_pathrust-analyzer cargo runtime-format 0.1.3 compiled/tests/compiled_happy_path().() { |
| 142 | let formatter = |
| 143 | ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#::newrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]new().("Hello, {recipient}. Hope you are having a nice {time_descriptor}.") |
| 144 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 145 | let expected = "Hello, World. Hope you are having a nice morning."; |
| 146 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatter.with_argsrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]with_args().(&Messagerust-analyzer cargo runtime-format 0.1.3 compiled/tests/Message#).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), expected); |
| 147 | } |
| 148 | |
| 149 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 150 | fn compiled_failed_parsingrust-analyzer cargo runtime-format 0.1.3 compiled/tests/compiled_failed_parsing().() { |
| 151 | let err = |
| 152 | ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#::newrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]new().("Hello, {recipient}. Hope you are having a nice {time_descriptor.") |
| 153 | .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 154 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(err, FormatErrorrust-analyzer cargo runtime-format 0.1.3 FormatError#::Parserust-analyzer cargo runtime-format 0.1.3 FormatError#Parse#("time_descriptor.")); |
| 155 | } |
| 156 | |
| 157 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 158 | fn compiled_keysrust-analyzer cargo runtime-format 0.1.3 compiled/tests/compiled_keys().() { |
| 159 | let parsed = |
| 160 | ParsedFmtrust-analyzer cargo runtime-format 0.1.3 compiled/ParsedFmt#::newrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]new().("Hello, {recipient}. Hope you are having a nice {time_descriptr}.") |
| 161 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 162 | let keys: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = parsed.keysrust-analyzer cargo runtime-format 0.1.3 compiled/impl#[`ParsedFmt<'a>`]keys().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 163 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(keys, ["recipient", "time_descriptr"]); |
| 164 | } |
| 165 | } |