Below is one way to refactor the two parse functions to reduce duplication and leverage iterator combinators for clarity. In this refactoring, we create a small helper function to normalize the input string (by replacing newlines with spaces and trimming) and then use iterator methods to split the normalized input on semicolons and verify each segment. Note that the two functions still return different error types, so the empty–input check remains specific for each function.
use std::error::Error;
use std::fmt;
use super::Entry;
// -----------------------------------------------------------------------------
// Error definitions
// -----------------------------------------------------------------------------
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub enum EntryParseError {
Key,
Value,
}
impl fmt::Display for EntryParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use EntryParseError::*;
match self {
Key => write!(f, "Key cannot be empty"),
Value => write!(f, "Value cannot be empty"),
}
}
}
impl Error for EntryParseError {}
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct BlackListParseError;
impl fmt::Display for BlackListParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Pattern cannot be empty")
}
}
impl Error for BlackListParseError {}
// -----------------------------------------------------------------------------
// Helper function for normalization
// -----------------------------------------------------------------------------
fn normalize(input: &str) -> String {
input.replace('\n', " ").trim().to_string()
}
// -----------------------------------------------------------------------------
// Refactored parse_entries function
// -----------------------------------------------------------------------------
pub fn parse_entries(input: &str) -> Result<Vec<Entry>, EntryParseError> {
let normalized = normalize(input);
if normalized.is_empty() {
// We treat an entirely empty input as missing key.
return Err(EntryParseError::Key);
}
normalized
.split(';')
.map(|segment| {
let seg = segment.trim();
if seg.is_empty() {
return Err(EntryParseError::Key);
}
if let Some(eq_pos) = seg.find('=') {
let key = seg[..eq_pos].trim();
let val = seg[eq_pos + 1..].trim();
if key.is_empty() {
return Err(EntryParseError::Key);
}
if val.is_empty() {
return Err(EntryParseError::Value);
}
Ok(Entry::Pair(key.to_string(), val.to_string()))
} else {
Ok(Entry::Single(seg.to_string()))
}
})
.collect()
}
// -----------------------------------------------------------------------------
// Refactored parse_exclude_pattern function
// -----------------------------------------------------------------------------
pub fn parse_exclude_pattern(input: &str) -> Result<Vec<String>, BlackListParseError> {
let normalized = normalize(input);
if normalized.is_empty() {
return Err(BlackListParseError);
}
normalized
.split(';')
.map(|segment| {
let seg = segment.trim();
if seg.is_empty() {
return Err(BlackListParseError);
}
Ok(seg.to_string())
})
.collect()
}