Debian Codegraph @

src/tl_iterator.rs

raw symbols SCIP
1 // Copyright 2020 - developers of the `grammers` project.
2 // Copyright 2021 - 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 craterust-analyzer cargo tdlib-rs-parser 1.4.0 crate/::errorsrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/::ParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParseError#;
11 use craterust-analyzer cargo tdlib-rs-parser 1.4.0 crate/::tlrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/::{Categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#, Definitionrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/definition/Definition#};
12
13 const DEFINITION_SEPrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/DEFINITION_SEP.: char = ';';
14 const FUNCTIONS_SEPrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/FUNCTIONS_SEP.: &str = "---functions---";
15 const TYPES_SEPrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TYPES_SEP.: &str = "---types---";
16
17 /// An iterator over [Type Language] definitions.
18 ///
19 /// [Type Language]: https://core.telegram.org/mtproto/TL
20 pub struct TlIteratorrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator# {
21 contentsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#contents.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
22 indexrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#index.: usize,
23 categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#category.: Categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#,
24 }
25
26 impl TlIteratorrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator# {
27 pub(craterust-analyzer cargo tdlib-rs-parser 1.4.0 crate/) fn newrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator]new().(contents: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator] {
28 TlIteratorrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator# {
29 contentsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#contents.,
30 indexrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#index.: 0,
31 categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#category.: Categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#::Typesrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#Types#,
32 }
33 }
34 }
35
36 impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for TlIteratorrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator# {
37 type Itemrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]Item# = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Definitionrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/definition/Definition#, ParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParseError#>;
38
39 fn nextrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> {
40 let definition = loop {
41 if self.indexrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#index. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.contentsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#contents..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() {
42 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
43 }
44
45 let (end, is_empty) = {
46 let mut chars = self.contentsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#contents.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().self.indexrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#index...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..char_indicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]char_indices().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().();
47 let mut in_comment = false;
48 let mut is_empty = true;
49
50 loop {
51 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((i, c)) = chars.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() {
52 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().in_comment && c 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().'/' {
53 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_, pc)) = chars.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().()
54 && *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().pc 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().'/'
55 {
56 in_comment = true;
57 }
58 } else if in_comment && c 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().'\n' {
59 in_comment = false;
60 }
61
62 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().in_comment {
63 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_whitespace().() {
64 is_empty = false;
65 }
66
67 if c 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().DEFINITION_SEPrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/DEFINITION_SEP. {
68 break (self.indexrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#index. 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().i, is_empty);
69 }
70 }
71 } else {
72 break (self.contentsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#contents..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(), is_empty);
73 }
74 }
75 };
76
77 let definition = self.contentsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#contents.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index().self.indexrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#index...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()..trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().();
78 self.indexrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#index. = end 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().DEFINITION_SEPrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/DEFINITION_SEP..len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().();
79
80 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_empty {
81 break definition;
82 }
83 };
84
85 // Get rid of the leading separator and adjust category
86 let definition = if definition.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("---") {
87 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(definition) = definition.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().(FUNCTIONS_SEPrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/FUNCTIONS_SEP.) {
88 self.categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#category. = Categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#::Functionsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#Functions#;
89 definition.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().()
90 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(definition) = definition.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().(TYPES_SEPrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TYPES_SEP.) {
91 self.categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#category. = Categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#::Typesrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/category/Category#Types#;
92 definition.trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().()
93 } else {
94 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParseError#::UnknownSeparatorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParseError#UnknownSeparator#));
95 }
96 } else {
97 definition
98 };
99
100 // Yield the fixed definition
101 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(match definition.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<Definitionrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/definition/Definition#>() {
102 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mut d) => {
103 d.categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl/definition/Definition#category. = self.categoryrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#category.;
104 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(d)
105 }
106 x => x,
107 })
108 }
109 }
110
111 #[cfg(test)]
112 mod testsrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/tests/ {
113 use superrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/::*;
114 use craterust-analyzer cargo tdlib-rs-parser 1.4.0 crate/::errorsrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/::ParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParseError#;
115
116 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
117 fn parse_bad_separatorrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/tests/parse_bad_separator().() {
118 let mut it = TlIteratorrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#::newrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator]new().("---foo---".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
119 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(it.nextrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]next().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParseError#::UnknownSeparatorrust-analyzer cargo tdlib-rs-parser 1.4.0 errors/ParseError#UnknownSeparator#)));
120 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(it.nextrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]next().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
121 }
122
123 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
124 fn parse_filerust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/tests/parse_file().() {
125 let mut it = TlIteratorrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/TlIterator#::newrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator]new().(
126 "
127 // leading; comment
128 first = t; // inline comment
129 second and bad;
130 third = t;
131 // trailing comment
132 "
133 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
134 );
135
136 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(it.nextrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/definition/Definition#name., "first");
137 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(it.nextrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
138 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(it.nextrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().namerust-analyzer cargo tdlib-rs-parser 1.4.0 tl/definition/Definition#name., "third");
139 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(it.nextrust-analyzer cargo tdlib-rs-parser 1.4.0 tl_iterator/impl#[TlIterator][Iterator]next().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
140 }
141 }