| 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 | use craterust-analyzer cargo tdlib-rs-parser 1.4.0 crate/::tlrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/::Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#; |
| 15 | |
| 16 | /// A single parameter, with a name and a type. |
| 17 | #[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:)] |
| 18 | pub struct Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter# { |
| 19 | /// The name of the parameter. |
| 20 | pub namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 21 | |
| 22 | /// The type of the parameter. |
| 23 | pub tyrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#ty.: Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#, |
| 24 | |
| 25 | /// The description of the parameter. |
| 26 | pub descriptionrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#description.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 27 | } |
| 28 | |
| 29 | 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 Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter# { |
| 30 | fn fmtrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][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# { |
| 31 | 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/parameter/Parameter#name., self.tyrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#ty.) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter# { |
| 36 | type Errrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]Err# = ParamParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#; |
| 37 | |
| 38 | /// Parses a parameter. |
| 39 | /// |
| 40 | /// # Examples |
| 41 | /// |
| 42 | /// ``` |
| 43 | /// use tdlib_rs_parser::tl::Parameter; |
| 44 | /// |
| 45 | /// assert!("foo:Bar".parse::<Parameter>().is_ok()); |
| 46 | /// ``` |
| 47 | fn from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().(param: &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/parameter/impl#[Parameter][FromStr], Selfrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> { |
| 48 | // Parse `name:type` |
| 49 | let (name, ty) = { |
| 50 | let mut it = param.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(':'); |
| 51 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(n) = it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().() { |
| 52 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(t) = it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().() { |
| 53 | (n, t) |
| 54 | } else { |
| 55 | 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#::NotImplementedrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#NotImplemented#); |
| 56 | } |
| 57 | } else { |
| 58 | 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#); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | if name.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 63 | 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#); |
| 64 | } |
| 65 | |
| 66 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter# { |
| 67 | namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#name.: name.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 68 | tyrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#ty.: ty.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 69 | descriptionrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#description.: 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().(), |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #[cfg(test)] |
| 75 | mod testsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/tests/ { |
| 76 | use superrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/::*; |
| 77 | use craterust-analyzer cargo tdlib-rs-parser 1.4.0 crate/::tlrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/::Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#; |
| 78 | |
| 79 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 80 | fn parse_empty_paramrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/tests/parse_empty_param().() { |
| 81 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().(":noname"), 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#)); |
| 82 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().("notype:"), 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#)); |
| 83 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][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#)); |
| 84 | } |
| 85 | |
| 86 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 87 | fn parse_unknown_paramrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/tests/parse_unknown_param().() { |
| 88 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 89 | Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().(""), |
| 90 | 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#::NotImplementedrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#NotImplemented#) |
| 91 | ); |
| 92 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 93 | Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().("no colon"), |
| 94 | 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#::NotImplementedrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#NotImplemented#) |
| 95 | ); |
| 96 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 97 | Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().("colonless"), |
| 98 | 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#::NotImplementedrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParamParseError#NotImplemented#) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 103 | fn parse_bad_genericsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/tests/parse_bad_generics().() { |
| 104 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 105 | Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().("foo:<bar"), |
| 106 | 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#) |
| 107 | ); |
| 108 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 109 | Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().("foo:bar<"), |
| 110 | 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#) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 115 | fn parse_valid_paramrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/tests/parse_valid_param().() { |
| 116 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 117 | Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#::from_strrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/impl#[Parameter][FromStr]from_str().("foo:bar<baz>"), |
| 118 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Parameterrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter# { |
| 119 | namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#name.: "foo".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 120 | tyrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#ty.: Typerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type# { |
| 121 | namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#name.: "bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 122 | barerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/ty/Type#bare.: true, |
| 123 | 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#(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().("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().())), |
| 124 | }, |
| 125 | descriptionrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/parameter/Parameter#description.: 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().(), |
| 126 | }) |
| 127 | ); |
| 128 | } |
| 129 | } |