Remove unnecessary impl Default for Event

This commit is contained in:
Simon Ask Ulsnes 2024-02-04 12:15:56 +01:00
parent 873ccb3760
commit 882327162b
5 changed files with 76 additions and 146 deletions

View file

@ -40,9 +40,6 @@ pub(crate) fn test_main(
let mut is_end = false;
match &event.data {
EventData::NoEvent => {
_ = writeln!(stdout, "???");
}
EventData::StreamStart { .. } => {
_ = writeln!(stdout, "+STR");
}

View file

@ -345,7 +345,6 @@ impl Document {
loop {
let event = parser.parse()?;
match event.data {
EventData::NoEvent => panic!("empty event"),
EventData::StreamStart { .. } => panic!("unexpected stream start event"),
EventData::StreamEnd => panic!("unexpected stream end event"),
EventData::DocumentStart { .. } => panic!("unexpected document start event"),
@ -612,23 +611,15 @@ impl Document {
} else {
assert!(emitter.opened);
emitter.anchors = vec![Anchors::default(); self.nodes.len()];
let event = Event {
data: EventData::DocumentStart {
version_directive: self.version_directive,
tag_directives: core::mem::take(&mut self.tag_directives),
implicit: self.start_implicit,
},
..Default::default()
};
let event = Event::new(EventData::DocumentStart {
version_directive: self.version_directive,
tag_directives: core::mem::take(&mut self.tag_directives),
implicit: self.start_implicit,
});
emitter.emit(event)?;
self.anchor_node(emitter, 1);
self.dump_node(emitter, 1)?;
let event = Event {
data: EventData::DocumentEnd {
implicit: self.end_implicit,
},
..Default::default()
};
let event = Event::document_end(self.end_implicit);
emitter.emit(event)?;
}
@ -682,10 +673,7 @@ impl Document {
}
fn dump_alias(emitter: &mut Emitter, anchor: String) -> Result<(), EmitterError> {
let event = Event {
data: EventData::Alias { anchor },
..Default::default()
};
let event = Event::new(EventData::Alias { anchor });
emitter.emit(event)
}
@ -700,17 +688,14 @@ impl Document {
let NodeData::Scalar { value, style } = node.data else {
unreachable!()
};
let event = Event {
data: EventData::Scalar {
anchor,
tag: node.tag,
value,
plain_implicit,
quoted_implicit,
style,
},
..Default::default()
};
let event = Event::new(EventData::Scalar {
anchor,
tag: node.tag,
value,
plain_implicit,
quoted_implicit,
style,
});
emitter.emit(event)
}
@ -725,24 +710,18 @@ impl Document {
let NodeData::Sequence { items, style } = node.data else {
unreachable!()
};
let event = Event {
data: EventData::SequenceStart {
anchor,
tag: node.tag,
implicit,
style,
},
..Default::default()
};
let event = Event::new(EventData::SequenceStart {
anchor,
tag: node.tag,
implicit,
style,
});
emitter.emit(event)?;
for item in items {
self.dump_node(emitter, item)?;
}
let event = Event {
data: EventData::SequenceEnd,
..Default::default()
};
let event = Event::sequence_end();
emitter.emit(event)
}
@ -757,25 +736,19 @@ impl Document {
let NodeData::Mapping { pairs, style } = node.data else {
unreachable!()
};
let event = Event {
data: EventData::MappingStart {
anchor,
tag: node.tag,
implicit,
style,
},
..Default::default()
};
let event = Event::new(EventData::MappingStart {
anchor,
tag: node.tag,
implicit,
style,
});
emitter.emit(event)?;
for pair in pairs {
self.dump_node(emitter, pair.key)?;
self.dump_node(emitter, pair.value)?;
}
let event = Event {
data: EventData::MappingEnd,
..Default::default()
};
let event = Event::mapping_end();
emitter.emit(event)
}
}

View file

@ -284,12 +284,7 @@ impl<'w> Emitter<'w> {
/// [`Document::dump()`](crate::Document::dump) is called.
pub fn open(&mut self) -> Result<(), EmitterError> {
assert!(!self.opened);
let event = Event {
data: EventData::StreamStart {
encoding: Encoding::Any,
},
..Default::default()
};
let event = Event::stream_start(Encoding::Any);
self.emit(event)?;
self.opened = true;
Ok(())
@ -304,10 +299,7 @@ impl<'w> Emitter<'w> {
if self.closed {
return Ok(());
}
let event = Event {
data: EventData::StreamEnd,
..Default::default()
};
let event = Event::stream_end();
self.emit(event)?;
self.closed = true;
Ok(())

View file

@ -3,7 +3,7 @@ use crate::{
};
/// The event structure.
#[derive(Default, Debug)]
#[derive(Debug)]
#[non_exhaustive]
pub struct Event {
/// The event data.
@ -14,10 +14,8 @@ pub struct Event {
pub end_mark: Mark,
}
#[derive(Default, Debug)]
#[derive(Debug)]
pub enum EventData {
#[default]
NoEvent,
/// The stream parameters (for YAML_STREAM_START_EVENT).
StreamStart {
/// The document encoding.
@ -84,20 +82,23 @@ pub enum EventData {
}
impl Event {
/// Make an event from its data, setting both marks to zero.
pub(crate) fn new(data: EventData) -> Self {
Self {
data,
start_mark: Mark::default(),
end_mark: Mark::default(),
}
}
/// Create the STREAM-START event.
pub fn stream_start(encoding: Encoding) -> Self {
Event {
data: EventData::StreamStart { encoding },
..Default::default()
}
Self::new(EventData::StreamStart { encoding })
}
/// Create the STREAM-END event.
pub fn stream_end() -> Self {
Event {
data: EventData::StreamEnd,
..Default::default()
}
Self::new(EventData::StreamEnd)
}
/// Create the DOCUMENT-START event.
@ -111,14 +112,11 @@ impl Event {
) -> Self {
let tag_directives = tag_directives_in.to_vec();
Event {
data: EventData::DocumentStart {
version_directive,
tag_directives,
implicit,
},
..Default::default()
}
Self::new(EventData::DocumentStart {
version_directive,
tag_directives,
implicit,
})
}
/// Create the DOCUMENT-END event.
@ -126,20 +124,14 @@ impl Event {
/// The `implicit` argument is considered as a stylistic parameter and may be
/// ignored by the emitter.
pub fn document_end(implicit: bool) -> Self {
Event {
data: EventData::DocumentEnd { implicit },
..Default::default()
}
Self::new(EventData::DocumentEnd { implicit })
}
/// Create an ALIAS event.
pub fn alias(anchor: &str) -> Self {
Event {
data: EventData::Alias {
anchor: String::from(anchor),
},
..Default::default()
}
Self::new(EventData::Alias {
anchor: String::from(anchor),
})
}
/// Create a SCALAR event.
@ -157,11 +149,6 @@ impl Event {
quoted_implicit: bool,
style: ScalarStyle,
) -> Self {
let mark = Mark {
index: 0_u64,
line: 0_u64,
column: 0_u64,
};
let mut anchor_copy: Option<String> = None;
let mut tag_copy: Option<String> = None;
@ -172,18 +159,14 @@ impl Event {
tag_copy = Some(String::from(tag));
}
Event {
data: EventData::Scalar {
anchor: anchor_copy,
tag: tag_copy,
value: String::from(value),
plain_implicit,
quoted_implicit,
style,
},
start_mark: mark,
end_mark: mark,
}
Self::new(EventData::Scalar {
anchor: anchor_copy,
tag: tag_copy,
value: String::from(value),
plain_implicit,
quoted_implicit,
style,
})
}
/// Create a SEQUENCE-START event.
@ -207,23 +190,17 @@ impl Event {
tag_copy = Some(String::from(tag));
}
Event {
data: EventData::SequenceStart {
anchor: anchor_copy,
tag: tag_copy,
implicit,
style,
},
..Default::default()
}
Self::new(EventData::SequenceStart {
anchor: anchor_copy,
tag: tag_copy,
implicit,
style,
})
}
/// Create a SEQUENCE-END event.
pub fn sequence_end() -> Self {
Event {
data: EventData::SequenceEnd,
..Default::default()
}
Self::new(EventData::SequenceEnd)
}
/// Create a MAPPING-START event.
@ -248,22 +225,16 @@ impl Event {
tag_copy = Some(String::from(tag));
}
Event {
data: EventData::MappingStart {
anchor: anchor_copy,
tag: tag_copy,
implicit,
style,
},
..Default::default()
}
Self::new(EventData::MappingStart {
anchor: anchor_copy,
tag: tag_copy,
implicit,
style,
})
}
/// Create a MAPPING-END event.
pub fn mapping_end() -> Self {
Event {
data: EventData::MappingEnd,
..Default::default()
}
Self::new(EventData::MappingEnd)
}
}

View file

@ -224,10 +224,7 @@ impl<'r> Parser<'r> {
/// will break the parser.
pub fn parse(&mut self) -> Result<Event, ParserError> {
if self.scanner.stream_end_produced || self.state == ParserState::End {
return Ok(Event {
data: EventData::StreamEnd,
..Default::default()
});
return Ok(Event::stream_end());
}
self.state_machine()
}