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 crate::errors::ParseError;
11 use crate::tl::{Category, Definition};
12
13 const DEFINITION_SEP: char = ';';
14 const FUNCTIONS_SEP: &str = "---functions---";
15 const 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 TlIterator {
21 contents: String,
22 index: usize,
23 category: Category,
24 }
25
26 impl TlIterator {
27 pub(crate) fn new(contents: String) -> Self {
28 TlIterator {
29 contents,
30 index: 0,
31 category: Category::Types,
32 }
33 }
34 }
35
36 impl Iterator for TlIterator {
37 type Item = Result<Definition, ParseError>;
38
39 fn next(&mut self) -> Option<Self::Item> {
40 let definition = loop {
41 if self.index >= self.contents.len() {
42 return None;
43 }
44
45 let (end, is_empty) = {
46 let mut chars = self.contents[self.index..].char_indices().peekable();
47 let mut in_comment = false;
48 let mut is_empty = true;
49
50 loop {
51 if let Some((i, c)) = chars.next() {
52 if !in_comment && c == '/' {
53 if let Some((_, pc)) = chars.peek()
54 && *pc == '/'
55 {
56 in_comment = true;
57 }
58 } else if in_comment && c == '\n' {
59 in_comment = false;
60 }
61
62 if !in_comment {
63 if !c.is_whitespace() {
64 is_empty = false;
65 }
66
67 if c == DEFINITION_SEP {
68 break (self.index + i, is_empty);
69 }
70 }
71 } else {
72 break (self.contents.len(), is_empty);
73 }
74 }
75 };
76
77 let definition = self.contents[self.index..end].trim();
78 self.index = end + DEFINITION_SEP.len_utf8();
79
80 if !is_empty {
81 break definition;
82 }
83 };
84
85 // Get rid of the leading separator and adjust category
86 let definition = if definition.starts_with("---") {
87 if let Some(definition) = definition.strip_prefix(FUNCTIONS_SEP) {
88 self.category = Category::Functions;
89 definition.trim()
90 } else if let Some(definition) = definition.strip_prefix(TYPES_SEP) {
91 self.category = Category::Types;
92 definition.trim()
93 } else {
94 return Some(Err(ParseError::UnknownSeparator));
95 }
96 } else {
97 definition
98 };
99
100 // Yield the fixed definition
101 Some(match definition.parse::<Definition>() {
102 Ok(mut d) => {
103 d.category = self.category;
104 Ok(d)
105 }
106 x => x,
107 })
108 }
109 }
110
111 #[cfg(test)]
112 mod tests {
113 use super::*;
114 use crate::errors::ParseError;
115
116 #[test]
117 fn parse_bad_separator() {
118 let mut it = TlIterator::new("---foo---".into());
119 assert_eq!(it.next(), Some(Err(ParseError::UnknownSeparator)));
120 assert_eq!(it.next(), None);
121 }
122
123 #[test]
124 fn parse_file() {
125 let mut it = TlIterator::new(
126 "
127 // leading; comment
128 first = t; // inline comment
129 second and bad;
130 third = t;
131 // trailing comment
132 "
133 .into(),
134 );
135
136 assert_eq!(it.next().unwrap().unwrap().name, "first");
137 assert!(it.next().unwrap().is_err());
138 assert_eq!(it.next().unwrap().unwrap().name, "third");
139 assert_eq!(it.next(), None);
140 }
141 }