roezio/config: Don't repeat trim().to_lowercase() on key/section

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-08-29 11:04:40 +02:00
parent 1dbb7b540e
commit cc19b20469

View file

@ -149,12 +149,9 @@ impl<'a> Config<'a> {
fn parse<R: Read + BufRead>(
mut reader: R,
key: &str,
section: &str,
key: String,
section: String,
) -> Result<ConfigParsed, Error> {
let key = key.trim().to_lowercase();
let section = section.trim().to_lowercase();
let mut line = String::new();
let mut clear_offset: usize = 0;
let mut sections_found: Vec<usize> = Vec::new();
@ -166,7 +163,7 @@ impl<'a> Config<'a> {
// EOF
if len == 0 {
if current_section == Some(section.clone()) {
if current_section == Some(section) {
sections_found.push(clear_offset);
}
break;
@ -212,13 +209,11 @@ impl<'a> Config<'a> {
reader: &mut R,
writer: &mut W,
parsed: ConfigParsed,
key: &str,
value: &str,
section: &str,
key: String,
value: String,
section: String,
) -> Result<(), Error> {
let key = key.trim().to_lowercase();
let value = value.trim();
let section = section.trim().to_lowercase();
// Write up to offset value
let mut buffer: Vec<u8> = Vec::new();
@ -269,16 +264,9 @@ impl<'a> Config<'a> {
let mut out_file = fs::File::create(&tempfile)?;
let mut writer = BufWriter::new(&mut out_file);
let parsed = Config::parse(&mut reader, section.as_str(), key.as_str())?;
let parsed = Config::parse(&mut reader, section.clone(), key.clone())?;
reader.seek(SeekFrom::Start(0))?;
Config::write(
&mut reader,
&mut writer,
parsed,
key.as_str(),
value.as_str(),
section.as_str(),
)?;
Config::write(&mut reader, &mut writer, parsed, key, value, section)?;
}
fs::rename(tempfile, self.filename.clone())?;
@ -364,7 +352,8 @@ mod tests {
fn parse_section_and_key_exist() {
let file = String::from("[section1]\n key1= value1\n");
let mut reader = BufReader::new(file.as_bytes());
let parsed = Config::parse(&mut reader, "key1", "section1").unwrap();
let parsed =
Config::parse(&mut reader, String::from("key1"), String::from("section1")).unwrap();
assert_eq!(parsed.section_found, true);
assert_eq!(parsed.key_found, true);
@ -375,7 +364,8 @@ mod tests {
fn parse_section_exists_key_doesnt() {
let file = String::from("[section1]\nkey1 = value1\nkey2 = value2\n");
let mut reader = BufReader::new(file.as_bytes());
let parsed = Config::parse(&mut reader, "key3", "section1").unwrap();
let parsed =
Config::parse(&mut reader, String::from("key3"), String::from("section1")).unwrap();
assert_eq!(parsed.section_found, true);
assert_eq!(parsed.key_found, false);
@ -386,7 +376,8 @@ mod tests {
fn parse_section_doesnt_exists_key_does() {
let file = String::from("[section2]\nkey1 = value1\nkey2 = value2\n");
let mut reader = BufReader::new(file.as_bytes());
let parsed = Config::parse(&mut reader, "key1", "section1").unwrap();
let parsed =
Config::parse(&mut reader, String::from("key1"), String::from("section1")).unwrap();
assert_eq!(parsed.section_found, false);
assert_eq!(parsed.key_found, false);
@ -417,7 +408,8 @@ mod tests {
)
.unwrap();
let mut reader = BufReader::new(file.as_bytes());
let parsed = Config::parse(&mut reader, "key3", "section1").unwrap();
let parsed =
Config::parse(&mut reader, String::from("key3"), String::from("section1")).unwrap();
assert_eq!(parsed.section_found, true);
assert_eq!(parsed.key_found, true);
@ -444,7 +436,8 @@ mod tests {
)
.unwrap();
let mut reader = BufReader::new(file.as_bytes());
let parsed = Config::parse(&mut reader, "key2", "section1").unwrap();
let parsed =
Config::parse(&mut reader, String::from("key2"), String::from("section1")).unwrap();
assert_eq!(parsed.section_found, true);
assert_eq!(parsed.key_found, true);
@ -472,7 +465,8 @@ mod tests {
)
.unwrap();
let mut reader = BufReader::new(file.as_bytes());
let parsed = Config::parse(&mut reader, "key3", "section1").unwrap();
let parsed =
Config::parse(&mut reader, String::from("key3"), String::from("section1")).unwrap();
assert_eq!(parsed.section_found, true);
assert_eq!(parsed.key_found, false);
@ -495,9 +489,9 @@ mod tests {
&mut reader,
&mut writer,
parsed,
"key1",
"value1",
"section1",
String::from("key1"),
String::from("value1"),
String::from("section1"),
)
.unwrap();
assert_eq!(writer, after.as_bytes());
@ -519,9 +513,9 @@ mod tests {
&mut reader,
&mut writer,
parsed,
"key1",
"value1",
"section1",
String::from("key1"),
String::from("value1"),
String::from("section1"),
)
.unwrap();
assert_eq!(writer, after.as_bytes());
@ -543,9 +537,9 @@ mod tests {
&mut reader,
&mut writer,
parsed,
"key1",
"value1",
"section1",
String::from("key1"),
String::from("value1"),
String::from("section1"),
)
.unwrap();
assert_eq!(writer, after.as_bytes());
@ -567,9 +561,9 @@ mod tests {
&mut reader,
&mut writer,
parsed,
"key1",
"value1",
"section1",
String::from("key1"),
String::from("value1"),
String::from("section1"),
)
.unwrap();
assert_eq!(writer, after.as_bytes());