From 59048f68ae72ea3d287ce0002237655545cd71db Mon Sep 17 00:00:00 2001 From: Ethiraric Date: Sun, 13 Oct 2024 14:42:50 +0200 Subject: [PATCH] Code cleanup after monorepo-ing. --- bench/src/lib.rs | 1 - parser/src/lib.rs | 2 +- parser/tests/yaml-test-suite.rs | 6 +++--- saphyr/src/annotated/marked_yaml.rs | 26 ++++++++++++-------------- saphyr/src/loader.rs | 20 ++++++++++---------- saphyr/src/yaml.rs | 8 +++----- 6 files changed, 29 insertions(+), 34 deletions(-) diff --git a/bench/src/lib.rs b/bench/src/lib.rs index 139597f..8b13789 100644 --- a/bench/src/lib.rs +++ b/bench/src/lib.rs @@ -1,2 +1 @@ - diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 36da373..9006276 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -39,6 +39,6 @@ mod input; mod parser; mod scanner; -pub use crate::input::BufferedInput; +pub use crate::input::{BufferedInput, Input}; pub use crate::parser::{Event, EventReceiver, Parser, SpannedEventReceiver, Tag}; pub use crate::scanner::{Marker, ScanError, Span, TScalarStyle}; diff --git a/parser/tests/yaml-test-suite.rs b/parser/tests/yaml-test-suite.rs index 5521433..8a3b75a 100644 --- a/parser/tests/yaml-test-suite.rs +++ b/parser/tests/yaml-test-suite.rs @@ -2,7 +2,7 @@ use std::fs::{self, DirEntry}; use libtest_mimic::{run_tests, Arguments, Outcome, Test}; -use saphyr::{yaml, Yaml, YamlLoader}; +use saphyr::{Hash, Yaml}; use saphyr_parser::{Event, EventReceiver, Parser, ScanError, TScalarStyle, Tag}; type Result> = std::result::Result; @@ -77,11 +77,11 @@ fn load_tests_from_file(entry: &DirEntry) -> Result>> { let test_name = file_name .strip_suffix(".yaml") .ok_or("unexpected filename")?; - let tests = YamlLoader::load_from_str(&fs::read_to_string(entry.path())?)?; + let tests = Yaml::load_from_str(&fs::read_to_string(entry.path())?)?; let tests = tests[0].as_vec().ok_or("no test list found in file")?; let mut result = vec![]; - let mut current_test = yaml::Hash::new(); + let mut current_test = Hash::new(); for (idx, test_data) in tests.iter().enumerate() { let name = if tests.len() > 1 { format!("{test_name}-{idx:02}") diff --git a/saphyr/src/annotated/marked_yaml.rs b/saphyr/src/annotated/marked_yaml.rs index dcc73dc..634e488 100644 --- a/saphyr/src/annotated/marked_yaml.rs +++ b/saphyr/src/annotated/marked_yaml.rs @@ -3,21 +3,21 @@ //! This is set aside so as to not clutter `annotated.rs`. use hashlink::LinkedHashMap; -use saphyr_parser::{Marker, Parser, ScanError}; +use saphyr_parser::{BufferedInput, Input, Parser, ScanError, Span}; use crate::{LoadableYamlNode, Yaml, YamlData, YamlLoader}; -/// A YAML node with [`Marker`]s pointing to the start of the node. +/// A YAML node with [`Span`]s pointing to the start of the node. /// /// This structure does not implement functions to operate on the YAML object. To access those, /// refer to the [`Self::data`] field. #[derive(Clone, Debug)] pub struct MarkedYaml { - /// The marker pointing to the start of the node. + /// The span indicating where in the input stream the object is. /// - /// The marker is relative to the start of the input stream that was given to the parser, not + /// The markers are relative to the start of the input stream that was given to the parser, not /// to the start of the document within the input stream. - pub marker: Marker, + pub span: Span, /// The YAML contents of the node. pub data: YamlData, } @@ -44,7 +44,7 @@ impl MarkedYaml { /// /// [`load_from_str`]: `Yaml::load_from_str` pub fn load_from_iter>(source: I) -> Result, ScanError> { - let mut parser = Parser::new(source); + let mut parser = Parser::new(BufferedInput::new(source)); Self::load_from_parser(&mut parser) } @@ -56,9 +56,7 @@ impl MarkedYaml { /// Returns `ScanError` when loading fails. /// /// [`load_from_str`]: `Yaml::load_from_str` - pub fn load_from_parser>( - parser: &mut Parser, - ) -> Result, ScanError> { + pub fn load_from_parser(parser: &mut Parser) -> Result, ScanError> { let mut loader = YamlLoader::::default(); parser.load(&mut loader, true)?; Ok(loader.into_documents()) @@ -83,7 +81,7 @@ impl std::hash::Hash for MarkedYaml { impl From> for MarkedYaml { fn from(value: YamlData) -> Self { Self { - marker: Marker::default(), + span: Span::default(), data: value, } } @@ -92,7 +90,7 @@ impl From> for MarkedYaml { impl LoadableYamlNode for MarkedYaml { fn from_bare_yaml(yaml: Yaml) -> Self { Self { - marker: Marker::default(), + span: Span::default(), data: match yaml { Yaml::Real(x) => YamlData::Real(x), Yaml::Integer(x) => YamlData::Integer(x), @@ -138,15 +136,15 @@ impl LoadableYamlNode for MarkedYaml { fn take(&mut self) -> Self { let mut taken_out = MarkedYaml { - marker: Marker::default(), + span: Span::default(), data: YamlData::BadValue, }; std::mem::swap(&mut taken_out, self); taken_out } - fn with_marker(mut self, marker: Marker) -> Self { - self.marker = marker; + fn with_span(mut self, span: Span) -> Self { + self.span = span; self } } diff --git a/saphyr/src/loader.rs b/saphyr/src/loader.rs index ab1a94c..bb9f309 100644 --- a/saphyr/src/loader.rs +++ b/saphyr/src/loader.rs @@ -3,7 +3,7 @@ use std::{collections::BTreeMap, sync::Arc}; use hashlink::LinkedHashMap; -use saphyr_parser::{Event, MarkedEventReceiver, Marker, ScanError, TScalarStyle, Tag}; +use saphyr_parser::{Event, ScanError, Span, SpannedEventReceiver, TScalarStyle, Tag}; use crate::{Hash, Yaml}; @@ -43,13 +43,13 @@ where } } -impl MarkedEventReceiver for YamlLoader +impl SpannedEventReceiver for YamlLoader where Node: LoadableYamlNode, { - fn on_event(&mut self, ev: Event, marker: Marker) { + fn on_event(&mut self, ev: Event, span: Span) { match ev { - Event::DocumentStart | Event::Nothing | Event::StreamStart | Event::StreamEnd => { + Event::DocumentStart(_) | Event::Nothing | Event::StreamStart | Event::StreamEnd => { // do nothing } Event::DocumentEnd => { @@ -57,14 +57,14 @@ where // empty document 0 => self .docs - .push(Node::from_bare_yaml(Yaml::BadValue).with_marker(marker)), + .push(Node::from_bare_yaml(Yaml::BadValue).with_span(span)), 1 => self.docs.push(self.doc_stack.pop().unwrap().0), _ => unreachable!(), } } Event::SequenceStart(aid, _) => { self.doc_stack.push(( - Node::from_bare_yaml(Yaml::Array(Vec::new())).with_marker(marker), + Node::from_bare_yaml(Yaml::Array(Vec::new())).with_span(span), aid, )); } @@ -74,7 +74,7 @@ where } Event::MappingStart(aid, _) => { self.doc_stack.push(( - Node::from_bare_yaml(Yaml::Hash(Hash::new())).with_marker(marker), + Node::from_bare_yaml(Yaml::Hash(Hash::new())).with_span(span), aid, )); self.key_stack.push(Node::from_bare_yaml(Yaml::BadValue)); @@ -122,14 +122,14 @@ where // Datatype is not specified, or unrecognized Yaml::from_str(&v) }; - self.insert_new_node((Node::from_bare_yaml(node).with_marker(marker), aid)); + self.insert_new_node((Node::from_bare_yaml(node).with_span(span), aid)); } Event::Alias(id) => { let n = match self.anchor_map.get(&id) { Some(v) => v.clone(), None => Node::from_bare_yaml(Yaml::BadValue), }; - self.insert_new_node((n.with_marker(marker), 0)); + self.insert_new_node((n.with_span(span), 0)); } } } @@ -253,7 +253,7 @@ pub trait LoadableYamlNode: Clone + std::hash::Hash + Eq { /// Provide the marker for the node (builder-style). #[inline] #[must_use] - fn with_marker(self, _: Marker) -> Self { + fn with_span(self, _: Span) -> Self { self } } diff --git a/saphyr/src/yaml.rs b/saphyr/src/yaml.rs index 581d5d7..6ed97ce 100644 --- a/saphyr/src/yaml.rs +++ b/saphyr/src/yaml.rs @@ -5,7 +5,7 @@ use std::{convert::TryFrom, ops::Index, ops::IndexMut}; use hashlink::LinkedHashMap; -use saphyr_parser::{Parser, ScanError}; +use saphyr_parser::{BufferedInput, Input, Parser, ScanError}; use crate::{loader::parse_f64, YamlLoader}; @@ -97,7 +97,7 @@ impl Yaml { /// # Errors /// Returns `ScanError` when loading fails. pub fn load_from_iter>(source: I) -> Result, ScanError> { - let mut parser = Parser::new(source); + let mut parser = Parser::new(BufferedInput::new(source)); Self::load_from_parser(&mut parser) } @@ -107,9 +107,7 @@ impl Yaml { /// /// # Errors /// Returns `ScanError` when loading fails. - pub fn load_from_parser>( - parser: &mut Parser, - ) -> Result, ScanError> { + pub fn load_from_parser(parser: &mut Parser) -> Result, ScanError> { let mut loader = YamlLoader::default(); parser.load(&mut loader, true)?; Ok(loader.into_documents())