commit d591c694dd3cb93112029c43cf2ee574a1417351
parent 45bae77a09f77e46f99af7ff22e75230de494800
Author: William Casarin <jb55@jb55.com>
Date: Wed, 28 Jun 2023 23:24:47 -0400
Add simple parser
Just getting warmed up. This will be used for note parsing.
Diffstat:
2 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
@@ -2,6 +2,7 @@ mod app;
//mod camera;
mod contacts;
mod error;
+mod parser;
pub use app::Damus;
pub use error::Error;
diff --git a/src/parser.rs b/src/parser.rs
@@ -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);
+ }
+}