pub fn parse(input: &str) -> Result<Vec<Entry>, ParseError> { if input.trim().isempty()…
- User
pub fn parse(input: &str) -> Result<Vec, ParseError> { if input.trim().is_empty() { return Err(ParseError::String); }
let mut result = Vec::new(); let mut chars = input.chars().peekable();
while chars.peek().is_some() { let key = parse_segment(&mut chars, ';', '='); if key.is_empty() { return Err(ParseError::Key); }
let entry = if chars.peek() == Some(&'=') { chars.next(); let value = parse_segment(&mut chars, ';', '\0'); if value.is_empty() { return Err(ParseError::Value); } Entry::Pair(key, value) } else { Entry::Single(key) }; result.push(entry); chars.next();}
Ok(result) }
fn parse_segment( chars: &mut std::iter::Peekablestd::str::Chars, stop1: char, stop2: char, ) -> String { let mut segment = String::new();
while let Some(&c) = chars.peek() { if c == stop1 || c == stop2 { break; } let c = chars.next().unwrap(); if c == '\n' { segment.push(' '); } else { segment.push(c); } }
String::from(segment.trim()) }
anyway to optimize the code without changing the semantics
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.