Skip to content

Commit 928bca8

Browse files
committed
Code review changes.
1 parent e9a2944 commit 928bca8

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

vm/src/format.rs

+36-32
Original file line numberDiff line numberDiff line change
@@ -591,36 +591,40 @@ pub(crate) enum FieldNamePart {
591591
impl FieldNamePart {
592592
fn parse_part(
593593
chars: &mut impl PeekingNext<Item = char>,
594-
) -> Result<FieldNamePart, FormatParseError> {
595-
let ch = chars.next().unwrap();
596-
if ch == '.' {
597-
let mut attribute = String::new();
598-
for ch in chars.peeking_take_while(|ch| *ch != '.' && *ch != '[') {
599-
attribute.push(ch);
600-
}
601-
if attribute.is_empty() {
602-
Err(FormatParseError::EmptyAttribute)
603-
} else {
604-
Ok(FieldNamePart::Attribute(attribute))
605-
}
606-
} else if ch == '[' {
607-
let mut index = String::new();
608-
for ch in chars {
609-
if ch == ']' {
610-
return if index.is_empty() {
594+
) -> Result<Option<FieldNamePart>, FormatParseError> {
595+
chars
596+
.next()
597+
.map(|ch| match ch {
598+
'.' => {
599+
let mut attribute = String::new();
600+
for ch in chars.peeking_take_while(|ch| *ch != '.' && *ch != '[') {
601+
attribute.push(ch);
602+
}
603+
if attribute.is_empty() {
611604
Err(FormatParseError::EmptyAttribute)
612-
} else if let Ok(index) = index.parse::<usize>() {
613-
Ok(FieldNamePart::Index(index))
614605
} else {
615-
Ok(FieldNamePart::StringIndex(index))
616-
};
606+
Ok(FieldNamePart::Attribute(attribute))
607+
}
617608
}
618-
index.push(ch);
619-
}
620-
Err(FormatParseError::MissingRightBracket)
621-
} else {
622-
Err(FormatParseError::InvalidCharacterAfterRightBracket)
623-
}
609+
'[' => {
610+
let mut index = String::new();
611+
for ch in chars {
612+
if ch == ']' {
613+
return if index.is_empty() {
614+
Err(FormatParseError::EmptyAttribute)
615+
} else if let Ok(index) = index.parse::<usize>() {
616+
Ok(FieldNamePart::Index(index))
617+
} else {
618+
Ok(FieldNamePart::StringIndex(index))
619+
};
620+
}
621+
index.push(ch);
622+
}
623+
Err(FormatParseError::MissingRightBracket)
624+
}
625+
_ => Err(FormatParseError::InvalidCharacterAfterRightBracket),
626+
})
627+
.transpose()
624628
}
625629
}
626630

@@ -633,8 +637,8 @@ pub(crate) enum FieldType {
633637

634638
#[derive(Debug, PartialEq)]
635639
pub(crate) struct FieldName {
636-
pub(crate) field_type: FieldType,
637-
pub(crate) parts: Vec<FieldNamePart>,
640+
pub field_type: FieldType,
641+
pub parts: Vec<FieldNamePart>,
638642
}
639643

640644
impl FieldName {
@@ -654,8 +658,8 @@ impl FieldName {
654658
};
655659

656660
let mut parts = Vec::new();
657-
while chars.peek().is_some() {
658-
parts.push(FieldNamePart::parse_part(&mut chars)?)
661+
while let Some(part) = FieldNamePart::parse_part(&mut chars)? {
662+
parts.push(part)
659663
}
660664

661665
Ok(FieldName { field_type, parts })
@@ -674,7 +678,7 @@ pub(crate) enum FormatPart {
674678

675679
#[derive(Debug, PartialEq)]
676680
pub(crate) struct FormatString {
677-
pub(crate) format_parts: Vec<FormatPart>,
681+
pub format_parts: Vec<FormatPart>,
678682
}
679683

680684
impl FormatString {

0 commit comments

Comments
 (0)