Change out-parameter to return values

This commit is contained in:
Simon Ask Ulsnes 2024-02-02 10:54:41 +01:00
parent 8a080a1ce7
commit 666034cf5c
6 changed files with 93 additions and 135 deletions

View file

@ -6,8 +6,7 @@ use crate::{
libc, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t, yaml_encoding_t,
yaml_event_t, yaml_mapping_style_t, yaml_mark_t, yaml_node_pair_t, yaml_node_t,
yaml_parser_state_t, yaml_parser_t, yaml_scalar_style_t, yaml_sequence_style_t,
yaml_tag_directive_t, yaml_token_t, yaml_version_directive_t, YAML_ANY_ENCODING,
YAML_UTF8_ENCODING,
yaml_tag_directive_t, yaml_version_directive_t, YAML_ANY_ENCODING, YAML_UTF8_ENCODING,
};
use core::ptr;
use std::collections::VecDeque;
@ -53,9 +52,7 @@ pub fn yaml_parser_new<'r>() -> yaml_parser_t<'r> {
pub fn yaml_parser_delete(parser: &mut yaml_parser_t) {
parser.raw_buffer.clear();
parser.buffer.clear();
for mut token in parser.tokens.drain(..) {
yaml_token_delete(&mut token);
}
parser.tokens.clear();
parser.indents.clear();
parser.simple_keys.clear();
parser.states.clear();
@ -129,9 +126,7 @@ pub fn yaml_emitter_delete(emitter: &mut yaml_emitter_t) {
emitter.buffer.clear();
emitter.raw_buffer.clear();
emitter.states.clear();
while let Some(mut event) = emitter.events.pop_front() {
yaml_event_delete(&mut event);
}
emitter.events.clear();
emitter.indents.clear();
emitter.tag_directives.clear();
*emitter = yaml_emitter_t::default();
@ -198,68 +193,62 @@ pub fn yaml_emitter_set_break(emitter: &mut yaml_emitter_t, line_break: yaml_bre
emitter.line_break = line_break;
}
/// Free any memory allocated for a token object.
pub fn yaml_token_delete(token: &mut yaml_token_t) {
*token = yaml_token_t::default();
}
/// Create the STREAM-START event.
pub fn yaml_stream_start_event_initialize(event: &mut yaml_event_t, encoding: yaml_encoding_t) {
*event = yaml_event_t {
pub fn yaml_stream_start_event_new(encoding: yaml_encoding_t) -> yaml_event_t {
yaml_event_t {
data: YamlEventData::StreamStart { encoding },
..Default::default()
};
}
}
/// Create the STREAM-END event.
pub fn yaml_stream_end_event_initialize(event: &mut yaml_event_t) {
*event = yaml_event_t {
pub fn yaml_stream_end_event_new() -> yaml_event_t {
yaml_event_t {
data: YamlEventData::StreamEnd,
..Default::default()
};
}
}
/// Create the DOCUMENT-START event.
///
/// The `implicit` argument is considered as a stylistic parameter and may be
/// ignored by the emitter.
pub fn yaml_document_start_event_initialize(
event: &mut yaml_event_t,
pub fn yaml_document_start_event_new(
version_directive: Option<yaml_version_directive_t>,
tag_directives_in: &[yaml_tag_directive_t],
implicit: bool,
) {
) -> yaml_event_t {
let tag_directives = Vec::from_iter(tag_directives_in.iter().cloned());
*event = yaml_event_t {
yaml_event_t {
data: YamlEventData::DocumentStart {
version_directive,
tag_directives,
implicit,
},
..Default::default()
};
}
}
/// Create the DOCUMENT-END event.
///
/// The `implicit` argument is considered as a stylistic parameter and may be
/// ignored by the emitter.
pub fn yaml_document_end_event_initialize(event: &mut yaml_event_t, implicit: bool) {
*event = yaml_event_t {
pub fn yaml_document_end_event_new(implicit: bool) -> yaml_event_t {
yaml_event_t {
data: YamlEventData::DocumentEnd { implicit },
..Default::default()
};
}
}
/// Create an ALIAS event.
pub fn yaml_alias_event_initialize(event: &mut yaml_event_t, anchor: &str) {
*event = yaml_event_t {
pub fn yaml_alias_event_new(anchor: &str) -> yaml_event_t {
yaml_event_t {
data: YamlEventData::Alias {
anchor: String::from(anchor),
},
..Default::default()
};
}
}
/// Create a SCALAR event.
@ -269,15 +258,14 @@ pub fn yaml_alias_event_initialize(event: &mut yaml_event_t, anchor: &str) {
/// Either the `tag` attribute or one of the `plain_implicit` and
/// `quoted_implicit` flags must be set.
///
pub fn yaml_scalar_event_initialize(
event: &mut yaml_event_t,
pub fn yaml_scalar_event_new(
anchor: Option<&str>,
tag: Option<&str>,
value: &str,
plain_implicit: bool,
quoted_implicit: bool,
style: yaml_scalar_style_t,
) {
) -> yaml_event_t {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
@ -293,7 +281,7 @@ pub fn yaml_scalar_event_initialize(
tag_copy = Some(String::from(tag));
}
*event = yaml_event_t {
yaml_event_t {
data: YamlEventData::Scalar {
anchor: anchor_copy,
tag: tag_copy,
@ -304,7 +292,7 @@ pub fn yaml_scalar_event_initialize(
},
start_mark: mark,
end_mark: mark,
};
}
}
/// Create a SEQUENCE-START event.
@ -312,13 +300,12 @@ pub fn yaml_scalar_event_initialize(
/// The `style` argument may be ignored by the emitter.
///
/// Either the `tag` attribute or the `implicit` flag must be set.
pub fn yaml_sequence_start_event_initialize(
event: &mut yaml_event_t,
pub fn yaml_sequence_start_event_new(
anchor: Option<&str>,
tag: Option<&str>,
implicit: bool,
style: yaml_sequence_style_t,
) {
) -> yaml_event_t {
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
@ -329,7 +316,7 @@ pub fn yaml_sequence_start_event_initialize(
tag_copy = Some(String::from(tag));
}
*event = yaml_event_t {
yaml_event_t {
data: YamlEventData::SequenceStart {
anchor: anchor_copy,
tag: tag_copy,
@ -337,15 +324,15 @@ pub fn yaml_sequence_start_event_initialize(
style,
},
..Default::default()
};
}
}
/// Create a SEQUENCE-END event.
pub fn yaml_sequence_end_event_initialize(event: &mut yaml_event_t) {
*event = yaml_event_t {
pub fn yaml_sequence_end_event_new() -> yaml_event_t {
yaml_event_t {
data: YamlEventData::SequenceEnd,
..Default::default()
};
}
}
/// Create a MAPPING-START event.
@ -353,13 +340,12 @@ pub fn yaml_sequence_end_event_initialize(event: &mut yaml_event_t) {
/// The `style` argument may be ignored by the emitter.
///
/// Either the `tag` attribute or the `implicit` flag must be set.
pub fn yaml_mapping_start_event_initialize(
event: &mut yaml_event_t,
pub fn yaml_mapping_start_event_new(
anchor: Option<&str>,
tag: Option<&str>,
implicit: bool,
style: yaml_mapping_style_t,
) {
) -> yaml_event_t {
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
@ -371,7 +357,7 @@ pub fn yaml_mapping_start_event_initialize(
tag_copy = Some(String::from(tag));
}
*event = yaml_event_t {
yaml_event_t {
data: YamlEventData::MappingStart {
anchor: anchor_copy,
tag: tag_copy,
@ -379,41 +365,36 @@ pub fn yaml_mapping_start_event_initialize(
style,
},
..Default::default()
};
}
}
/// Create a MAPPING-END event.
pub fn yaml_mapping_end_event_initialize(event: &mut yaml_event_t) {
*event = yaml_event_t {
pub fn yaml_mapping_end_event_new() -> yaml_event_t {
yaml_event_t {
data: YamlEventData::MappingEnd,
..Default::default()
};
}
/// Free any memory allocated for an event object.
pub fn yaml_event_delete(event: &mut yaml_event_t) {
*event = Default::default();
}
}
/// Create a YAML document.
pub fn yaml_document_initialize(
document: &mut yaml_document_t,
pub fn yaml_document_new(
version_directive: Option<yaml_version_directive_t>,
tag_directives_in: &[yaml_tag_directive_t],
start_implicit: bool,
end_implicit: bool,
) {
) -> yaml_document_t {
let nodes = Vec::with_capacity(16);
let tag_directives = Vec::from_iter(tag_directives_in.iter().cloned());
*document = yaml_document_t {
yaml_document_t {
nodes,
version_directive,
tag_directives,
start_implicit,
end_implicit,
..Default::default()
};
start_mark: yaml_mark_t::default(),
end_mark: yaml_mark_t::default(),
}
}
/// Delete a YAML document and all its nodes.

View file

@ -15,13 +15,12 @@
)]
use libyaml_safer::{
yaml_alias_event_initialize, yaml_document_end_event_initialize,
yaml_document_start_event_initialize, yaml_emitter_delete, yaml_emitter_emit, yaml_emitter_new,
yaml_emitter_set_canonical, yaml_emitter_set_output, yaml_emitter_set_unicode, yaml_event_t,
yaml_mapping_end_event_initialize, yaml_mapping_start_event_initialize,
yaml_scalar_event_initialize, yaml_scalar_style_t, yaml_sequence_end_event_initialize,
yaml_sequence_start_event_initialize, yaml_stream_end_event_initialize,
yaml_stream_start_event_initialize, YAML_ANY_SCALAR_STYLE, YAML_BLOCK_MAPPING_STYLE,
yaml_alias_event_new, yaml_document_end_event_new, yaml_document_start_event_new,
yaml_emitter_delete, yaml_emitter_emit, yaml_emitter_new, yaml_emitter_set_canonical,
yaml_emitter_set_output, yaml_emitter_set_unicode, yaml_mapping_end_event_new,
yaml_mapping_start_event_new, yaml_scalar_event_new, yaml_scalar_style_t,
yaml_sequence_end_event_new, yaml_sequence_start_event_new, yaml_stream_end_event_new,
yaml_stream_start_event_new, YAML_ANY_SCALAR_STYLE, YAML_BLOCK_MAPPING_STYLE,
YAML_BLOCK_SEQUENCE_STYLE, YAML_DOUBLE_QUOTED_SCALAR_STYLE, YAML_FOLDED_SCALAR_STYLE,
YAML_LITERAL_SCALAR_STYLE, YAML_PLAIN_SCALAR_STYLE, YAML_SINGLE_QUOTED_SCALAR_STYLE,
YAML_UTF8_ENCODING,
@ -47,8 +46,6 @@ pub(crate) fn test_main(
let mut value_buffer = String::with_capacity(128);
let result = loop {
let mut event = yaml_event_t::default();
line_buffer.clear();
let n = buf.read_line(&mut line_buffer)?;
if n == 0 {
@ -56,42 +53,39 @@ pub(crate) fn test_main(
}
let line = line_buffer.strip_suffix('\n').unwrap_or(&line_buffer);
if line.starts_with("+STR") {
yaml_stream_start_event_initialize(&mut event, YAML_UTF8_ENCODING)
let event = if line.starts_with("+STR") {
yaml_stream_start_event_new(YAML_UTF8_ENCODING)
} else if line.starts_with("-STR") {
yaml_stream_end_event_initialize(&mut event)
yaml_stream_end_event_new()
} else if line.starts_with("+DOC") {
let implicit = !line[4..].starts_with(" ---");
yaml_document_start_event_initialize(&mut event, None, &[], implicit)
yaml_document_start_event_new(None, &[], implicit)
} else if line.starts_with("-DOC") {
let implicit = !line[4..].starts_with(" ...");
yaml_document_end_event_initialize(&mut event, implicit)
yaml_document_end_event_new(implicit)
} else if line.starts_with("+MAP") {
yaml_mapping_start_event_initialize(
&mut event,
yaml_mapping_start_event_new(
get_anchor('&', line),
get_tag(line),
false,
YAML_BLOCK_MAPPING_STYLE,
)
} else if line.starts_with("-MAP") {
yaml_mapping_end_event_initialize(&mut event)
yaml_mapping_end_event_new()
} else if line.starts_with("+SEQ") {
yaml_sequence_start_event_initialize(
&mut event,
yaml_sequence_start_event_new(
get_anchor('&', line),
get_tag(line),
false,
YAML_BLOCK_SEQUENCE_STYLE,
)
} else if line.starts_with("-SEQ") {
yaml_sequence_end_event_initialize(&mut event)
yaml_sequence_end_event_new()
} else if line.starts_with("=VAL") {
let mut style = YAML_ANY_SCALAR_STYLE;
let value = get_value(line, &mut value_buffer, &mut style);
let implicit = get_tag(line).is_none();
yaml_scalar_event_initialize(
&mut event,
yaml_scalar_event_new(
get_anchor('&', line),
get_tag(line),
value,
@ -100,7 +94,7 @@ pub(crate) fn test_main(
style,
)
} else if line.starts_with("=ALI") {
yaml_alias_event_initialize(&mut event, get_anchor('*', line).expect("no alias name"))
yaml_alias_event_new(get_anchor('*', line).expect("no alias name"))
} else {
break Err(format!("Unknown event: '{line}'").into());
};

View file

@ -7,7 +7,7 @@ use crate::macros::{
use crate::ops::ForceMul as _;
use crate::yaml::YamlEventData;
use crate::{
libc, yaml_emitter_flush, yaml_emitter_t, yaml_event_delete, yaml_event_t, yaml_scalar_style_t,
libc, yaml_emitter_flush, yaml_emitter_t, yaml_event_t, yaml_scalar_style_t,
yaml_tag_directive_t, yaml_version_directive_t, EmitterError, WriterError, YAML_ANY_BREAK,
YAML_ANY_ENCODING, YAML_ANY_SCALAR_STYLE, YAML_CRLN_BREAK, YAML_CR_BREAK,
YAML_DOUBLE_QUOTED_SCALAR_STYLE, YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE,
@ -133,12 +133,11 @@ pub fn yaml_emitter_emit(
event: yaml_event_t,
) -> Result<(), EmitterError> {
emitter.events.push_back(event);
while let Some(mut event) = yaml_emitter_needs_mode_events(emitter) {
while let Some(event) = yaml_emitter_needs_mode_events(emitter) {
let tag_directives = core::mem::take(&mut emitter.tag_directives);
let mut analysis = yaml_emitter_analyze_event(emitter, &event, &tag_directives)?;
yaml_emitter_state_machine(emitter, &event, &mut analysis)?;
yaml_event_delete(&mut event);
// The DOCUMENT-START event populates the tag directives, and this
// happens only once, so don't swap out the tags in that case.

View file

@ -102,19 +102,17 @@ mod writer;
mod yaml;
pub use crate::api::{
yaml_alias_event_initialize, yaml_document_add_mapping, yaml_document_add_scalar,
yaml_alias_event_new, yaml_document_add_mapping, yaml_document_add_scalar,
yaml_document_add_sequence, yaml_document_append_mapping_pair,
yaml_document_append_sequence_item, yaml_document_delete, yaml_document_end_event_initialize,
yaml_document_get_node, yaml_document_get_root_node, yaml_document_initialize,
yaml_document_start_event_initialize, yaml_emitter_delete, yaml_emitter_new,
yaml_emitter_set_break, yaml_emitter_set_canonical, yaml_emitter_set_encoding,
yaml_emitter_set_indent, yaml_emitter_set_output, yaml_emitter_set_output_string,
yaml_emitter_set_unicode, yaml_emitter_set_width, yaml_event_delete,
yaml_mapping_end_event_initialize, yaml_mapping_start_event_initialize, yaml_parser_delete,
yaml_parser_new, yaml_parser_set_encoding, yaml_parser_set_input, yaml_parser_set_input_string,
yaml_scalar_event_initialize, yaml_sequence_end_event_initialize,
yaml_sequence_start_event_initialize, yaml_stream_end_event_initialize,
yaml_stream_start_event_initialize, yaml_token_delete,
yaml_document_append_sequence_item, yaml_document_delete, yaml_document_end_event_new,
yaml_document_get_node, yaml_document_get_root_node, yaml_document_new,
yaml_document_start_event_new, yaml_emitter_delete, yaml_emitter_new, yaml_emitter_set_break,
yaml_emitter_set_canonical, yaml_emitter_set_encoding, yaml_emitter_set_indent,
yaml_emitter_set_output, yaml_emitter_set_output_string, yaml_emitter_set_unicode,
yaml_emitter_set_width, yaml_mapping_end_event_new, yaml_mapping_start_event_new,
yaml_parser_delete, yaml_parser_new, yaml_parser_set_encoding, yaml_parser_set_input,
yaml_parser_set_input_string, yaml_scalar_event_new, yaml_sequence_end_event_new,
yaml_sequence_start_event_new, yaml_stream_end_event_new, yaml_stream_start_event_new,
};
pub use crate::dumper::{yaml_emitter_close, yaml_emitter_dump, yaml_emitter_open};
pub use crate::emitter::yaml_emitter_emit;
@ -158,8 +156,7 @@ tie-fighter: '|\-*-/|'
"#;
let mut read_in = SANITY_INPUT.as_bytes();
yaml_parser_set_input_string(&mut parser, &mut read_in);
let mut doc = yaml_document_t::default();
yaml_parser_load(&mut parser, &mut doc).unwrap();
let _doc = yaml_parser_load(&mut parser).unwrap();
// let mut doc = doc.assume_init();
// let mut emitter = core::mem::MaybeUninit::uninit();
@ -193,8 +190,7 @@ foo: bar
let mut parser = yaml_parser_new();
let mut input = TEST_CASE_QF4Y.as_bytes();
yaml_parser_set_input_string(&mut parser, &mut input);
let mut doc = yaml_document_t::default();
yaml_parser_load(&mut parser, &mut doc).unwrap();
let _doc = yaml_parser_load(&mut parser).unwrap();
}
// #[test]
@ -255,13 +251,11 @@ foo: bar
let mut output = Vec::new();
yaml_emitter_set_output_string(&mut emitter, &mut output);
let mut event = yaml_event_t::default();
yaml_stream_start_event_initialize(&mut event, YAML_UTF8_ENCODING);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_document_start_event_initialize(&mut event, None, &[], true);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_scalar_event_initialize(
&mut event,
let event = yaml_stream_start_event_new(YAML_UTF8_ENCODING);
yaml_emitter_emit(&mut emitter, event).unwrap();
let event = yaml_document_start_event_new(None, &[], true);
yaml_emitter_emit(&mut emitter, event).unwrap();
let event = yaml_scalar_event_new(
None,
None,
"1st non-empty\n2nd non-empty 3rd non-empty",
@ -269,11 +263,11 @@ foo: bar
true,
YAML_PLAIN_SCALAR_STYLE,
);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_document_end_event_initialize(&mut event, true);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_stream_end_event_initialize(&mut event);
yaml_emitter_emit(&mut emitter, core::mem::take(&mut event)).unwrap();
yaml_emitter_emit(&mut emitter, event).unwrap();
let event = yaml_document_end_event_new(true);
yaml_emitter_emit(&mut emitter, event).unwrap();
let event = yaml_stream_end_event_new();
yaml_emitter_emit(&mut emitter, event).unwrap();
assert_eq!(
core::str::from_utf8(&output),

View file

@ -3,7 +3,7 @@ use alloc::{vec, vec::Vec};
use crate::yaml::{YamlEventData, YamlNodeData};
use crate::{
libc, yaml_alias_data_t, yaml_document_delete, yaml_document_t, yaml_event_t, yaml_mark_t,
libc, yaml_alias_data_t, yaml_document_new, yaml_document_t, yaml_event_t, yaml_mark_t,
yaml_node_pair_t, yaml_node_t, yaml_parser_parse, yaml_parser_t, ComposerError,
};
@ -15,17 +15,11 @@ use crate::{
/// If the produced document has no root node, it means that the document end
/// has been reached.
///
/// An application is responsible for freeing any data associated with the
/// produced document object using the yaml_document_delete() function.
///
/// An application must not alternate the calls of yaml_parser_load() with the
/// calls of yaml_parser_scan() or yaml_parser_parse(). Doing this will break
/// the parser.
pub fn yaml_parser_load(
parser: &mut yaml_parser_t,
document: &mut yaml_document_t,
) -> Result<(), ComposerError> {
*document = yaml_document_t::default();
pub fn yaml_parser_load(parser: &mut yaml_parser_t) -> Result<yaml_document_t, ComposerError> {
let mut document = yaml_document_new(None, &[], false, false);
document.nodes.reserve(16);
if !parser.stream_start_produced {
@ -37,25 +31,24 @@ pub fn yaml_parser_load(
Ok(_) => panic!("expected stream start"),
Err(err) => {
yaml_parser_delete_aliases(parser);
yaml_document_delete(document);
return Err(err.into());
}
}
}
if parser.stream_end_produced {
return Ok(());
return Ok(document);
}
let err: ComposerError;
match yaml_parser_parse(parser) {
Ok(event) => {
if let YamlEventData::StreamEnd = &event.data {
return Ok(());
return Ok(document);
}
parser.aliases.reserve(16);
match yaml_parser_load_document(parser, event, document) {
match yaml_parser_load_document(parser, event, &mut document) {
Ok(()) => {
yaml_parser_delete_aliases(parser);
return Ok(());
return Ok(document);
}
Err(e) => err = e,
}
@ -63,7 +56,6 @@ pub fn yaml_parser_load(
Err(e) => err = e.into(),
}
yaml_parser_delete_aliases(parser);
yaml_document_delete(document);
Err(err)
}
@ -271,7 +263,6 @@ fn yaml_parser_load_scalar(
unreachable!()
};
if tag.is_none() || tag.as_deref() == Some("!") {
tag = Some(String::from("tag:yaml.org,2002:str"));
}
@ -304,7 +295,7 @@ fn yaml_parser_load_sequence(
};
let mut items = Vec::with_capacity(16);
if tag.is_none() || tag.as_deref() == Some("!") {
tag = Some(String::from("tag:yaml.org,2002:seq"));
}
@ -361,7 +352,7 @@ fn yaml_parser_load_mapping(
};
let mut pairs = Vec::with_capacity(16);
if tag.is_none() || tag.as_deref() == Some("!") {
tag = Some(String::from("tag:yaml.org,2002:map"));
}

View file

@ -509,7 +509,6 @@ pub struct yaml_node_pair_t {
}
/// The document structure.
#[derive(Default)]
#[repr(C)]
#[non_exhaustive]
pub struct yaml_document_t {