Skip to content

Commit

Permalink
parser: add Parser::new_from_str(&str)
Browse files Browse the repository at this point in the history
  • Loading branch information
davvid committed Mar 24, 2024
1 parent 0ab2930 commit 02a4451
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub struct Parser<T> {
/// /// Load events from a yaml string.
/// fn str_to_events(yaml: &str) -> Vec<Event> {
/// let mut sink = EventSink { events: Vec::new() };
/// let mut parser = Parser::new(yaml.chars());
/// let mut parser = Parser::new_from_str(yaml);
/// // Load events using our sink as the receiver.
/// parser.load(&mut sink, true).unwrap();
/// sink.events
Expand Down Expand Up @@ -210,8 +210,16 @@ impl<R: EventReceiver> MarkedEventReceiver for R {
/// A convenience alias for a `Result` of a parser event.
pub type ParseResult = Result<(Event, Marker), ScanError>;

impl<'a> Parser<core::str::Chars<'a>> {
/// Create a new instance of a parser from a &str.
#[must_use]
pub fn new_from_str(value: &'a str) -> Self {
Parser::new(value.chars())
}
}

impl<T: Iterator<Item = char>> Parser<T> {
/// Crate a new instance of a parser from the given input of characters.
/// Create a new instance of a parser from the given input of characters.
pub fn new(src: T) -> Parser<T> {
Parser {
scanner: Scanner::new(src),
Expand Down Expand Up @@ -1099,7 +1107,7 @@ a4:
- 2
a5: *x
";
let mut p = Parser::new(s.chars());
let mut p = Parser::new_from_str(s);
while {
let event_peek = p.peek().unwrap().clone();
let event = p.next_token().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl EventReceiver for YamlChecker {

fn str_to_test_events(docs: &str) -> Vec<TestEvent> {
let mut p = YamlChecker { evs: Vec::new() };
let mut parser = Parser::new(docs.chars());
let mut parser = Parser::new_from_str(docs);
parser.load(&mut p, true).unwrap();
p.evs
}
Expand Down
2 changes: 1 addition & 1 deletion tests/yaml-test-suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn load_tests_from_file(entry: &DirEntry) -> Result<Vec<Test<YamlTest>>> {

fn parse_to_events(source: &str) -> Result<Vec<String>, ScanError> {
let mut reporter = EventReporter::new();
Parser::new(source.chars()).load(&mut reporter, true)?;
Parser::new_from_str(source).load(&mut reporter, true)?;
Ok(reporter.events)
}

Expand Down
2 changes: 1 addition & 1 deletion tools/dump_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl MarkedEventReceiver for EventSink {

fn str_to_events(yaml: &str) -> Vec<(Event, Marker)> {
let mut sink = EventSink { events: Vec::new() };
let mut parser = Parser::new(yaml.chars());
let mut parser = Parser::new_from_str(yaml);
// Load events using our sink as the receiver.
parser.load(&mut sink, true).unwrap();
sink.events
Expand Down
2 changes: 1 addition & 1 deletion tools/run_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl MarkedEventReceiver for NullSink {
/// Parse the given input, returning elapsed time in nanoseconds.
fn do_parse(input: &str) -> u64 {
let mut sink = NullSink {};
let mut parser = Parser::new(input.chars());
let mut parser = Parser::new_from_str(input);
let begin = std::time::Instant::now();
parser.load(&mut sink, true).unwrap();
let end = std::time::Instant::now();
Expand Down
2 changes: 1 addition & 1 deletion tools/time_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
f.read_to_string(&mut s).unwrap();

let mut sink = NullSink {};
let mut parser = Parser::new(s.chars());
let mut parser = Parser::new_from_str(&s);

// Load events using our sink as the receiver.
let begin = std::time::Instant::now();
Expand Down

0 comments on commit 02a4451

Please sign in to comment.