Skip to content

Commit

Permalink
Box errors, remove thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
simonask committed Feb 8, 2024
1 parent 8f6ce4b commit 4f9ef32
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 399 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ unsafe-libyaml-test-suite = { path = "tests/data" }
[lib]
doc-scrape-examples = false

[dependencies]
thiserror = "1.0"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
rustdoc-args = ["--generate-link-to-definition"]
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ errors may be reported with reduced fidelity compared with libyaml (e.g., error
messages may look slightly different), but the same inputs should generate the
same general errors.

This library introduces no new dependencies except for
[`thiserror`](https://docs.rs/thiserror) for convenience. This dependency may go
away in the future.

### Compatibility and interoperability

While this library matches the behavior of libyaml, it is not intended as a
Expand Down
93 changes: 30 additions & 63 deletions src/document.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
AliasData, Anchors, ComposerError, Emitter, EmitterError, Event, EventData, MappingStyle, Mark,
Parser, ScalarStyle, SequenceStyle, TagDirective, VersionDirective, DEFAULT_MAPPING_TAG,
AliasData, Anchors, Emitter, Error, Event, EventData, MappingStyle, Mark, Parser, Result,
ScalarStyle, SequenceStyle, TagDirective, VersionDirective, DEFAULT_MAPPING_TAG,
DEFAULT_SCALAR_TAG, DEFAULT_SEQUENCE_TAG,
};

Expand Down Expand Up @@ -249,7 +249,7 @@ impl Document {
///
/// An application must not alternate the calls of [`Document::load()`] with
/// the calls of [`Parser::parse()`]. Doing this will break the parser.
pub fn load(parser: &mut Parser) -> Result<Document, ComposerError> {
pub fn load(parser: &mut Parser) -> Result<Document> {
let mut document = Document::new(None, &[], false, false);
document.nodes.reserve(16);

Expand All @@ -262,14 +262,14 @@ impl Document {
Ok(_) => panic!("expected stream start"),
Err(err) => {
parser.delete_aliases();
return Err(err.into());
return Err(err);
}
}
}
if parser.scanner.stream_end_produced {
return Ok(document);
}
let err: ComposerError;
let err: Error;
match parser.parse() {
Ok(event) => {
if let EventData::StreamEnd = &event.data {
Expand All @@ -284,37 +284,13 @@ impl Document {
Err(e) => err = e,
}
}
Err(e) => err = e.into(),
Err(e) => err = e,
}
parser.delete_aliases();
Err(err)
}

fn set_composer_error<T>(
problem: &'static str,
problem_mark: Mark,
) -> Result<T, ComposerError> {
Err(ComposerError::Problem {
problem,
mark: problem_mark,
})
}

fn set_composer_error_context<T>(
context: &'static str,
context_mark: Mark,
problem: &'static str,
problem_mark: Mark,
) -> Result<T, ComposerError> {
Err(ComposerError::ProblemWithContext {
context,
context_mark,
problem,
mark: problem_mark,
})
}

fn load_document(&mut self, parser: &mut Parser, event: Event) -> Result<(), ComposerError> {
fn load_document(&mut self, parser: &mut Parser, event: Event) -> Result<()> {
let mut ctx = vec![];
if let EventData::DocumentStart {
version_directive,
Expand All @@ -338,7 +314,7 @@ impl Document {
}
}

fn load_nodes(&mut self, parser: &mut Parser, ctx: &mut Vec<i32>) -> Result<(), ComposerError> {
fn load_nodes(&mut self, parser: &mut Parser, ctx: &mut Vec<i32>) -> Result<()> {
let end_implicit;
let end_mark;

Expand Down Expand Up @@ -383,7 +359,7 @@ impl Document {
parser: &mut Parser,
index: i32,
anchor: Option<String>,
) -> Result<(), ComposerError> {
) -> Result<()> {
let Some(anchor) = anchor else {
return Ok(());
};
Expand All @@ -394,19 +370,19 @@ impl Document {
};
for alias_data in &parser.aliases {
if alias_data.anchor == data.anchor {
return Self::set_composer_error_context(
return Err(Error::composer(
"found duplicate anchor; first occurrence",
alias_data.mark,
"second occurrence",
data.mark,
);
));
}
}
parser.aliases.push(data);
Ok(())
}

fn load_node_add(&mut self, ctx: &[i32], index: i32) -> Result<(), ComposerError> {
fn load_node_add(&mut self, ctx: &[i32], index: i32) -> Result<()> {
if ctx.is_empty() {
return Ok(());
}
Expand Down Expand Up @@ -439,12 +415,7 @@ impl Document {
Ok(())
}

fn load_alias(
&mut self,
parser: &mut Parser,
event: Event,
ctx: &[i32],
) -> Result<(), ComposerError> {
fn load_alias(&mut self, parser: &mut Parser, event: Event, ctx: &[i32]) -> Result<()> {
let EventData::Alias { anchor } = &event.data else {
unreachable!()
};
Expand All @@ -455,15 +426,15 @@ impl Document {
}
}

Self::set_composer_error("found undefined alias", event.start_mark)
Err(Error::composer(
"",
Mark::default(),
"found undefined alias",
event.start_mark,
))
}

fn load_scalar(
&mut self,
parser: &mut Parser,
event: Event,
ctx: &[i32],
) -> Result<(), ComposerError> {
fn load_scalar(&mut self, parser: &mut Parser, event: Event, ctx: &[i32]) -> Result<()> {
let EventData::Scalar {
mut tag,
value,
Expand Down Expand Up @@ -495,7 +466,7 @@ impl Document {
parser: &mut Parser,
event: Event,
ctx: &mut Vec<i32>,
) -> Result<(), ComposerError> {
) -> Result<()> {
let EventData::SequenceStart {
anchor,
mut tag,
Expand Down Expand Up @@ -530,7 +501,7 @@ impl Document {
Ok(())
}

fn load_sequence_end(&mut self, event: Event, ctx: &mut Vec<i32>) -> Result<(), ComposerError> {
fn load_sequence_end(&mut self, event: Event, ctx: &mut Vec<i32>) -> Result<()> {
assert!(!ctx.is_empty());
let index: i32 = *ctx.last().unwrap();
assert!(matches!(
Expand All @@ -547,7 +518,7 @@ impl Document {
parser: &mut Parser,
event: Event,
ctx: &mut Vec<i32>,
) -> Result<(), ComposerError> {
) -> Result<()> {
let EventData::MappingStart {
anchor,
mut tag,
Expand Down Expand Up @@ -580,7 +551,7 @@ impl Document {
Ok(())
}

fn load_mapping_end(&mut self, event: Event, ctx: &mut Vec<i32>) -> Result<(), ComposerError> {
fn load_mapping_end(&mut self, event: Event, ctx: &mut Vec<i32>) -> Result<()> {
assert!(!ctx.is_empty());
let index: i32 = *ctx.last().unwrap();
assert!(matches!(
Expand All @@ -596,7 +567,7 @@ impl Document {
///
/// The document object may be generated using the [`Document::load()`]
/// function or the [`Document::new()`] function.
pub fn dump(mut self, emitter: &mut Emitter) -> Result<(), EmitterError> {
pub fn dump(mut self, emitter: &mut Emitter) -> Result<()> {
if !emitter.opened {
if let Err(err) = emitter.open() {
emitter.reset_anchors();
Expand Down Expand Up @@ -651,7 +622,7 @@ impl Document {
}
}

fn dump_node(&mut self, emitter: &mut Emitter, index: i32) -> Result<(), EmitterError> {
fn dump_node(&mut self, emitter: &mut Emitter, index: i32) -> Result<()> {
let node = &mut self.nodes[index as usize - 1];
let anchor_id: i32 = emitter.anchors[index as usize - 1].anchor;
let mut anchor: Option<String> = None;
Expand All @@ -672,16 +643,12 @@ impl Document {
}
}

fn dump_alias(emitter: &mut Emitter, anchor: String) -> Result<(), EmitterError> {
fn dump_alias(emitter: &mut Emitter, anchor: String) -> Result<()> {
let event = Event::new(EventData::Alias { anchor });
emitter.emit(event)
}

fn dump_scalar(
emitter: &mut Emitter,
node: Node,
anchor: Option<String>,
) -> Result<(), EmitterError> {
fn dump_scalar(emitter: &mut Emitter, node: Node, anchor: Option<String>) -> Result<()> {
let plain_implicit = node.tag.as_deref() == Some(DEFAULT_SCALAR_TAG);
let quoted_implicit = node.tag.as_deref() == Some(DEFAULT_SCALAR_TAG); // TODO: Why compare twice?! (even the C code does this)

Expand All @@ -704,7 +671,7 @@ impl Document {
emitter: &mut Emitter,
node: Node,
anchor: Option<String>,
) -> Result<(), EmitterError> {
) -> Result<()> {
let implicit = node.tag.as_deref() == Some(DEFAULT_SEQUENCE_TAG);

let NodeData::Sequence { items, style } = node.data else {
Expand All @@ -730,7 +697,7 @@ impl Document {
emitter: &mut Emitter,
node: Node,
anchor: Option<String>,
) -> Result<(), EmitterError> {
) -> Result<()> {
let implicit = node.tag.as_deref() == Some(DEFAULT_MAPPING_TAG);

let NodeData::Mapping { pairs, style } = node.data else {
Expand Down
Loading

0 comments on commit 4f9ef32

Please sign in to comment.