Skip to content

Commit dc848d1

Browse files
committed
refactor: split off the parse_inner_type function from parse_inner_types
1 parent 9fdab6c commit dc848d1

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

types/src/data_types.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -663,15 +663,39 @@ fn parse_variant(input: &str) -> Result<DataTypeNode, TypesError> {
663663
/// let input1 = "Tuple(Enum8('f\'()' = 1))"; // the result is `f\'()`
664664
/// let input2 = "Tuple(Enum8('(' = 1))"; // the result is `(`
665665
/// ```
666-
fn parse_inner_types(input: &str) -> Result<Vec<DataTypeNode>, TypesError> {
666+
fn parse_inner_types(mut input: &str) -> Result<Vec<DataTypeNode>, TypesError> {
667667
let mut inner_types: Vec<DataTypeNode> = Vec::new();
668668

669+
while !input.is_empty() {
670+
inner_types.push(parse_inner_type(&mut input)?);
671+
672+
if !input.starts_with(',') {
673+
break;
674+
}
675+
676+
input = &input[1..];
677+
678+
if input.starts_with(' ') {
679+
input = &input[1..];
680+
}
681+
}
682+
683+
if !input.is_empty() {
684+
return Err(TypesError::TypeParsingError(format!(
685+
"Failed to parse all inner types, remaining input: {input}"
686+
)));
687+
}
688+
689+
Ok(inner_types)
690+
}
691+
692+
/// Parses a single type from the input string and removes it from the input.
693+
fn parse_inner_type(input: &mut &str) -> Result<DataTypeNode, TypesError> {
669694
let input_bytes = input.as_bytes();
670695

671696
let mut open_parens = 0;
672697
let mut quote_open = false;
673698
let mut char_escaped = false;
674-
let mut last_element_index = 0;
675699

676700
let mut i = 0;
677701
while i < input_bytes.len() {
@@ -687,42 +711,20 @@ fn parse_inner_types(input: &str) -> Result<Vec<DataTypeNode>, TypesError> {
687711
} else if input_bytes[i] == b')' {
688712
open_parens -= 1;
689713
} else if input_bytes[i] == b',' && open_parens == 0 {
690-
let data_type_str = String::from_utf8(input_bytes[last_element_index..i].to_vec())
691-
.map_err(|_| {
692-
TypesError::TypeParsingError(format!(
693-
"Invalid UTF-8 sequence in input for the inner data type: {}",
694-
&input[last_element_index..]
695-
))
696-
})?;
697-
let data_type = DataTypeNode::new(&data_type_str)?;
698-
inner_types.push(data_type);
699-
// Skip ', ' (comma and space)
700-
if i + 2 <= input_bytes.len() && input_bytes[i + 1] == b' ' {
701-
i += 2;
702-
} else {
703-
i += 1;
704-
}
705-
last_element_index = i;
706-
continue; // Skip the normal increment at the end of the loop
714+
break;
707715
}
708716
}
709717
i += 1;
710718
}
711719

712-
// Push the remaining part of the type if it seems to be valid (at least all parentheses are closed)
713-
if open_parens == 0 && last_element_index < input_bytes.len() {
714-
let data_type_str =
715-
String::from_utf8(input_bytes[last_element_index..].to_vec()).map_err(|_| {
716-
TypesError::TypeParsingError(format!(
717-
"Invalid UTF-8 sequence in input for the inner data type: {}",
718-
&input[last_element_index..]
719-
))
720-
})?;
721-
let data_type = DataTypeNode::new(&data_type_str)?;
722-
inner_types.push(data_type);
723-
}
724-
725-
Ok(inner_types)
720+
let data_type_str = String::from_utf8(input_bytes[..i].to_vec()).map_err(|_| {
721+
TypesError::TypeParsingError(format!(
722+
"Invalid UTF-8 sequence in input for the inner data type: {}",
723+
&input[..i]
724+
))
725+
})?;
726+
*input = &input[i..];
727+
DataTypeNode::new(&data_type_str)
726728
}
727729

728730
#[inline]

0 commit comments

Comments
 (0)