Stylistic: Prefer let-else

This commit is contained in:
Simon Ask Ulsnes 2024-02-04 09:43:06 +01:00
parent 7071ddd276
commit 1ffb036601
3 changed files with 109 additions and 123 deletions

View file

@ -167,22 +167,21 @@ fn yaml_emitter_dump_scalar(
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)
if let NodeData::Scalar { value, style } = node.data {
let event = Event {
data: EventData::Scalar {
anchor,
tag: node.tag,
value,
plain_implicit,
quoted_implicit,
style,
},
..Default::default()
};
yaml_emitter_emit(emitter, event)
} else {
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()
};
yaml_emitter_emit(emitter, event)
}
fn yaml_emitter_dump_sequence(
@ -193,29 +192,28 @@ fn yaml_emitter_dump_sequence(
) -> Result<(), EmitterError> {
let implicit = node.tag.as_deref() == Some(DEFAULT_SEQUENCE_TAG);
if let NodeData::Sequence { items, style } = node.data {
let event = Event {
data: EventData::SequenceStart {
anchor,
tag: node.tag,
implicit,
style,
},
..Default::default()
};
yaml_emitter_emit(emitter, event)?;
for item in items {
yaml_emitter_dump_node(emitter, document, item)?;
}
let event = Event {
data: EventData::SequenceEnd,
..Default::default()
};
yaml_emitter_emit(emitter, event)
} else {
let NodeData::Sequence { items, style } = node.data else {
unreachable!()
};
let event = Event {
data: EventData::SequenceStart {
anchor,
tag: node.tag,
implicit,
style,
},
..Default::default()
};
yaml_emitter_emit(emitter, event)?;
for item in items {
yaml_emitter_dump_node(emitter, document, item)?;
}
let event = Event {
data: EventData::SequenceEnd,
..Default::default()
};
yaml_emitter_emit(emitter, event)
}
fn yaml_emitter_dump_mapping(
@ -226,28 +224,27 @@ fn yaml_emitter_dump_mapping(
) -> Result<(), EmitterError> {
let implicit = node.tag.as_deref() == Some(DEFAULT_MAPPING_TAG);
if let NodeData::Mapping { pairs, style } = node.data {
let event = Event {
data: EventData::MappingStart {
anchor,
tag: node.tag,
implicit,
style,
},
..Default::default()
};
yaml_emitter_emit(emitter, event)?;
for pair in pairs {
yaml_emitter_dump_node(emitter, document, pair.key)?;
yaml_emitter_dump_node(emitter, document, pair.value)?;
}
let event = Event {
data: EventData::MappingEnd,
..Default::default()
};
yaml_emitter_emit(emitter, event)
} else {
let NodeData::Mapping { pairs, style } = node.data else {
unreachable!()
};
let event = Event {
data: EventData::MappingStart {
anchor,
tag: node.tag,
implicit,
style,
},
..Default::default()
};
yaml_emitter_emit(emitter, event)?;
for pair in pairs {
yaml_emitter_dump_node(emitter, document, pair.key)?;
yaml_emitter_dump_node(emitter, document, pair.value)?;
}
let event = Event {
data: EventData::MappingEnd,
..Default::default()
};
yaml_emitter_emit(emitter, event)
}

View file

@ -642,15 +642,13 @@ fn yaml_emitter_emit_sequence_start(
yaml_emitter_process_anchor(emitter, anchor)?;
yaml_emitter_process_tag(emitter, tag)?;
let style = if let EventData::SequenceStart { style, .. } = &event.data {
*style
} else {
let EventData::SequenceStart { style, .. } = &event.data else {
unreachable!()
};
if emitter.flow_level != 0
|| emitter.canonical
|| style == SequenceStyle::Flow
|| *style == SequenceStyle::Flow
|| yaml_emitter_check_empty_sequence(emitter, event)
{
emitter.state = EmitterState::FlowSequenceFirstItem;
@ -669,15 +667,13 @@ fn yaml_emitter_emit_mapping_start(
yaml_emitter_process_anchor(emitter, anchor)?;
yaml_emitter_process_tag(emitter, tag)?;
let style = if let EventData::MappingStart { style, .. } = &event.data {
*style
} else {
let EventData::MappingStart { style, .. } = &event.data else {
unreachable!()
};
if emitter.flow_level != 0
|| emitter.canonical
|| style == MappingStyle::Flow
|| *style == MappingStyle::Flow
|| yaml_emitter_check_empty_mapping(emitter, event)
{
emitter.state = EmitterState::FlowMappingFirstKey;
@ -759,66 +755,61 @@ fn yaml_emitter_select_scalar_style(
scalar_analysis: &mut ScalarAnalysis,
tag_analysis: &mut Option<TagAnalysis>,
) -> Result<(), EmitterError> {
if let EventData::Scalar {
let EventData::Scalar {
plain_implicit,
quoted_implicit,
style,
..
} = &event.data
{
let mut style: ScalarStyle = *style;
let no_tag = tag_analysis.is_none();
if no_tag && !*plain_implicit && !*quoted_implicit {
yaml_emitter_set_emitter_error(
emitter,
"neither tag nor implicit flags are specified",
)?;
}
if style == ScalarStyle::Any {
style = ScalarStyle::Plain;
}
if emitter.canonical {
style = ScalarStyle::DoubleQuoted;
}
if emitter.simple_key_context && scalar_analysis.multiline {
style = ScalarStyle::DoubleQuoted;
}
if style == ScalarStyle::Plain {
if emitter.flow_level != 0 && !scalar_analysis.flow_plain_allowed
|| emitter.flow_level == 0 && !scalar_analysis.block_plain_allowed
{
style = ScalarStyle::SingleQuoted;
}
if scalar_analysis.value.is_empty()
&& (emitter.flow_level != 0 || emitter.simple_key_context)
{
style = ScalarStyle::SingleQuoted;
}
if no_tag && !*plain_implicit {
style = ScalarStyle::SingleQuoted;
}
}
if style == ScalarStyle::SingleQuoted && !scalar_analysis.single_quoted_allowed {
style = ScalarStyle::DoubleQuoted;
}
if (style == ScalarStyle::Literal || style == ScalarStyle::Folded)
&& (!scalar_analysis.block_allowed
|| emitter.flow_level != 0
|| emitter.simple_key_context)
{
style = ScalarStyle::DoubleQuoted;
}
if no_tag && !*quoted_implicit && style != ScalarStyle::Plain {
*tag_analysis = Some(TagAnalysis {
handle: "!",
suffix: "",
});
}
scalar_analysis.style = style;
Ok(())
} else {
else {
unreachable!()
};
let mut style: ScalarStyle = *style;
let no_tag = tag_analysis.is_none();
if no_tag && !*plain_implicit && !*quoted_implicit {
yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified")?;
}
if style == ScalarStyle::Any {
style = ScalarStyle::Plain;
}
if emitter.canonical {
style = ScalarStyle::DoubleQuoted;
}
if emitter.simple_key_context && scalar_analysis.multiline {
style = ScalarStyle::DoubleQuoted;
}
if style == ScalarStyle::Plain {
if emitter.flow_level != 0 && !scalar_analysis.flow_plain_allowed
|| emitter.flow_level == 0 && !scalar_analysis.block_plain_allowed
{
style = ScalarStyle::SingleQuoted;
}
if scalar_analysis.value.is_empty()
&& (emitter.flow_level != 0 || emitter.simple_key_context)
{
style = ScalarStyle::SingleQuoted;
}
if no_tag && !*plain_implicit {
style = ScalarStyle::SingleQuoted;
}
}
if style == ScalarStyle::SingleQuoted && !scalar_analysis.single_quoted_allowed {
style = ScalarStyle::DoubleQuoted;
}
if (style == ScalarStyle::Literal || style == ScalarStyle::Folded)
&& (!scalar_analysis.block_allowed || emitter.flow_level != 0 || emitter.simple_key_context)
{
style = ScalarStyle::DoubleQuoted;
}
if no_tag && !*quoted_implicit && style != ScalarStyle::Plain {
*tag_analysis = Some(TagAnalysis {
handle: "!",
suffix: "",
});
}
scalar_analysis.style = style;
Ok(())
}
fn yaml_emitter_process_anchor(

View file

@ -233,14 +233,12 @@ fn yaml_parser_load_alias(
document: &mut Document,
ctx: &[i32],
) -> Result<(), ComposerError> {
let anchor: &str = if let EventData::Alias { anchor } = &event.data {
anchor
} else {
let EventData::Alias { anchor } = &event.data else {
unreachable!()
};
for alias_data in &parser.aliases {
if alias_data.anchor == anchor {
if alias_data.anchor == *anchor {
return yaml_parser_load_node_add(document, ctx, alias_data.index);
}
}