| 1 | // Copyright 2020 - developers of the `grammers` project. |
| 2 | // Copyright 2022 - developers of the `tdlib-rs` project. |
| 3 | // Copyright 2024 - developers of the `tgt` and `tdlib-rs` projects. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 8 | // option. This file may not be copied, modified, or distributed |
| 9 | // except according to those terms. |
| 10 | 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/; |
| 11 | 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#; |
| 12 | |
| 13 | use craterust-analyzer cargo tdlib-rs-parser 1.4.0 crate/::errorsrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/::ParamParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#; |
| 14 | |
| 15 | /// The type of a definition or a parameter. |
| 16 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 17 | pub struct Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 18 | /// The name of the type. |
| 19 | pub namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 20 | |
| 21 | /// Whether this type is bare or boxed. |
| 22 | pub barerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#bare.: bool, |
| 23 | |
| 24 | /// If the type has a generic argument, which is its type. |
| 25 | pub generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#>>, |
| 26 | } |
| 27 | |
| 28 | impl 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# for Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 29 | fn fmtrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 30 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", self.namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#name.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 31 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(generic_arg) = &self.generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg. { |
| 32 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "<{generic_arg}>")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 33 | } |
| 34 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 39 | type Errrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]Err# = ParamParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#; |
| 40 | |
| 41 | /// Parses a type. |
| 42 | /// |
| 43 | /// # Examples |
| 44 | /// |
| 45 | /// ``` |
| 46 | /// use tdlib_rs_parser::tl::Type; |
| 47 | /// |
| 48 | /// assert!("vector<int>".parse::<Type>().is_ok()); |
| 49 | /// ``` |
| 50 | fn from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().(ty: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr], Selfrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> { |
| 51 | // Parse `type<generic_arg>` |
| 52 | let (ty, generic_arg) = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pos) = ty.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('<') { |
| 53 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ty.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().('>') { |
| 54 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParamParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#::InvalidGenericrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#InvalidGeneric#); |
| 55 | } |
| 56 | ( |
| 57 | &ty[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#pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., |
| 58 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().(&ty[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().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..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#ty.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/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]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 result/impl#[`Result<T, E>`][Try]branch().)), |
| 59 | ) |
| 60 | } else { |
| 61 | (ty, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) |
| 62 | }; |
| 63 | |
| 64 | if ty.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 65 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParamParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#::Emptyrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#Empty#); |
| 66 | } |
| 67 | |
| 68 | // Safe to unwrap because we just checked is not empty |
| 69 | let bare = ty.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]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().().is_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_lowercase().(); |
| 70 | |
| 71 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr] { |
| 72 | namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#name.: ty.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 73 | barerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#bare., |
| 74 | generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg., |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[cfg(test)] |
| 80 | mod testsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/tests/ { |
| 81 | use superrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/::*; |
| 82 | |
| 83 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 84 | fn check_empty_simplerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/tests/check_empty_simple().() { |
| 85 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().(""), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParamParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#::Emptyrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#Empty#)); |
| 86 | } |
| 87 | |
| 88 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 89 | fn check_simplerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/tests/check_simple().() { |
| 90 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 91 | Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().("foo"), |
| 92 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 93 | namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#name.: "foo".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 94 | barerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#bare.: true, |
| 95 | generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 96 | }) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 101 | fn check_emptyrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/tests/check_empty().() { |
| 102 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().(""), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParamParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#::Emptyrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#Empty#)); |
| 103 | } |
| 104 | |
| 105 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 106 | fn check_barerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/tests/check_bare().() { |
| 107 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().("foo"), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { barerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#bare.: true, .. }))); |
| 108 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!( |
| 109 | Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().("Foo"), |
| 110 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { barerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#bare.: false, .. }) |
| 111 | )); |
| 112 | } |
| 113 | |
| 114 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 115 | fn check_generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/tests/check_generic_arg().() { |
| 116 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!( |
| 117 | Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().("foo"), |
| 118 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 119 | generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 120 | .. |
| 121 | }) |
| 122 | )); |
| 123 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(match Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().("foo<bar>") { |
| 124 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 125 | generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x), |
| 126 | .. |
| 127 | }) => *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().x == "bar".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 128 | _ => false, |
| 129 | }); |
| 130 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(match Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().("foo<bar>") { |
| 131 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 132 | generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x), |
| 133 | .. |
| 134 | }) => *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().x == "bar".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 135 | _ => false, |
| 136 | }); |
| 137 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(match Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/impl#[Type][FromStr]from_str().("foo<bar<baz>>") { |
| 138 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 139 | generic_argrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#generic_arg.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x), |
| 140 | .. |
| 141 | }) => *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().x == "bar<baz>".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 142 | _ => false, |
| 143 | }); |
| 144 | } |
| 145 | } |