Add simple parser

Just getting warmed up. This will be used for note parsing.
This commit is contained in:
William Casarin 2023-06-28 23:24:47 -04:00
parent 45bae77a09
commit d591c694dd
2 changed files with 42 additions and 0 deletions

View File

@ -2,6 +2,7 @@ mod app;
//mod camera;
mod contacts;
mod error;
mod parser;
pub use app::Damus;
pub use error::Error;

41
src/parser.rs Normal file
View File

@ -0,0 +1,41 @@
use log::info;
struct Parser<'a> {
data: &'a str,
pos: usize,
}
impl<'a> Parser<'a> {
fn new(data: &'a str) -> Parser {
Parser { data: data, pos: 0 }
}
fn parse_until(&mut self, needle: char) -> bool {
let mut count = 0;
for c in self.data[self.pos..].chars() {
if c == needle {
self.pos += count - 1;
return true;
} else {
count += 1;
}
}
return false;
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parser() {
let s = "hey there #hashtag";
let mut parser = Parser::new(s);
parser.parse_until('#');
assert_eq!(parser.pos, 9);
parser.parse_until('t');
assert_eq!(parser.pos, 14);
}
}