| 1 | use superrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/::{FmtArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#, FmtStrComponentrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#, FormatStrrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#, ParseErrorKindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#, WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#}; |
| 2 | |
| 3 | use craterust-analyzer cargo const_format_proc_macros 0.2.34 crate/::{ |
| 4 | formattingrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/::{FormattingFlagsrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/FormattingFlags#, IsAlternaterust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#, NumberFormattingrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#}, |
| 5 | parse_utilsrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/::StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#, |
| 6 | }; |
| 7 | |
| 8 | #[cfg(test)] |
| 9 | impl FmtStrComponentrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent# { |
| 10 | pub(superrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/) fn strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtStrComponent]str().(s: &str) -> Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtStrComponent] { |
| 11 | Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtStrComponent]::Strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#Str#(s.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#::dummyrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/impl#[StrRawness]dummy().()) |
| 12 | } |
| 13 | pub(superrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/) fn argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtStrComponent]arg().(which_arg: WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#, formatting: FormattingFlagsrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/FormattingFlags#) -> Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtStrComponent] { |
| 14 | Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtStrComponent]::Argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#Arg#(FmtArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg# { |
| 15 | which_argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#which_arg., |
| 16 | formattingrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#formatting., |
| 17 | rawnessrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#rawness.: StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#::dummyrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/impl#[StrRawness]dummy().(), |
| 18 | }) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl FmtArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg# { |
| 23 | fn newrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtArg]new().(which_arg: WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#, formatting: FormattingFlagsrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/FormattingFlags#, rawness: StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#) -> Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtArg] { |
| 24 | Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtArg] { |
| 25 | which_argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#which_arg., |
| 26 | formattingrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#formatting., |
| 27 | rawnessrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#rawness., |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | #[allow(dead_code)] |
| 33 | impl WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg# { |
| 34 | pub(superrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/) fn identrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[WhichArg]ident().(s: &str) -> Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[WhichArg] { |
| 35 | Selfrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[WhichArg]::Identrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#Ident#(s.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | ///////////////////////////////////// |
| 40 | |
| 41 | #[cfg(test)] |
| 42 | impl 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# for FormatStrrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr# { |
| 43 | type Errrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FormatStr][FromStr]Err# = ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#; |
| 44 | |
| 45 | fn from_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FormatStr][FromStr]from_str().(input: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<FormatStrrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 46 | parse_format_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_format_str().(input, StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#::dummyrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/impl#[StrRawness]dummy().()) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl FormatStrrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr# { |
| 51 | pub fn parserust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FormatStr]parse().(input: &str, rawness: StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<FormatStrrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 52 | parse_format_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_format_str().(input, rawness) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn parse_format_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_format_str().(input: &str, rawness: StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<FormatStrrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 57 | let mut components = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::<FmtStrComponentrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#>::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 58 | |
| 59 | let mut arg_start = 0; |
| 60 | |
| 61 | loop { |
| 62 | let open_pos = input.find_fromrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[str][StrExt]find_from().('{', arg_start); |
| 63 | |
| 64 | let str = &input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().arg_start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#open_pos.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().())]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 65 | components.push_arg_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[`Vec<FmtStrComponent>`][VecExt]push_arg_str().(parse_mid_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_mid_str().(str, arg_start)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., rawness); |
| 66 | |
| 67 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(open_pos) = open_pos { |
| 68 | let after_open = open_pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 69 | if input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().after_open..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('{') { |
| 70 | components.push_arg_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[`Vec<FmtStrComponent>`][VecExt]push_arg_str().("{".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), rawness); |
| 71 | |
| 72 | arg_start = open_pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2; |
| 73 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(close_pos) = input.find_fromrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[str][StrExt]find_from().('}', after_open) { |
| 74 | let after_close = close_pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 75 | |
| 76 | let arg = parse_fmt_argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_fmt_arg().(&input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().after_open..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#close_pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., after_open, rawness)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 77 | components.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(FmtStrComponentrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#::Argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#Arg#(arg)); |
| 78 | |
| 79 | arg_start = after_close; |
| 80 | } else { |
| 81 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError# { |
| 82 | posrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#pos.: open_pos, |
| 83 | kindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#kind.: ParseErrorKindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#::UnclosedArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#UnclosedArg#, |
| 84 | }); |
| 85 | } |
| 86 | } else { |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(FormatStrrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr# { listrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FormatStr#list.: components }) |
| 92 | } |
| 93 | |
| 94 | /// Parses the text between arguments, to unescape `}}` into `}` |
| 95 | fn parse_mid_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_mid_str().(str: &str, starts_at: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 96 | let mut buffer = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(str.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()); |
| 97 | |
| 98 | let mut starts_pos = 0; |
| 99 | let bytes = str.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 100 | |
| 101 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(close_pos) = str.find_fromrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[str][StrExt]find_from().('}', starts_pos) { |
| 102 | let after_close = close_pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 103 | if bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(after_close) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&b'}') { |
| 104 | buffer.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&str[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().starts_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#after_close]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 105 | starts_pos = after_close rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 106 | } else { |
| 107 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError# { |
| 108 | posrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#pos.: starts_at rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().close_pos, |
| 109 | kindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#kind.: ParseErrorKindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#::InvalidClosedArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#InvalidClosedArg#, |
| 110 | }); |
| 111 | } |
| 112 | } |
| 113 | buffer.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&str[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().starts_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 114 | |
| 115 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buffer) |
| 116 | } |
| 117 | |
| 118 | /// Parses the format arguments (`{:?}`, `{foo:}`, `{0}`, etc). |
| 119 | /// |
| 120 | /// `starts_at` is the offset of `input` in the formatting string. |
| 121 | fn parse_fmt_argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_fmt_arg().(input: &str, starts_at: usize, rawness: StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<FmtArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 122 | let colon = input.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().(':'); |
| 123 | |
| 124 | let which_arg_str = &input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#colon.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().())]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 125 | let formatting_str = colon.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().("", |x| &input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 126 | let formatting_starts_at = colon.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(), |x| starts_at rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 127 | |
| 128 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(FmtArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtArg#::newrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[FmtArg]new().( |
| 129 | parse_which_argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_which_arg().(which_arg_str, starts_at)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 130 | parse_formattingrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_formatting().(formatting_str, formatting_starts_at)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 131 | rawness, |
| 132 | )) |
| 133 | } |
| 134 | |
| 135 | /// Parses the name of the argument in `{foo}`, `{}`, `{bar:?}` |
| 136 | /// |
| 137 | /// `starts_at` is the offset of `input` in the formatting string. |
| 138 | fn parse_which_argrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_which_arg().(input: &str, starts_at: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 139 | if input.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 140 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#::Positionalrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#Positional#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)) |
| 141 | } else if input.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_digit().() { |
| 142 | match input.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<usize>() { |
| 143 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(number) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#::Positionalrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#Positional#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(number))), |
| 144 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError# { |
| 145 | posrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#pos.: starts_at, |
| 146 | kindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#kind.: ParseErrorKindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#::NotANumberrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#NotANumber# { |
| 147 | whatrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#NotANumber#what.: input.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 148 | }, |
| 149 | }), |
| 150 | } |
| 151 | } else { |
| 152 | parse_identrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_ident().(input, starts_at) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /// Parses the `?` and other formatters inside formatting arguments (`{}`). |
| 157 | /// |
| 158 | /// `starts_at` is the offset of `input` in the formatting string. |
| 159 | fn parse_formattingrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_formatting().(input: &str, starts_at: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<FormattingFlagsrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/FormattingFlags#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 160 | match input { |
| 161 | "#" => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(FormattingFlagsrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/FormattingFlags#::displayrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/impl#[FormattingFlags]display().(IsAlternaterust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#::Yesrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#Yes#)), |
| 162 | "" => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(FormattingFlagsrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/FormattingFlags#::displayrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/impl#[FormattingFlags]display().(IsAlternaterust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#::Norust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#No#)), |
| 163 | _ => {} |
| 164 | } |
| 165 | |
| 166 | let mut bytes = input.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 167 | |
| 168 | let make_error = || ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError# { |
| 169 | posrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#pos.: starts_at, |
| 170 | kindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#kind.: ParseErrorKindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#::UnknownFormattingrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#UnknownFormatting# { |
| 171 | whatrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#UnknownFormatting#what.: input.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 172 | }, |
| 173 | }; |
| 174 | |
| 175 | if let [before @ .., b'?'] = bytes { |
| 176 | bytes = before; |
| 177 | } |
| 178 | |
| 179 | let mut num_fmt = NumberFormattingrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#::Decimalrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#Decimal#; |
| 180 | let mut is_alternate = IsAlternaterust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#::Norust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#No#; |
| 181 | |
| 182 | for byte in bytes { |
| 183 | match byte { |
| 184 | b'b' if num_fmt.is_regularrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/impl#[NumberFormatting]is_regular().() => num_fmt = NumberFormattingrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#::Binaryrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#Binary#, |
| 185 | b'x' if num_fmt.is_regularrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/impl#[NumberFormatting]is_regular().() => num_fmt = NumberFormattingrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#::LowerHexadecimalrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#LowerHexadecimal#, |
| 186 | b'X' if num_fmt.is_regularrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/impl#[NumberFormatting]is_regular().() => num_fmt = NumberFormattingrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#::Hexadecimalrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/NumberFormatting#Hexadecimal#, |
| 187 | b'#' => is_alternate = IsAlternaterust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#::Yesrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/IsAlternate#Yes#, |
| 188 | _ => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(make_error()), |
| 189 | } |
| 190 | } |
| 191 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(FormattingFlagsrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/FormattingFlags#::debugrust-analyzer cargo const_format_proc_macros 0.2.34 formatting/impl#[FormattingFlags]debug().(num_fmt, is_alternate)) |
| 192 | } |
| 193 | |
| 194 | /// Parses an identifier in a formatting argument. |
| 195 | /// |
| 196 | /// `starts_at` is the offset of `input` in the formatting string. |
| 197 | fn parse_identrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/parse_ident().(ident_str: &str, starts_at: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#, ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#> { |
| 198 | if is_identrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/is_ident().(ident_str) { |
| 199 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(WhichArgrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#::Identrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/WhichArg#Ident#(ident_str.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())) |
| 200 | } else { |
| 201 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError# { |
| 202 | posrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#pos.: starts_at, |
| 203 | kindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseError#kind.: ParseErrorKindrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#::NotAnIdentrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#NotAnIdent# { |
| 204 | whatrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/errors/ParseErrorKind#NotAnIdent#what.: ident_str.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 205 | }, |
| 206 | }) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | //////////////////////////////////////////////////////////////////////////////// |
| 211 | |
| 212 | fn is_identrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/is_ident().(s: &str) -> bool { |
| 213 | use unicode_xidrust-analyzer cargo unicode-xid 0.2.6 crate/::UnicodeXIDrust-analyzer cargo unicode-xid 0.2.6 UnicodeXID#; |
| 214 | |
| 215 | if s.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() || s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."_" { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | let mut chars = s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().(); |
| 220 | let first = chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 221 | |
| 222 | // For some reason '_' is not considered a valid character for the stard of an ident |
| 223 | (first.is_xid_startrust-analyzer cargo unicode-xid 0.2.6 impl#[char][UnicodeXID]is_xid_start().() || first rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'_') && chars.allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#all().(|c| c.is_xid_continuerust-analyzer cargo unicode-xid 0.2.6 impl#[char][UnicodeXID]is_xid_continue().()) |
| 224 | } |
| 225 | |
| 226 | //////////////////////////////////////////////////////////////////////////////// |
| 227 | |
| 228 | trait VecExtrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/VecExt# { |
| 229 | fn push_arg_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/VecExt#push_arg_str().(&mut self, str: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, rawness: StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#); |
| 230 | } |
| 231 | |
| 232 | impl VecExtrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/VecExt# for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<FmtStrComponentrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#> { |
| 233 | fn push_arg_strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[`Vec<FmtStrComponent>`][VecExt]push_arg_str().(&mut self, str: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, rawness: StrRawnessrust-analyzer cargo const_format_proc_macros 0.2.34 parse_utils/StrRawness#) { |
| 234 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().str.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]is_empty().() { |
| 235 | self.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(FmtStrComponentrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#::Strrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/FmtStrComponent#Str#(str, rawness)); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | trait StrExtrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/StrExt# { |
| 241 | fn find_fromrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/StrExt#find_from().(&self, c: char, from: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>; |
| 242 | } |
| 243 | |
| 244 | impl StrExtrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/StrExt# for str { |
| 245 | fn find_fromrust-analyzer cargo const_format_proc_macros 0.2.34 format_str/parsing/impl#[str][StrExt]find_from().(&self, c: char, from: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> { |
| 246 | self[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().from..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().(c).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|p| p rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().from) |
| 247 | } |
| 248 | } |