Rustify enums

This commit is contained in:
Simon Ask Ulsnes 2024-02-03 09:43:57 +01:00
parent 4f8f997d8f
commit b394312eda
11 changed files with 256 additions and 324 deletions

View file

@ -5,8 +5,7 @@ use crate::yaml::{EventData, NodeData};
use crate::{
Break, Document, Emitter, EmitterState, Encoding, Event, MappingStyle, Mark, Node, NodePair,
Parser, ParserState, ScalarStyle, SequenceStyle, TagDirective, VersionDirective,
YAML_ANY_ENCODING, YAML_DEFAULT_MAPPING_TAG, YAML_DEFAULT_SCALAR_TAG,
YAML_DEFAULT_SEQUENCE_TAG, YAML_UTF8_ENCODING,
YAML_DEFAULT_MAPPING_TAG, YAML_DEFAULT_SCALAR_TAG, YAML_DEFAULT_SEQUENCE_TAG,
};
use std::collections::VecDeque;
@ -24,7 +23,7 @@ pub fn yaml_parser_new<'r>() -> Parser<'r> {
eof: false,
buffer: VecDeque::with_capacity(INPUT_BUFFER_SIZE),
unread: 0,
encoding: YAML_ANY_ENCODING,
encoding: Encoding::Any,
offset: 0,
mark: Mark::default(),
stream_start_produced: false,
@ -64,7 +63,7 @@ pub fn yaml_parser_set_input<'r>(parser: &mut Parser<'r>, input: &'r mut dyn std
/// Set the source encoding.
pub fn yaml_parser_set_encoding(parser: &mut Parser, encoding: Encoding) {
assert!(parser.encoding == YAML_ANY_ENCODING);
assert!(parser.encoding == Encoding::Any);
parser.encoding = encoding;
}
@ -74,7 +73,7 @@ pub fn yaml_emitter_new<'w>() -> Emitter<'w> {
write_handler: None,
buffer: String::with_capacity(OUTPUT_BUFFER_SIZE),
raw_buffer: Vec::with_capacity(OUTPUT_BUFFER_SIZE),
encoding: YAML_ANY_ENCODING,
encoding: Encoding::Any,
canonical: false,
best_indent: 0,
best_width: 0,
@ -113,9 +112,9 @@ pub fn yaml_emitter_reset(emitter: &mut Emitter) {
/// The emitter will write the output characters to the `output` buffer.
pub fn yaml_emitter_set_output_string<'w>(emitter: &mut Emitter<'w>, output: &'w mut Vec<u8>) {
assert!(emitter.write_handler.is_none());
if emitter.encoding == YAML_ANY_ENCODING {
yaml_emitter_set_encoding(emitter, YAML_UTF8_ENCODING);
} else if emitter.encoding != YAML_UTF8_ENCODING {
if emitter.encoding == Encoding::Any {
yaml_emitter_set_encoding(emitter, Encoding::Utf8);
} else if emitter.encoding != Encoding::Utf8 {
panic!("cannot output UTF-16 to String")
}
output.clear();
@ -130,7 +129,7 @@ pub fn yaml_emitter_set_output<'w>(emitter: &mut Emitter<'w>, handler: &'w mut d
/// Set the output encoding.
pub fn yaml_emitter_set_encoding(emitter: &mut Emitter, encoding: Encoding) {
assert_eq!(emitter.encoding, YAML_ANY_ENCODING);
assert_eq!(emitter.encoding, Encoding::Any);
emitter.encoding = encoding;
}

View file

@ -20,9 +20,7 @@ use libyaml_safer::{
yaml_emitter_set_output, yaml_emitter_set_unicode, yaml_mapping_end_event_new,
yaml_mapping_start_event_new, yaml_scalar_event_new, yaml_sequence_end_event_new,
yaml_sequence_start_event_new, yaml_stream_end_event_new, yaml_stream_start_event_new,
ScalarStyle, 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,
Encoding, MappingStyle, ScalarStyle, SequenceStyle,
};
use std::env;
use std::error::Error;
@ -53,7 +51,7 @@ pub(crate) fn test_main(
let line = line_buffer.strip_suffix('\n').unwrap_or(&line_buffer);
let event = if line.starts_with("+STR") {
yaml_stream_start_event_new(YAML_UTF8_ENCODING)
yaml_stream_start_event_new(Encoding::Utf8)
} else if line.starts_with("-STR") {
yaml_stream_end_event_new()
} else if line.starts_with("+DOC") {
@ -67,7 +65,7 @@ pub(crate) fn test_main(
get_anchor('&', line),
get_tag(line),
false,
YAML_BLOCK_MAPPING_STYLE,
MappingStyle::Block,
)
} else if line.starts_with("-MAP") {
yaml_mapping_end_event_new()
@ -76,12 +74,12 @@ pub(crate) fn test_main(
get_anchor('&', line),
get_tag(line),
false,
YAML_BLOCK_SEQUENCE_STYLE,
SequenceStyle::Block,
)
} else if line.starts_with("-SEQ") {
yaml_sequence_end_event_new()
} else if line.starts_with("=VAL") {
let mut style = YAML_ANY_SCALAR_STYLE;
let mut style = ScalarStyle::Any;
let value = get_value(line, &mut value_buffer, &mut style);
let implicit = get_tag(line).is_none();
yaml_scalar_event_new(
@ -132,11 +130,11 @@ fn get_value<'a>(line: &str, buffer: &'a mut String, style: &mut ScalarStyle) ->
};
*style = match tail.chars().next().expect("string should not be empty") {
':' => YAML_PLAIN_SCALAR_STYLE,
'\'' => YAML_SINGLE_QUOTED_SCALAR_STYLE,
'"' => YAML_DOUBLE_QUOTED_SCALAR_STYLE,
'|' => YAML_LITERAL_SCALAR_STYLE,
'>' => YAML_FOLDED_SCALAR_STYLE,
':' => ScalarStyle::Plain,
'\'' => ScalarStyle::SingleQuoted,
'"' => ScalarStyle::DoubleQuoted,
'|' => ScalarStyle::Literal,
'>' => ScalarStyle::Folded,
_ => {
// This was an anchor, move to the next space.
remainder = tail;

View file

@ -13,8 +13,7 @@
use libyaml_safer::{
yaml_parser_new, yaml_parser_parse, yaml_parser_reset, yaml_parser_set_input, EventData,
YAML_DOUBLE_QUOTED_SCALAR_STYLE, YAML_FOLDED_SCALAR_STYLE, YAML_LITERAL_SCALAR_STYLE,
YAML_PLAIN_SCALAR_STYLE, YAML_SINGLE_QUOTED_SCALAR_STYLE,
ScalarStyle,
};
use std::env;
use std::error::Error;
@ -87,11 +86,11 @@ pub(crate) fn test_main(
_ = write!(stdout, " <{tag}>");
}
_ = stdout.write_all(match *style {
YAML_PLAIN_SCALAR_STYLE => b" :",
YAML_SINGLE_QUOTED_SCALAR_STYLE => b" '",
YAML_DOUBLE_QUOTED_SCALAR_STYLE => b" \"",
YAML_LITERAL_SCALAR_STYLE => b" |",
YAML_FOLDED_SCALAR_STYLE => b" >",
ScalarStyle::Plain => b" :",
ScalarStyle::SingleQuoted => b" '",
ScalarStyle::DoubleQuoted => b" \"",
ScalarStyle::Literal => b" |",
ScalarStyle::Folded => b" >",
_ => process::abort(),
});
print_escaped(stdout, value);

View file

@ -3,9 +3,7 @@ use std::mem::take;
use alloc::string::String;
use alloc::vec;
use crate::yaml::{
Anchors, Document, Emitter, Event, EventData, Node, NodeData, YAML_ANY_ENCODING,
};
use crate::yaml::{Anchors, Any, Document, Emitter, Event, EventData, Node, NodeData};
use crate::{
yaml_emitter_emit, EmitterError, YAML_DEFAULT_MAPPING_TAG, YAML_DEFAULT_SCALAR_TAG,
YAML_DEFAULT_SEQUENCE_TAG,
@ -17,9 +15,7 @@ use crate::{
pub fn yaml_emitter_open(emitter: &mut Emitter) -> Result<(), EmitterError> {
assert!(!emitter.opened);
let event = Event {
data: EventData::StreamStart {
encoding: YAML_ANY_ENCODING,
},
data: EventData::StreamStart { encoding: Any },
..Default::default()
};
yaml_emitter_emit(emitter, event)?;

View file

@ -6,19 +6,8 @@ use crate::macros::{
};
use crate::yaml::EventData;
use crate::{
yaml_emitter_flush, Emitter, EmitterError, Event, ScalarStyle, TagDirective, VersionDirective,
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,
YAML_EMIT_BLOCK_MAPPING_KEY_STATE, YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE,
YAML_EMIT_BLOCK_MAPPING_VALUE_STATE, YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE,
YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE, YAML_EMIT_DOCUMENT_CONTENT_STATE,
YAML_EMIT_DOCUMENT_END_STATE, YAML_EMIT_DOCUMENT_START_STATE, YAML_EMIT_END_STATE,
YAML_EMIT_FIRST_DOCUMENT_START_STATE, YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE,
YAML_EMIT_FLOW_MAPPING_KEY_STATE, YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE,
YAML_EMIT_FLOW_MAPPING_VALUE_STATE, YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE,
YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE, YAML_EMIT_STREAM_START_STATE, YAML_FLOW_MAPPING_STYLE,
YAML_FLOW_SEQUENCE_STYLE, YAML_FOLDED_SCALAR_STYLE, YAML_LITERAL_SCALAR_STYLE, YAML_LN_BREAK,
YAML_PLAIN_SCALAR_STYLE, YAML_SINGLE_QUOTED_SCALAR_STYLE, YAML_UTF8_ENCODING,
yaml_emitter_flush, Break, Emitter, EmitterError, EmitterState, Encoding, Event, MappingStyle,
ScalarStyle, SequenceStyle, TagDirective, VersionDirective, WriterError,
};
fn FLUSH(emitter: &mut Emitter) -> Result<(), WriterError> {
@ -39,11 +28,11 @@ fn PUT(emitter: &mut Emitter, value: u8) -> Result<(), WriterError> {
fn PUT_BREAK(emitter: &mut Emitter) -> Result<(), WriterError> {
FLUSH(emitter)?;
if emitter.line_break == YAML_CR_BREAK {
if emitter.line_break == Break::Cr {
emitter.buffer.push('\r');
} else if emitter.line_break == YAML_LN_BREAK {
} else if emitter.line_break == Break::Ln {
emitter.buffer.push('\n');
} else if emitter.line_break == YAML_CRLN_BREAK {
} else if emitter.line_break == Break::CrLn {
emitter.buffer.push_str("\r\n");
};
emitter.column = 0;
@ -216,52 +205,50 @@ fn yaml_emitter_state_machine(
analysis: &mut Analysis,
) -> Result<(), EmitterError> {
match emitter.state {
YAML_EMIT_STREAM_START_STATE => yaml_emitter_emit_stream_start(emitter, event),
YAML_EMIT_FIRST_DOCUMENT_START_STATE => {
yaml_emitter_emit_document_start(emitter, event, true)
}
YAML_EMIT_DOCUMENT_START_STATE => yaml_emitter_emit_document_start(emitter, event, false),
YAML_EMIT_DOCUMENT_CONTENT_STATE => {
EmitterState::StreamStart => yaml_emitter_emit_stream_start(emitter, event),
EmitterState::FirstDocumentStart => yaml_emitter_emit_document_start(emitter, event, true),
EmitterState::DocumentStart => yaml_emitter_emit_document_start(emitter, event, false),
EmitterState::DocumentContent => {
yaml_emitter_emit_document_content(emitter, event, analysis)
}
YAML_EMIT_DOCUMENT_END_STATE => yaml_emitter_emit_document_end(emitter, event),
YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE => {
EmitterState::DocumentEnd => yaml_emitter_emit_document_end(emitter, event),
EmitterState::FlowSequenceFirstItem => {
yaml_emitter_emit_flow_sequence_item(emitter, event, true, analysis)
}
YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE => {
EmitterState::FlowSequenceItem => {
yaml_emitter_emit_flow_sequence_item(emitter, event, false, analysis)
}
YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE => {
EmitterState::FlowMappingFirstKey => {
yaml_emitter_emit_flow_mapping_key(emitter, event, true, analysis)
}
YAML_EMIT_FLOW_MAPPING_KEY_STATE => {
EmitterState::FlowMappingKey => {
yaml_emitter_emit_flow_mapping_key(emitter, event, false, analysis)
}
YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE => {
EmitterState::FlowMappingSimpleValue => {
yaml_emitter_emit_flow_mapping_value(emitter, event, true, analysis)
}
YAML_EMIT_FLOW_MAPPING_VALUE_STATE => {
EmitterState::FlowMappingValue => {
yaml_emitter_emit_flow_mapping_value(emitter, event, false, analysis)
}
YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE => {
EmitterState::BlockSequenceFirstItem => {
yaml_emitter_emit_block_sequence_item(emitter, event, true, analysis)
}
YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE => {
EmitterState::BlockSequenceItem => {
yaml_emitter_emit_block_sequence_item(emitter, event, false, analysis)
}
YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE => {
EmitterState::BlockMappingFirstKey => {
yaml_emitter_emit_block_mapping_key(emitter, event, true, analysis)
}
YAML_EMIT_BLOCK_MAPPING_KEY_STATE => {
EmitterState::BlockMappingKey => {
yaml_emitter_emit_block_mapping_key(emitter, event, false, analysis)
}
YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE => {
EmitterState::BlockMappingSimpleValue => {
yaml_emitter_emit_block_mapping_value(emitter, event, true, analysis)
}
YAML_EMIT_BLOCK_MAPPING_VALUE_STATE => {
EmitterState::BlockMappingValue => {
yaml_emitter_emit_block_mapping_value(emitter, event, false, analysis)
}
YAML_EMIT_END_STATE => {
EmitterState::End => {
yaml_emitter_set_emitter_error(emitter, "expected nothing after STREAM-END")
}
}
@ -273,11 +260,11 @@ fn yaml_emitter_emit_stream_start(
) -> Result<(), EmitterError> {
emitter.open_ended = 0;
if let EventData::StreamStart { ref encoding } = event.data {
if emitter.encoding == YAML_ANY_ENCODING {
if emitter.encoding == Encoding::Any {
emitter.encoding = *encoding;
}
if emitter.encoding == YAML_ANY_ENCODING {
emitter.encoding = YAML_UTF8_ENCODING;
if emitter.encoding == Encoding::Any {
emitter.encoding = Encoding::Utf8;
}
if emitter.best_indent < 2 || emitter.best_indent > 9 {
emitter.best_indent = 2;
@ -288,18 +275,18 @@ fn yaml_emitter_emit_stream_start(
if emitter.best_width < 0 {
emitter.best_width = i32::MAX;
}
if emitter.line_break == YAML_ANY_BREAK {
emitter.line_break = YAML_LN_BREAK;
if emitter.line_break == Break::Any {
emitter.line_break = Break::Ln;
}
emitter.indent = -1;
emitter.line = 0;
emitter.column = 0;
emitter.whitespace = true;
emitter.indention = true;
if emitter.encoding != YAML_UTF8_ENCODING {
if emitter.encoding != Encoding::Utf8 {
yaml_emitter_write_bom(emitter)?;
}
emitter.state = YAML_EMIT_FIRST_DOCUMENT_START_STATE;
emitter.state = EmitterState::FirstDocumentStart;
return Ok(());
}
yaml_emitter_set_emitter_error(emitter, "expected STREAM-START")
@ -378,7 +365,7 @@ fn yaml_emitter_emit_document_start(
yaml_emitter_write_indent(emitter)?;
}
}
emitter.state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
emitter.state = EmitterState::DocumentContent;
emitter.open_ended = 0;
return Ok(());
} else if let EventData::StreamEnd = &event.data {
@ -388,7 +375,7 @@ fn yaml_emitter_emit_document_start(
yaml_emitter_write_indent(emitter)?;
}
yaml_emitter_flush(emitter)?;
emitter.state = YAML_EMIT_END_STATE;
emitter.state = EmitterState::End;
return Ok(());
}
@ -400,7 +387,7 @@ fn yaml_emitter_emit_document_content(
event: &Event,
analysis: &mut Analysis,
) -> Result<(), EmitterError> {
emitter.states.push(YAML_EMIT_DOCUMENT_END_STATE);
emitter.states.push(EmitterState::DocumentEnd);
yaml_emitter_emit_node(emitter, event, true, false, false, false, analysis)
}
@ -419,7 +406,7 @@ fn yaml_emitter_emit_document_end(
emitter.open_ended = 1;
}
yaml_emitter_flush(emitter)?;
emitter.state = YAML_EMIT_DOCUMENT_START_STATE;
emitter.state = EmitterState::DocumentStart;
emitter.tag_directives.clear();
return Ok(());
}
@ -455,7 +442,7 @@ fn yaml_emitter_emit_flow_sequence_item(
if emitter.canonical || emitter.column > emitter.best_width {
yaml_emitter_write_indent(emitter)?;
}
emitter.states.push(YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE);
emitter.states.push(EmitterState::FlowSequenceItem);
yaml_emitter_emit_node(emitter, event, false, true, false, false, analysis)
}
@ -492,13 +479,11 @@ fn yaml_emitter_emit_flow_mapping_key(
yaml_emitter_write_indent(emitter)?;
}
if !emitter.canonical && yaml_emitter_check_simple_key(emitter, event, analysis) {
emitter
.states
.push(YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE);
emitter.states.push(EmitterState::FlowMappingSimpleValue);
yaml_emitter_emit_node(emitter, event, false, false, true, true, analysis)
} else {
yaml_emitter_write_indicator(emitter, "?", true, false, false)?;
emitter.states.push(YAML_EMIT_FLOW_MAPPING_VALUE_STATE);
emitter.states.push(EmitterState::FlowMappingValue);
yaml_emitter_emit_node(emitter, event, false, false, true, false, analysis)
}
}
@ -517,7 +502,7 @@ fn yaml_emitter_emit_flow_mapping_value(
}
yaml_emitter_write_indicator(emitter, ":", true, false, false)?;
}
emitter.states.push(YAML_EMIT_FLOW_MAPPING_KEY_STATE);
emitter.states.push(EmitterState::FlowMappingKey);
yaml_emitter_emit_node(emitter, event, false, false, true, false, analysis)
}
@ -541,7 +526,7 @@ fn yaml_emitter_emit_block_sequence_item(
}
yaml_emitter_write_indent(emitter)?;
yaml_emitter_write_indicator(emitter, "-", true, false, true)?;
emitter.states.push(YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE);
emitter.states.push(EmitterState::BlockSequenceItem);
yaml_emitter_emit_node(emitter, event, false, true, false, false, analysis)
}
@ -561,13 +546,11 @@ fn yaml_emitter_emit_block_mapping_key(
}
yaml_emitter_write_indent(emitter)?;
if yaml_emitter_check_simple_key(emitter, event, analysis) {
emitter
.states
.push(YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE);
emitter.states.push(EmitterState::BlockMappingSimpleValue);
yaml_emitter_emit_node(emitter, event, false, false, true, true, analysis)
} else {
yaml_emitter_write_indicator(emitter, "?", true, false, true)?;
emitter.states.push(YAML_EMIT_BLOCK_MAPPING_VALUE_STATE);
emitter.states.push(EmitterState::BlockMappingValue);
yaml_emitter_emit_node(emitter, event, false, false, true, false, analysis)
}
}
@ -584,7 +567,7 @@ fn yaml_emitter_emit_block_mapping_value(
yaml_emitter_write_indent(emitter)?;
yaml_emitter_write_indicator(emitter, ":", true, false, true)?;
}
emitter.states.push(YAML_EMIT_BLOCK_MAPPING_KEY_STATE);
emitter.states.push(EmitterState::BlockMappingKey);
yaml_emitter_emit_node(emitter, event, false, false, true, false, analysis)
}
@ -670,12 +653,12 @@ fn yaml_emitter_emit_sequence_start(
if emitter.flow_level != 0
|| emitter.canonical
|| style == YAML_FLOW_SEQUENCE_STYLE
|| style == SequenceStyle::Flow
|| yaml_emitter_check_empty_sequence(emitter, event)
{
emitter.state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
emitter.state = EmitterState::FlowSequenceFirstItem;
} else {
emitter.state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
emitter.state = EmitterState::BlockSequenceFirstItem;
};
Ok(())
}
@ -697,12 +680,12 @@ fn yaml_emitter_emit_mapping_start(
if emitter.flow_level != 0
|| emitter.canonical
|| style == YAML_FLOW_MAPPING_STYLE
|| style == MappingStyle::Flow
|| yaml_emitter_check_empty_mapping(emitter, event)
{
emitter.state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
emitter.state = EmitterState::FlowMappingFirstKey;
} else {
emitter.state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
emitter.state = EmitterState::BlockMappingFirstKey;
}
Ok(())
}
@ -794,44 +777,44 @@ fn yaml_emitter_select_scalar_style(
"neither tag nor implicit flags are specified",
)?;
}
if style == YAML_ANY_SCALAR_STYLE {
style = YAML_PLAIN_SCALAR_STYLE;
if style == ScalarStyle::Any {
style = ScalarStyle::Plain;
}
if emitter.canonical {
style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
style = ScalarStyle::DoubleQuoted;
}
if emitter.simple_key_context && scalar_analysis.multiline {
style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
style = ScalarStyle::DoubleQuoted;
}
if style == YAML_PLAIN_SCALAR_STYLE {
if style == ScalarStyle::Plain {
if emitter.flow_level != 0 && !scalar_analysis.flow_plain_allowed
|| emitter.flow_level == 0 && !scalar_analysis.block_plain_allowed
{
style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
style = ScalarStyle::SingleQuoted;
}
if scalar_analysis.value.is_empty()
&& (emitter.flow_level != 0 || emitter.simple_key_context)
{
style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
style = ScalarStyle::SingleQuoted;
}
if no_tag && !*plain_implicit {
style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
style = ScalarStyle::SingleQuoted;
}
}
if style == YAML_SINGLE_QUOTED_SCALAR_STYLE {
if style == ScalarStyle::SingleQuoted {
if !scalar_analysis.single_quoted_allowed {
style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
style = ScalarStyle::DoubleQuoted;
}
}
if style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE {
if style == ScalarStyle::Literal || style == ScalarStyle::Folded {
if !scalar_analysis.block_allowed
|| emitter.flow_level != 0
|| emitter.simple_key_context
{
style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
style = ScalarStyle::DoubleQuoted;
}
}
if no_tag && !*quoted_implicit && style != YAML_PLAIN_SCALAR_STYLE {
if no_tag && !*quoted_implicit && style != ScalarStyle::Plain {
*tag_analysis = Some(TagAnalysis {
handle: "!",
suffix: "",
@ -890,22 +873,22 @@ fn yaml_emitter_process_scalar(
analysis: &ScalarAnalysis,
) -> Result<(), EmitterError> {
match analysis.style {
YAML_PLAIN_SCALAR_STYLE => {
ScalarStyle::Plain => {
yaml_emitter_write_plain_scalar(emitter, analysis.value, !emitter.simple_key_context)
}
YAML_SINGLE_QUOTED_SCALAR_STYLE => yaml_emitter_write_single_quoted_scalar(
ScalarStyle::SingleQuoted => yaml_emitter_write_single_quoted_scalar(
emitter,
analysis.value,
!emitter.simple_key_context,
),
YAML_DOUBLE_QUOTED_SCALAR_STYLE => yaml_emitter_write_double_quoted_scalar(
ScalarStyle::DoubleQuoted => yaml_emitter_write_double_quoted_scalar(
emitter,
analysis.value,
!emitter.simple_key_context,
),
YAML_LITERAL_SCALAR_STYLE => yaml_emitter_write_literal_scalar(emitter, analysis.value),
YAML_FOLDED_SCALAR_STYLE => yaml_emitter_write_folded_scalar(emitter, analysis.value),
YAML_ANY_SCALAR_STYLE => unreachable!("No scalar style chosen"),
ScalarStyle::Literal => yaml_emitter_write_literal_scalar(emitter, analysis.value),
ScalarStyle::Folded => yaml_emitter_write_folded_scalar(emitter, analysis.value),
ScalarStyle::Any => unreachable!("No scalar style chosen"),
}
}
@ -1034,7 +1017,7 @@ fn yaml_emitter_analyze_scalar<'a>(
block_plain_allowed: true,
single_quoted_allowed: true,
block_allowed: false,
style: YAML_ANY_SCALAR_STYLE,
style: ScalarStyle::Any,
});
}
@ -1135,7 +1118,7 @@ fn yaml_emitter_analyze_scalar<'a>(
block_plain_allowed: true,
single_quoted_allowed: true,
block_allowed: true,
style: YAML_ANY_SCALAR_STYLE,
style: ScalarStyle::Any,
};
analysis.multiline = line_breaks;

View file

@ -77,10 +77,9 @@ pub use crate::yaml::{
};
#[doc(hidden)]
pub use crate::yaml::{
Break::*, EmitterState::*, Encoding::*, MappingStyle::*, ParserState::*, ScalarStyle::*,
SequenceStyle::*, YAML_BOOL_TAG, YAML_DEFAULT_MAPPING_TAG, YAML_DEFAULT_SCALAR_TAG,
YAML_DEFAULT_SEQUENCE_TAG, YAML_FLOAT_TAG, YAML_INT_TAG, YAML_MAP_TAG, YAML_NULL_TAG,
YAML_SEQ_TAG, YAML_STR_TAG, YAML_TIMESTAMP_TAG,
YAML_BOOL_TAG, YAML_DEFAULT_MAPPING_TAG, YAML_DEFAULT_SCALAR_TAG, YAML_DEFAULT_SEQUENCE_TAG,
YAML_FLOAT_TAG, YAML_INT_TAG, YAML_MAP_TAG, YAML_NULL_TAG, YAML_SEQ_TAG, YAML_STR_TAG,
YAML_TIMESTAMP_TAG,
};
#[cfg(test)]
@ -200,7 +199,7 @@ foo: bar
let mut output = Vec::new();
yaml_emitter_set_output_string(&mut emitter, &mut output);
let event = yaml_stream_start_event_new(YAML_UTF8_ENCODING);
let event = yaml_stream_start_event_new(Encoding::Utf8);
yaml_emitter_emit(&mut emitter, event).unwrap();
let event = yaml_document_start_event_new(None, &[], true);
yaml_emitter_emit(&mut emitter, event).unwrap();
@ -210,7 +209,7 @@ foo: bar
"1st non-empty\n2nd non-empty 3rd non-empty",
true,
true,
YAML_PLAIN_SCALAR_STYLE,
ScalarStyle::Plain,
);
yaml_emitter_emit(&mut emitter, event).unwrap();
let event = yaml_document_end_event_new(true);

View file

@ -4,22 +4,8 @@ use alloc::{vec, vec::Vec};
use crate::scanner::yaml_parser_fetch_more_tokens;
use crate::yaml::{EventData, TokenData};
use crate::{
Event, Mark, Parser, ParserError, TagDirective, Token, VersionDirective,
YAML_BLOCK_MAPPING_STYLE, YAML_BLOCK_SEQUENCE_STYLE, YAML_FLOW_MAPPING_STYLE,
YAML_FLOW_SEQUENCE_STYLE, YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE,
YAML_PARSE_BLOCK_MAPPING_KEY_STATE, YAML_PARSE_BLOCK_MAPPING_VALUE_STATE,
YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE, YAML_PARSE_BLOCK_NODE_STATE,
YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE, YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE,
YAML_PARSE_DOCUMENT_CONTENT_STATE, YAML_PARSE_DOCUMENT_END_STATE,
YAML_PARSE_DOCUMENT_START_STATE, YAML_PARSE_END_STATE,
YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE, YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE,
YAML_PARSE_FLOW_MAPPING_KEY_STATE, YAML_PARSE_FLOW_MAPPING_VALUE_STATE,
YAML_PARSE_FLOW_NODE_STATE, YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE,
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE,
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE, YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE,
YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE, YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE,
YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE, YAML_PARSE_STREAM_START_STATE,
YAML_PLAIN_SCALAR_STYLE,
Event, MappingStyle, Mark, Parser, ParserError, ParserState, ScalarStyle, SequenceStyle,
TagDirective, Token, VersionDirective,
};
fn PEEK_TOKEN<'a>(parser: &'a mut Parser) -> Result<&'a Token, ParserError> {
@ -79,7 +65,7 @@ fn SKIP_TOKEN(parser: &mut Parser) {
/// calls of yaml_parser_scan() or yaml_parser_load(). Doing this will break the
/// parser.
pub fn yaml_parser_parse(parser: &mut Parser) -> Result<Event, ParserError> {
if parser.stream_end_produced || parser.state == YAML_PARSE_END_STATE {
if parser.stream_end_produced || parser.state == ParserState::End {
return Ok(Event {
data: EventData::StreamEnd,
..Default::default()
@ -114,52 +100,38 @@ fn yaml_parser_set_parser_error_context<T>(
fn yaml_parser_state_machine(parser: &mut Parser) -> Result<Event, ParserError> {
match parser.state {
YAML_PARSE_STREAM_START_STATE => yaml_parser_parse_stream_start(parser),
YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE => yaml_parser_parse_document_start(parser, true),
YAML_PARSE_DOCUMENT_START_STATE => yaml_parser_parse_document_start(parser, false),
YAML_PARSE_DOCUMENT_CONTENT_STATE => yaml_parser_parse_document_content(parser),
YAML_PARSE_DOCUMENT_END_STATE => yaml_parser_parse_document_end(parser),
YAML_PARSE_BLOCK_NODE_STATE => yaml_parser_parse_node(parser, true, false),
YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE => {
yaml_parser_parse_node(parser, true, true)
}
YAML_PARSE_FLOW_NODE_STATE => yaml_parser_parse_node(parser, false, false),
YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE => {
ParserState::StreamStart => yaml_parser_parse_stream_start(parser),
ParserState::ImplicitDocumentStart => yaml_parser_parse_document_start(parser, true),
ParserState::DocumentStart => yaml_parser_parse_document_start(parser, false),
ParserState::DocumentContent => yaml_parser_parse_document_content(parser),
ParserState::DocumentEnd => yaml_parser_parse_document_end(parser),
ParserState::BlockNode => yaml_parser_parse_node(parser, true, false),
ParserState::BlockNodeOrIndentlessSequence => yaml_parser_parse_node(parser, true, true),
ParserState::FlowNode => yaml_parser_parse_node(parser, false, false),
ParserState::BlockSequenceFirstEntry => {
yaml_parser_parse_block_sequence_entry(parser, true)
}
YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE => {
yaml_parser_parse_block_sequence_entry(parser, false)
}
YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE => {
yaml_parser_parse_indentless_sequence_entry(parser)
}
YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE => {
yaml_parser_parse_block_mapping_key(parser, true)
}
YAML_PARSE_BLOCK_MAPPING_KEY_STATE => yaml_parser_parse_block_mapping_key(parser, false),
YAML_PARSE_BLOCK_MAPPING_VALUE_STATE => yaml_parser_parse_block_mapping_value(parser),
YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE => {
yaml_parser_parse_flow_sequence_entry(parser, true)
}
YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE => {
yaml_parser_parse_flow_sequence_entry(parser, false)
}
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE => {
ParserState::BlockSequenceEntry => yaml_parser_parse_block_sequence_entry(parser, false),
ParserState::IndentlessSequenceEntry => yaml_parser_parse_indentless_sequence_entry(parser),
ParserState::BlockMappingFirstKey => yaml_parser_parse_block_mapping_key(parser, true),
ParserState::BlockMappingKey => yaml_parser_parse_block_mapping_key(parser, false),
ParserState::BlockMappingValue => yaml_parser_parse_block_mapping_value(parser),
ParserState::FlowSequenceFirstEntry => yaml_parser_parse_flow_sequence_entry(parser, true),
ParserState::FlowSequenceEntry => yaml_parser_parse_flow_sequence_entry(parser, false),
ParserState::FlowSequenceEntryMappingKey => {
yaml_parser_parse_flow_sequence_entry_mapping_key(parser)
}
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE => {
ParserState::FlowSequenceEntryMappingValue => {
yaml_parser_parse_flow_sequence_entry_mapping_value(parser)
}
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE => {
ParserState::FlowSequenceEntryMappingEnd => {
yaml_parser_parse_flow_sequence_entry_mapping_end(parser)
}
YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE => yaml_parser_parse_flow_mapping_key(parser, true),
YAML_PARSE_FLOW_MAPPING_KEY_STATE => yaml_parser_parse_flow_mapping_key(parser, false),
YAML_PARSE_FLOW_MAPPING_VALUE_STATE => yaml_parser_parse_flow_mapping_value(parser, false),
YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE => {
yaml_parser_parse_flow_mapping_value(parser, true)
}
YAML_PARSE_END_STATE => panic!("parser end state reached unexpectedly"),
ParserState::FlowMappingFirstKey => yaml_parser_parse_flow_mapping_key(parser, true),
ParserState::FlowMappingKey => yaml_parser_parse_flow_mapping_key(parser, false),
ParserState::FlowMappingValue => yaml_parser_parse_flow_mapping_value(parser, false),
ParserState::FlowMappingEmptyValue => yaml_parser_parse_flow_mapping_value(parser, true),
ParserState::End => panic!("parser end state reached unexpectedly"),
}
}
@ -174,7 +146,7 @@ fn yaml_parser_parse_stream_start(parser: &mut Parser) -> Result<Event, ParserEr
start_mark: token.start_mark,
end_mark: token.end_mark,
};
parser.state = YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE;
parser.state = ParserState::ImplicitDocumentStart;
SKIP_TOKEN(parser);
Ok(event)
} else {
@ -213,8 +185,8 @@ fn yaml_parser_parse_document_start(
end_mark: token.end_mark,
};
yaml_parser_process_directives(parser, None, None)?;
parser.states.push(YAML_PARSE_DOCUMENT_END_STATE);
parser.state = YAML_PARSE_BLOCK_NODE_STATE;
parser.states.push(ParserState::DocumentEnd);
parser.state = ParserState::BlockNode;
Ok(event)
} else if !token.data.is_stream_end() {
let end_mark: Mark;
@ -241,8 +213,8 @@ fn yaml_parser_parse_document_start(
start_mark,
end_mark,
};
parser.states.push(YAML_PARSE_DOCUMENT_END_STATE);
parser.state = YAML_PARSE_DOCUMENT_CONTENT_STATE;
parser.states.push(ParserState::DocumentEnd);
parser.state = ParserState::DocumentContent;
SKIP_TOKEN(parser);
return Ok(event);
}
@ -252,7 +224,7 @@ fn yaml_parser_parse_document_start(
start_mark: token.start_mark,
end_mark: token.end_mark,
};
parser.state = YAML_PARSE_END_STATE;
parser.state = ParserState::End;
SKIP_TOKEN(parser);
Ok(event)
}
@ -286,7 +258,7 @@ fn yaml_parser_parse_document_end(parser: &mut Parser) -> Result<Event, ParserEr
implicit = false;
}
parser.tag_directives.clear();
parser.state = YAML_PARSE_DOCUMENT_START_STATE;
parser.state = ParserState::DocumentStart;
Ok(Event {
data: EventData::DocumentEnd { implicit },
start_mark,
@ -384,13 +356,13 @@ fn yaml_parser_parse_node(
if indentless_sequence && token.data.is_block_entry() {
end_mark = token.end_mark;
parser.state = YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE;
parser.state = ParserState::IndentlessSequenceEntry;
let event = Event {
data: EventData::SequenceStart {
anchor,
tag,
implicit,
style: YAML_BLOCK_SEQUENCE_STYLE,
style: SequenceStyle::Block,
},
start_mark,
end_mark,
@ -400,7 +372,7 @@ fn yaml_parser_parse_node(
let mut plain_implicit = false;
let mut quoted_implicit = false;
end_mark = token.end_mark;
if *style == YAML_PLAIN_SCALAR_STYLE && tag.is_none() || tag.as_deref() == Some("!") {
if *style == ScalarStyle::Plain && tag.is_none() || tag.as_deref() == Some("!") {
plain_implicit = true;
} else if tag.is_none() {
quoted_implicit = true;
@ -422,13 +394,13 @@ fn yaml_parser_parse_node(
return Ok(event);
} else if let TokenData::FlowSequenceStart = &token.data {
end_mark = token.end_mark;
parser.state = YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE;
parser.state = ParserState::FlowSequenceFirstEntry;
let event = Event {
data: EventData::SequenceStart {
anchor,
tag,
implicit,
style: YAML_FLOW_SEQUENCE_STYLE,
style: SequenceStyle::Flow,
},
start_mark,
end_mark,
@ -436,13 +408,13 @@ fn yaml_parser_parse_node(
return Ok(event);
} else if let TokenData::FlowMappingStart = &token.data {
end_mark = token.end_mark;
parser.state = YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE;
parser.state = ParserState::FlowMappingFirstKey;
let event = Event {
data: EventData::MappingStart {
anchor,
tag,
implicit,
style: YAML_FLOW_MAPPING_STYLE,
style: MappingStyle::Flow,
},
start_mark,
end_mark,
@ -450,13 +422,13 @@ fn yaml_parser_parse_node(
return Ok(event);
} else if block && token.data.is_block_sequence_start() {
end_mark = token.end_mark;
parser.state = YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE;
parser.state = ParserState::BlockSequenceFirstEntry;
let event = Event {
data: EventData::SequenceStart {
anchor,
tag,
implicit,
style: YAML_BLOCK_SEQUENCE_STYLE,
style: SequenceStyle::Block,
},
start_mark,
end_mark,
@ -464,13 +436,13 @@ fn yaml_parser_parse_node(
return Ok(event);
} else if block && token.data.is_block_mapping_start() {
end_mark = token.end_mark;
parser.state = YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE;
parser.state = ParserState::BlockMappingFirstKey;
let event = Event {
data: EventData::MappingStart {
anchor,
tag,
implicit,
style: YAML_BLOCK_MAPPING_STYLE,
style: MappingStyle::Block,
},
start_mark,
end_mark,
@ -485,7 +457,7 @@ fn yaml_parser_parse_node(
value: String::new(),
plain_implicit: implicit,
quoted_implicit: false,
style: YAML_PLAIN_SCALAR_STYLE,
style: ScalarStyle::Plain,
},
start_mark,
end_mark,
@ -523,10 +495,10 @@ fn yaml_parser_parse_block_sequence_entry(
SKIP_TOKEN(parser);
token = PEEK_TOKEN(parser)?;
if !token.data.is_block_entry() && !token.data.is_block_end() {
parser.states.push(YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE);
parser.states.push(ParserState::BlockSequenceEntry);
yaml_parser_parse_node(parser, true, false)
} else {
parser.state = YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE;
parser.state = ParserState::BlockSequenceEntry;
yaml_parser_process_empty_scalar(mark)
}
} else if token.data.is_block_end() {
@ -562,12 +534,10 @@ fn yaml_parser_parse_indentless_sequence_entry(parser: &mut Parser) -> Result<Ev
&& !token.data.is_value()
&& !token.data.is_block_end()
{
parser
.states
.push(YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE);
parser.states.push(ParserState::IndentlessSequenceEntry);
yaml_parser_parse_node(parser, true, false)
} else {
parser.state = YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE;
parser.state = ParserState::IndentlessSequenceEntry;
yaml_parser_process_empty_scalar(mark)
}
} else {
@ -598,10 +568,10 @@ fn yaml_parser_parse_block_mapping_key(
SKIP_TOKEN(parser);
token = PEEK_TOKEN(parser)?;
if !token.data.is_key() && !token.data.is_value() && !token.data.is_block_end() {
parser.states.push(YAML_PARSE_BLOCK_MAPPING_VALUE_STATE);
parser.states.push(ParserState::BlockMappingValue);
yaml_parser_parse_node(parser, true, true)
} else {
parser.state = YAML_PARSE_BLOCK_MAPPING_VALUE_STATE;
parser.state = ParserState::BlockMappingValue;
yaml_parser_process_empty_scalar(mark)
}
} else if token.data.is_block_end() {
@ -633,15 +603,15 @@ fn yaml_parser_parse_block_mapping_value(parser: &mut Parser) -> Result<Event, P
SKIP_TOKEN(parser);
token = PEEK_TOKEN(parser)?;
if !token.data.is_key() && !token.data.is_value() && !token.data.is_block_end() {
parser.states.push(YAML_PARSE_BLOCK_MAPPING_KEY_STATE);
parser.states.push(ParserState::BlockMappingKey);
yaml_parser_parse_node(parser, true, true)
} else {
parser.state = YAML_PARSE_BLOCK_MAPPING_KEY_STATE;
parser.state = ParserState::BlockMappingKey;
yaml_parser_process_empty_scalar(mark)
}
} else {
let mark = token.start_mark;
parser.state = YAML_PARSE_BLOCK_MAPPING_KEY_STATE;
parser.state = ParserState::BlockMappingKey;
yaml_parser_process_empty_scalar(mark)
}
}
@ -680,16 +650,16 @@ fn yaml_parser_parse_flow_sequence_entry(
anchor: None,
tag: None,
implicit: true,
style: YAML_FLOW_MAPPING_STYLE,
style: MappingStyle::Flow,
},
start_mark: token.start_mark,
end_mark: token.end_mark,
};
parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE;
parser.state = ParserState::FlowSequenceEntryMappingKey;
SKIP_TOKEN(parser);
return Ok(event);
} else if !token.data.is_flow_sequence_end() {
parser.states.push(YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE);
parser.states.push(ParserState::FlowSequenceEntry);
return yaml_parser_parse_node(parser, false, false);
}
}
@ -711,12 +681,12 @@ fn yaml_parser_parse_flow_sequence_entry_mapping_key(
if !token.data.is_value() && !token.data.is_flow_entry() && !token.data.is_flow_sequence_end() {
parser
.states
.push(YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE);
.push(ParserState::FlowSequenceEntryMappingValue);
yaml_parser_parse_node(parser, false, false)
} else {
let mark: Mark = token.end_mark;
SKIP_TOKEN(parser);
parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE;
parser.state = ParserState::FlowSequenceEntryMappingValue;
yaml_parser_process_empty_scalar(mark)
}
}
@ -729,14 +699,12 @@ fn yaml_parser_parse_flow_sequence_entry_mapping_value(
SKIP_TOKEN(parser);
token = PEEK_TOKEN(parser)?;
if !token.data.is_flow_entry() && !token.data.is_flow_sequence_end() {
parser
.states
.push(YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE);
parser.states.push(ParserState::FlowSequenceEntryMappingEnd);
return yaml_parser_parse_node(parser, false, false);
}
}
let mark = token.start_mark;
parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE;
parser.state = ParserState::FlowSequenceEntryMappingEnd;
yaml_parser_process_empty_scalar(mark)
}
@ -746,7 +714,7 @@ fn yaml_parser_parse_flow_sequence_entry_mapping_end(
let token = PEEK_TOKEN(parser)?;
let start_mark = token.start_mark;
let end_mark = token.end_mark;
parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE;
parser.state = ParserState::FlowSequenceEntry;
Ok(Event {
data: EventData::MappingEnd,
start_mark,
@ -789,17 +757,15 @@ fn yaml_parser_parse_flow_mapping_key(
&& !token.data.is_flow_entry()
&& !token.data.is_flow_mapping_end()
{
parser.states.push(YAML_PARSE_FLOW_MAPPING_VALUE_STATE);
parser.states.push(ParserState::FlowMappingValue);
return yaml_parser_parse_node(parser, false, false);
} else {
let mark = token.start_mark;
parser.state = YAML_PARSE_FLOW_MAPPING_VALUE_STATE;
parser.state = ParserState::FlowMappingValue;
return yaml_parser_process_empty_scalar(mark);
}
} else if !token.data.is_flow_mapping_end() {
parser
.states
.push(YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE);
parser.states.push(ParserState::FlowMappingEmptyValue);
return yaml_parser_parse_node(parser, false, false);
}
}
@ -821,19 +787,19 @@ fn yaml_parser_parse_flow_mapping_value(
let mut token = PEEK_TOKEN(parser)?;
if empty {
let mark = token.start_mark;
parser.state = YAML_PARSE_FLOW_MAPPING_KEY_STATE;
parser.state = ParserState::FlowMappingKey;
return yaml_parser_process_empty_scalar(mark);
}
if token.data.is_value() {
SKIP_TOKEN(parser);
token = PEEK_TOKEN(parser)?;
if !token.data.is_flow_entry() && !token.data.is_flow_mapping_end() {
parser.states.push(YAML_PARSE_FLOW_MAPPING_KEY_STATE);
parser.states.push(ParserState::FlowMappingKey);
return yaml_parser_parse_node(parser, false, false);
}
}
let mark = token.start_mark;
parser.state = YAML_PARSE_FLOW_MAPPING_KEY_STATE;
parser.state = ParserState::FlowMappingKey;
yaml_parser_process_empty_scalar(mark)
}
@ -845,7 +811,7 @@ fn yaml_parser_process_empty_scalar(mark: Mark) -> Result<Event, ParserError> {
value: String::new(),
plain_implicit: true,
quoted_implicit: false,
style: YAML_PLAIN_SCALAR_STYLE,
style: ScalarStyle::Plain,
},
start_mark: mark,
end_mark: mark,

View file

@ -2,10 +2,7 @@ use std::io::BufRead;
use alloc::collections::VecDeque;
use crate::{
Encoding, Parser, ReaderError, YAML_ANY_ENCODING, YAML_UTF16BE_ENCODING, YAML_UTF16LE_ENCODING,
YAML_UTF8_ENCODING,
};
use crate::{Encoding, Parser, ReaderError};
fn yaml_parser_set_reader_error<T>(
problem: &'static str,
@ -36,7 +33,7 @@ fn yaml_parser_determine_encoding(
let mut bom = [0; 3];
reader.read_exact(&mut bom)?;
if bom == BOM_UTF8 {
Ok(Some(YAML_UTF8_ENCODING))
Ok(Some(Encoding::Utf8))
} else {
Err(ReaderError::InvalidBom)
}
@ -45,14 +42,14 @@ fn yaml_parser_determine_encoding(
let mut bom = [0; 2];
reader.read_exact(&mut bom)?;
if bom == BOM_UTF16LE {
Ok(Some(YAML_UTF16LE_ENCODING))
Ok(Some(Encoding::Utf16Le))
} else if bom == BOM_UTF16BE {
Ok(Some(YAML_UTF16BE_ENCODING))
Ok(Some(Encoding::Utf16Be))
} else {
Err(ReaderError::InvalidBom)
}
}
_ => Ok(Some(YAML_UTF8_ENCODING)),
_ => Ok(Some(Encoding::Utf8)),
}
}
@ -290,7 +287,7 @@ pub(crate) fn yaml_parser_update_buffer(
if parser.unread >= length {
return Ok(());
}
if parser.encoding == YAML_ANY_ENCODING {
if parser.encoding == Encoding::Any {
if let Some(encoding) = yaml_parser_determine_encoding(reader)? {
parser.encoding = encoding;
} else {
@ -307,14 +304,12 @@ pub(crate) fn yaml_parser_update_buffer(
let tokens_before = parser.buffer.len();
let not_eof = match parser.encoding {
YAML_ANY_ENCODING => unreachable!(),
YAML_UTF8_ENCODING => {
read_utf8_buffered(reader, &mut parser.buffer, &mut parser.offset)?
}
YAML_UTF16LE_ENCODING => {
Encoding::Any => unreachable!(),
Encoding::Utf8 => read_utf8_buffered(reader, &mut parser.buffer, &mut parser.offset)?,
Encoding::Utf16Le => {
read_utf16_buffered::<false>(reader, &mut parser.buffer, &mut parser.offset)?
}
YAML_UTF16BE_ENCODING => {
Encoding::Utf16Be => {
read_utf16_buffered::<true>(reader, &mut parser.buffer, &mut parser.offset)?
}
};

View file

@ -3,11 +3,7 @@ use alloc::string::String;
use crate::macros::{is_blankz, is_break, vecdeque_starts_with};
use crate::reader::yaml_parser_update_buffer;
use crate::yaml::TokenData;
use crate::{
Mark, Parser, ReaderError, ScannerError, SimpleKey, Token, YAML_DOUBLE_QUOTED_SCALAR_STYLE,
YAML_FOLDED_SCALAR_STYLE, YAML_LITERAL_SCALAR_STYLE, YAML_PLAIN_SCALAR_STYLE,
YAML_SINGLE_QUOTED_SCALAR_STYLE,
};
use crate::{Mark, Parser, ReaderError, ScalarStyle, ScannerError, SimpleKey, Token};
fn CACHE(parser: &mut Parser, length: usize) -> Result<(), ReaderError> {
if parser.unread >= length {
@ -1372,9 +1368,9 @@ fn yaml_parser_scan_block_scalar(
data: TokenData::Scalar {
value: string,
style: if literal {
YAML_LITERAL_SCALAR_STYLE
ScalarStyle::Literal
} else {
YAML_FOLDED_SCALAR_STYLE
ScalarStyle::Folded
},
},
start_mark,
@ -1664,9 +1660,9 @@ fn yaml_parser_scan_flow_scalar(
data: TokenData::Scalar {
value: string,
style: if single {
YAML_SINGLE_QUOTED_SCALAR_STYLE
ScalarStyle::SingleQuoted
} else {
YAML_DOUBLE_QUOTED_SCALAR_STYLE
ScalarStyle::DoubleQuoted
},
},
start_mark,
@ -1799,7 +1795,7 @@ fn yaml_parser_scan_plain_scalar(
*token = Token {
data: TokenData::Scalar {
value: string,
style: YAML_PLAIN_SCALAR_STYLE,
style: ScalarStyle::Plain,
},
start_mark,
end_mark,

View file

@ -1,10 +1,9 @@
use crate::Encoding::YAML_UTF16BE_ENCODING;
use crate::{Emitter, WriterError, YAML_ANY_ENCODING, YAML_UTF16LE_ENCODING, YAML_UTF8_ENCODING};
use crate::{Emitter, Encoding, WriterError};
/// Flush the accumulated characters to the output.
pub fn yaml_emitter_flush(emitter: &mut Emitter) -> Result<(), WriterError> {
assert!((emitter.write_handler).is_some());
assert_ne!(emitter.encoding, YAML_ANY_ENCODING);
assert_ne!(emitter.encoding, Encoding::Any);
if emitter.buffer.is_empty() {
return Ok(());
@ -13,7 +12,7 @@ pub fn yaml_emitter_flush(emitter: &mut Emitter) -> Result<(), WriterError> {
// TODO: Support partial writes. These calls fail unless the writer is able
// to write absolutely everything in the buffer.
if emitter.encoding == YAML_UTF8_ENCODING {
if emitter.encoding == Encoding::Utf8 {
let to_emit = emitter.buffer.as_bytes();
emitter
.write_handler
@ -25,9 +24,11 @@ pub fn yaml_emitter_flush(emitter: &mut Emitter) -> Result<(), WriterError> {
}
let big_endian = match emitter.encoding {
YAML_ANY_ENCODING | YAML_UTF8_ENCODING => unreachable!("unhandled encoding"),
YAML_UTF16LE_ENCODING => false,
YAML_UTF16BE_ENCODING => true,
Encoding::Any | Encoding::Utf8 => {
unreachable!("unhandled encoding")
}
Encoding::Utf16Le => false,
Encoding::Utf16Be => true,
};
for ch in emitter.buffer.encode_utf16() {

View file

@ -57,13 +57,13 @@ pub struct TagDirective {
pub enum Encoding {
/// Let the parser choose the encoding.
#[default]
YAML_ANY_ENCODING = 0,
Any = 0,
/// The default UTF-8 encoding.
YAML_UTF8_ENCODING = 1,
Utf8 = 1,
/// The UTF-16-LE encoding with BOM.
YAML_UTF16LE_ENCODING = 2,
Utf16Le = 2,
/// The UTF-16-BE encoding with BOM.
YAML_UTF16BE_ENCODING = 3,
Utf16Be = 3,
}
/// Line break type.
@ -72,13 +72,13 @@ pub enum Encoding {
pub enum Break {
/// Let the parser choose the break type.
#[default]
YAML_ANY_BREAK = 0,
Any = 0,
/// Use CR for line breaks (Mac style).
YAML_CR_BREAK = 1,
Cr = 1,
/// Use LN for line breaks (Unix style).
YAML_LN_BREAK = 2,
Ln = 2,
/// Use CR LN for line breaks (DOS style).
YAML_CRLN_BREAK = 3,
CrLn = 3,
}
/// The pointer position.
@ -99,17 +99,17 @@ pub struct Mark {
pub enum ScalarStyle {
/// Let the emitter choose the style.
#[default]
YAML_ANY_SCALAR_STYLE = 0,
Any = 0,
/// The plain scalar style.
YAML_PLAIN_SCALAR_STYLE = 1,
Plain = 1,
/// The single-quoted scalar style.
YAML_SINGLE_QUOTED_SCALAR_STYLE = 2,
SingleQuoted = 2,
/// The double-quoted scalar style.
YAML_DOUBLE_QUOTED_SCALAR_STYLE = 3,
DoubleQuoted = 3,
/// The literal scalar style.
YAML_LITERAL_SCALAR_STYLE = 4,
Literal = 4,
/// The folded scalar style.
YAML_FOLDED_SCALAR_STYLE = 5,
Folded = 5,
}
/// Sequence styles.
@ -117,11 +117,11 @@ pub enum ScalarStyle {
#[non_exhaustive]
pub enum SequenceStyle {
/// Let the emitter choose the style.
YAML_ANY_SEQUENCE_STYLE = 0,
Any = 0,
/// The block sequence style.
YAML_BLOCK_SEQUENCE_STYLE = 1,
Block = 1,
/// The flow sequence style.
YAML_FLOW_SEQUENCE_STYLE = 2,
Flow = 2,
}
/// Mapping styles.
@ -129,11 +129,11 @@ pub enum SequenceStyle {
#[non_exhaustive]
pub enum MappingStyle {
/// Let the emitter choose the style.
YAML_ANY_MAPPING_STYLE = 0,
Any = 0,
/// The block mapping style.
YAML_BLOCK_MAPPING_STYLE = 1,
Block = 1,
/// The flow mapping style.
YAML_FLOW_MAPPING_STYLE = 2,
Flow = 2,
}
/// The token structure.
@ -520,53 +520,53 @@ pub struct SimpleKey {
pub enum ParserState {
/// Expect STREAM-START.
#[default]
YAML_PARSE_STREAM_START_STATE = 0,
StreamStart = 0,
/// Expect the beginning of an implicit document.
YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE = 1,
ImplicitDocumentStart = 1,
/// Expect DOCUMENT-START.
YAML_PARSE_DOCUMENT_START_STATE = 2,
DocumentStart = 2,
/// Expect the content of a document.
YAML_PARSE_DOCUMENT_CONTENT_STATE = 3,
DocumentContent = 3,
/// Expect DOCUMENT-END.
YAML_PARSE_DOCUMENT_END_STATE = 4,
DocumentEnd = 4,
/// Expect a block node.
YAML_PARSE_BLOCK_NODE_STATE = 5,
BlockNode = 5,
/// Expect a block node or indentless sequence.
YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE = 6,
BlockNodeOrIndentlessSequence = 6,
/// Expect a flow node.
YAML_PARSE_FLOW_NODE_STATE = 7,
FlowNode = 7,
/// Expect the first entry of a block sequence.
YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE = 8,
BlockSequenceFirstEntry = 8,
/// Expect an entry of a block sequence.
YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE = 9,
BlockSequenceEntry = 9,
/// Expect an entry of an indentless sequence.
YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE = 10,
IndentlessSequenceEntry = 10,
/// Expect the first key of a block mapping.
YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE = 11,
BlockMappingFirstKey = 11,
/// Expect a block mapping key.
YAML_PARSE_BLOCK_MAPPING_KEY_STATE = 12,
BlockMappingKey = 12,
/// Expect a block mapping value.
YAML_PARSE_BLOCK_MAPPING_VALUE_STATE = 13,
BlockMappingValue = 13,
/// Expect the first entry of a flow sequence.
YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE = 14,
FlowSequenceFirstEntry = 14,
/// Expect an entry of a flow sequence.
YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE = 15,
FlowSequenceEntry = 15,
/// Expect a key of an ordered mapping.
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE = 16,
FlowSequenceEntryMappingKey = 16,
/// Expect a value of an ordered mapping.
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE = 17,
FlowSequenceEntryMappingValue = 17,
/// Expect the and of an ordered mapping entry.
YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE = 18,
FlowSequenceEntryMappingEnd = 18,
/// Expect the first key of a flow mapping.
YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE = 19,
FlowMappingFirstKey = 19,
/// Expect a key of a flow mapping.
YAML_PARSE_FLOW_MAPPING_KEY_STATE = 20,
FlowMappingKey = 20,
/// Expect a value of a flow mapping.
YAML_PARSE_FLOW_MAPPING_VALUE_STATE = 21,
FlowMappingValue = 21,
/// Expect an empty value of a flow mapping.
YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE = 22,
FlowMappingEmptyValue = 22,
/// Expect nothing.
YAML_PARSE_END_STATE = 23,
End = 23,
}
/// This structure holds aliases data.
@ -646,41 +646,41 @@ impl<'r> Default for Parser<'r> {
pub enum EmitterState {
/// Expect STREAM-START.
#[default]
YAML_EMIT_STREAM_START_STATE = 0,
StreamStart = 0,
/// Expect the first DOCUMENT-START or STREAM-END.
YAML_EMIT_FIRST_DOCUMENT_START_STATE = 1,
FirstDocumentStart = 1,
/// Expect DOCUMENT-START or STREAM-END.
YAML_EMIT_DOCUMENT_START_STATE = 2,
DocumentStart = 2,
/// Expect the content of a document.
YAML_EMIT_DOCUMENT_CONTENT_STATE = 3,
DocumentContent = 3,
/// Expect DOCUMENT-END.
YAML_EMIT_DOCUMENT_END_STATE = 4,
DocumentEnd = 4,
/// Expect the first item of a flow sequence.
YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE = 5,
FlowSequenceFirstItem = 5,
/// Expect an item of a flow sequence.
YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE = 6,
FlowSequenceItem = 6,
/// Expect the first key of a flow mapping.
YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE = 7,
FlowMappingFirstKey = 7,
/// Expect a key of a flow mapping.
YAML_EMIT_FLOW_MAPPING_KEY_STATE = 8,
FlowMappingKey = 8,
/// Expect a value for a simple key of a flow mapping.
YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE = 9,
FlowMappingSimpleValue = 9,
/// Expect a value of a flow mapping.
YAML_EMIT_FLOW_MAPPING_VALUE_STATE = 10,
FlowMappingValue = 10,
/// Expect the first item of a block sequence.
YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE = 11,
BlockSequenceFirstItem = 11,
/// Expect an item of a block sequence.
YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE = 12,
BlockSequenceItem = 12,
/// Expect the first key of a block mapping.
YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE = 13,
BlockMappingFirstKey = 13,
/// Expect the key of a block mapping.
YAML_EMIT_BLOCK_MAPPING_KEY_STATE = 14,
BlockMappingKey = 14,
/// Expect a value for a simple key of a block mapping.
YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE = 15,
BlockMappingSimpleValue = 15,
/// Expect a value of a block mapping.
YAML_EMIT_BLOCK_MAPPING_VALUE_STATE = 16,
BlockMappingValue = 16,
/// Expect nothing.
YAML_EMIT_END_STATE = 17,
End = 17,
}
#[derive(Copy, Clone, Default)]