diff options
author | metamuffin <metamuffin@disroot.org> | 2024-11-22 17:34:55 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-11-22 17:56:32 +0100 |
commit | 1e4cee8c2ebfc1bd6aa7df140e05378b9ec7ede0 (patch) | |
tree | 08e54a6586fbb778c54ccc36b069b8cbd5fb610d | |
parent | ebdc9adf6d1096a78e93f5940175904c27953ff9 (diff) | |
download | hurrycurry-1e4cee8c2ebfc1bd6aa7df140e05378b9ec7ede0.tar hurrycurry-1e4cee8c2ebfc1bd6aa7df140e05378b9ec7ede0.tar.bz2 hurrycurry-1e4cee8c2ebfc1bd6aa7df140e05378b9ec7ede0.tar.zst |
improved error handling in localetool
-rw-r--r-- | locale/tools/src/main.rs | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/locale/tools/src/main.rs b/locale/tools/src/main.rs index 0173138c..a6232dda 100644 --- a/locale/tools/src/main.rs +++ b/locale/tools/src/main.rs @@ -102,26 +102,22 @@ msgstr "" {}"#, inputs .first() - .expect("at least one input required") + .ok_or(anyhow!("at least one input required"))? .file_stem() - .unwrap() + .ok_or(anyhow!("file name empty"))? .to_string_lossy(), ini.into_iter() - .fold(String::new(), |mut a, (mut key, value)| { + .try_fold(String::new(), |mut a, (mut key, value)| { if let Some(id_map) = &id_map { if let Some(new_id) = id_map.get(&key) { key = new_id.to_owned() } } - writeln!( - a, - "msgid {}\nmsgstr {}\n\n", - serde_json::to_string(&key).unwrap(), - serde_json::to_string(&value).unwrap(), - ) - .unwrap(); - a - }) + let key = serde_json::to_string(&key)?; + let value = serde_json::to_string(&value)?; + writeln!(a, "msgid {key}\nmsgstr {value}\n\n",)?; + Ok::<_, anyhow::Error>(a) + })? ) .as_bytes(), )?; @@ -135,7 +131,11 @@ msgstr "" .and_then(|e| { if e.file_name().to_string_lossy().ends_with(".ini") { Ok(Some(( - e.path().file_stem().unwrap().to_str().unwrap().to_string(), + e.path() + .file_stem() + .ok_or(anyhow!("empty filename"))? + .to_string_lossy() + .to_string(), load_ini(&e.path())?, ))) } else { @@ -157,18 +157,17 @@ msgstr "" File::create(output)?.write_all( tr_tr .into_iter() - .fold(format!("id,{}\n", langs.join(",")), |mut a, (k, v)| { + .try_fold(format!("id,{}\n", langs.join(",")), |mut a, (k, v)| { writeln!( a, "{k},{}", v.values() - .map(|s| serde_json::to_string(s).unwrap()) - .collect::<Vec<_>>() + .map(|s| serde_json::to_string(s)) + .try_collect::<Vec<_>>()? .join(",") - ) - .unwrap(); - a - }) + )?; + Ok::<_, anyhow::Error>(a) + })? .as_bytes(), )?; Ok(()) @@ -235,10 +234,10 @@ msgstr "" File::create(output)?.write_all( outmap .into_iter() - .fold("[hurrycurry]\n".to_string(), |mut a, (k, v)| { - writeln!(a, "{k}={v}").unwrap(); - a - }) + .try_fold("[hurrycurry]\n".to_string(), |mut a, (k, v)| { + writeln!(a, "{k}={v}")?; + Ok::<_, anyhow::Error>(a) + })? .as_bytes(), )?; @@ -293,10 +292,10 @@ msgstr "" File::create(output)?.write_all( output_unflip .into_iter() - .fold("[hurrycurry]\n".to_string(), |mut a, (k, v)| { - writeln!(a, "{k}={v}").unwrap(); - a - }) + .try_fold("[hurrycurry]\n".to_string(), |mut a, (k, v)| { + writeln!(a, "{k}={v}")?; + Ok::<_, anyhow::Error>(a) + })? .as_bytes(), )?; |