mirror of
https://github.com/damus-io/notedeck.git
synced 2024-09-18 21:03:31 +00:00
Add simple parser
Just getting warmed up. This will be used for note parsing.
This commit is contained in:
parent
45bae77a09
commit
d591c694dd
@ -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
41
src/parser.rs
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user