Code cleanup after monorepo-ing.

This commit is contained in:
Ethiraric 2024-10-13 14:42:50 +02:00
parent 8ee4921e5e
commit 59048f68ae
6 changed files with 29 additions and 34 deletions

View file

@ -1,2 +1 @@

View file

@ -39,6 +39,6 @@ mod input;
mod parser; mod parser;
mod scanner; 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::parser::{Event, EventReceiver, Parser, SpannedEventReceiver, Tag};
pub use crate::scanner::{Marker, ScanError, Span, TScalarStyle}; pub use crate::scanner::{Marker, ScanError, Span, TScalarStyle};

View file

@ -2,7 +2,7 @@ use std::fs::{self, DirEntry};
use libtest_mimic::{run_tests, Arguments, Outcome, Test}; 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}; use saphyr_parser::{Event, EventReceiver, Parser, ScanError, TScalarStyle, Tag};
type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>; type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
@ -77,11 +77,11 @@ fn load_tests_from_file(entry: &DirEntry) -> Result<Vec<Test<YamlTest>>> {
let test_name = file_name let test_name = file_name
.strip_suffix(".yaml") .strip_suffix(".yaml")
.ok_or("unexpected filename")?; .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 tests = tests[0].as_vec().ok_or("no test list found in file")?;
let mut result = vec![]; 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() { for (idx, test_data) in tests.iter().enumerate() {
let name = if tests.len() > 1 { let name = if tests.len() > 1 {
format!("{test_name}-{idx:02}") format!("{test_name}-{idx:02}")

View file

@ -3,21 +3,21 @@
//! This is set aside so as to not clutter `annotated.rs`. //! This is set aside so as to not clutter `annotated.rs`.
use hashlink::LinkedHashMap; use hashlink::LinkedHashMap;
use saphyr_parser::{Marker, Parser, ScanError}; use saphyr_parser::{BufferedInput, Input, Parser, ScanError, Span};
use crate::{LoadableYamlNode, Yaml, YamlData, YamlLoader}; 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, /// This structure does not implement functions to operate on the YAML object. To access those,
/// refer to the [`Self::data`] field. /// refer to the [`Self::data`] field.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct MarkedYaml { 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. /// to the start of the document within the input stream.
pub marker: Marker, pub span: Span,
/// The YAML contents of the node. /// The YAML contents of the node.
pub data: YamlData<MarkedYaml>, pub data: YamlData<MarkedYaml>,
} }
@ -44,7 +44,7 @@ impl MarkedYaml {
/// ///
/// [`load_from_str`]: `Yaml::load_from_str` /// [`load_from_str`]: `Yaml::load_from_str`
pub fn load_from_iter<I: Iterator<Item = char>>(source: I) -> Result<Vec<Self>, ScanError> { pub fn load_from_iter<I: Iterator<Item = char>>(source: I) -> Result<Vec<Self>, ScanError> {
let mut parser = Parser::new(source); let mut parser = Parser::new(BufferedInput::new(source));
Self::load_from_parser(&mut parser) Self::load_from_parser(&mut parser)
} }
@ -56,9 +56,7 @@ impl MarkedYaml {
/// Returns `ScanError` when loading fails. /// Returns `ScanError` when loading fails.
/// ///
/// [`load_from_str`]: `Yaml::load_from_str` /// [`load_from_str`]: `Yaml::load_from_str`
pub fn load_from_parser<I: Iterator<Item = char>>( pub fn load_from_parser<I: Input>(parser: &mut Parser<I>) -> Result<Vec<Self>, ScanError> {
parser: &mut Parser<I>,
) -> Result<Vec<Self>, ScanError> {
let mut loader = YamlLoader::<Self>::default(); let mut loader = YamlLoader::<Self>::default();
parser.load(&mut loader, true)?; parser.load(&mut loader, true)?;
Ok(loader.into_documents()) Ok(loader.into_documents())
@ -83,7 +81,7 @@ impl std::hash::Hash for MarkedYaml {
impl From<YamlData<MarkedYaml>> for MarkedYaml { impl From<YamlData<MarkedYaml>> for MarkedYaml {
fn from(value: YamlData<MarkedYaml>) -> Self { fn from(value: YamlData<MarkedYaml>) -> Self {
Self { Self {
marker: Marker::default(), span: Span::default(),
data: value, data: value,
} }
} }
@ -92,7 +90,7 @@ impl From<YamlData<MarkedYaml>> for MarkedYaml {
impl LoadableYamlNode for MarkedYaml { impl LoadableYamlNode for MarkedYaml {
fn from_bare_yaml(yaml: Yaml) -> Self { fn from_bare_yaml(yaml: Yaml) -> Self {
Self { Self {
marker: Marker::default(), span: Span::default(),
data: match yaml { data: match yaml {
Yaml::Real(x) => YamlData::Real(x), Yaml::Real(x) => YamlData::Real(x),
Yaml::Integer(x) => YamlData::Integer(x), Yaml::Integer(x) => YamlData::Integer(x),
@ -138,15 +136,15 @@ impl LoadableYamlNode for MarkedYaml {
fn take(&mut self) -> Self { fn take(&mut self) -> Self {
let mut taken_out = MarkedYaml { let mut taken_out = MarkedYaml {
marker: Marker::default(), span: Span::default(),
data: YamlData::BadValue, data: YamlData::BadValue,
}; };
std::mem::swap(&mut taken_out, self); std::mem::swap(&mut taken_out, self);
taken_out taken_out
} }
fn with_marker(mut self, marker: Marker) -> Self { fn with_span(mut self, span: Span) -> Self {
self.marker = marker; self.span = span;
self self
} }
} }

View file

@ -3,7 +3,7 @@
use std::{collections::BTreeMap, sync::Arc}; use std::{collections::BTreeMap, sync::Arc};
use hashlink::LinkedHashMap; 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}; use crate::{Hash, Yaml};
@ -43,13 +43,13 @@ where
} }
} }
impl<Node> MarkedEventReceiver for YamlLoader<Node> impl<Node> SpannedEventReceiver for YamlLoader<Node>
where where
Node: LoadableYamlNode, Node: LoadableYamlNode,
{ {
fn on_event(&mut self, ev: Event, marker: Marker) { fn on_event(&mut self, ev: Event, span: Span) {
match ev { match ev {
Event::DocumentStart | Event::Nothing | Event::StreamStart | Event::StreamEnd => { Event::DocumentStart(_) | Event::Nothing | Event::StreamStart | Event::StreamEnd => {
// do nothing // do nothing
} }
Event::DocumentEnd => { Event::DocumentEnd => {
@ -57,14 +57,14 @@ where
// empty document // empty document
0 => self 0 => self
.docs .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), 1 => self.docs.push(self.doc_stack.pop().unwrap().0),
_ => unreachable!(), _ => unreachable!(),
} }
} }
Event::SequenceStart(aid, _) => { Event::SequenceStart(aid, _) => {
self.doc_stack.push(( 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, aid,
)); ));
} }
@ -74,7 +74,7 @@ where
} }
Event::MappingStart(aid, _) => { Event::MappingStart(aid, _) => {
self.doc_stack.push(( 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, aid,
)); ));
self.key_stack.push(Node::from_bare_yaml(Yaml::BadValue)); self.key_stack.push(Node::from_bare_yaml(Yaml::BadValue));
@ -122,14 +122,14 @@ where
// Datatype is not specified, or unrecognized // Datatype is not specified, or unrecognized
Yaml::from_str(&v) 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) => { Event::Alias(id) => {
let n = match self.anchor_map.get(&id) { let n = match self.anchor_map.get(&id) {
Some(v) => v.clone(), Some(v) => v.clone(),
None => Node::from_bare_yaml(Yaml::BadValue), 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). /// Provide the marker for the node (builder-style).
#[inline] #[inline]
#[must_use] #[must_use]
fn with_marker(self, _: Marker) -> Self { fn with_span(self, _: Span) -> Self {
self self
} }
} }

View file

@ -5,7 +5,7 @@
use std::{convert::TryFrom, ops::Index, ops::IndexMut}; use std::{convert::TryFrom, ops::Index, ops::IndexMut};
use hashlink::LinkedHashMap; use hashlink::LinkedHashMap;
use saphyr_parser::{Parser, ScanError}; use saphyr_parser::{BufferedInput, Input, Parser, ScanError};
use crate::{loader::parse_f64, YamlLoader}; use crate::{loader::parse_f64, YamlLoader};
@ -97,7 +97,7 @@ impl Yaml {
/// # Errors /// # Errors
/// Returns `ScanError` when loading fails. /// Returns `ScanError` when loading fails.
pub fn load_from_iter<I: Iterator<Item = char>>(source: I) -> Result<Vec<Yaml>, ScanError> { pub fn load_from_iter<I: Iterator<Item = char>>(source: I) -> Result<Vec<Yaml>, ScanError> {
let mut parser = Parser::new(source); let mut parser = Parser::new(BufferedInput::new(source));
Self::load_from_parser(&mut parser) Self::load_from_parser(&mut parser)
} }
@ -107,9 +107,7 @@ impl Yaml {
/// ///
/// # Errors /// # Errors
/// Returns `ScanError` when loading fails. /// Returns `ScanError` when loading fails.
pub fn load_from_parser<I: Iterator<Item = char>>( pub fn load_from_parser<I: Input>(parser: &mut Parser<I>) -> Result<Vec<Yaml>, ScanError> {
parser: &mut Parser<I>,
) -> Result<Vec<Yaml>, ScanError> {
let mut loader = YamlLoader::default(); let mut loader = YamlLoader::default();
parser.load(&mut loader, true)?; parser.load(&mut loader, true)?;
Ok(loader.into_documents()) Ok(loader.into_documents())