mirror of
https://github.com/Yuyi-Oak/BlueArchiveToolkit.git
synced 2026-09-18 08:54:55 +08:00
@@ -166,6 +166,37 @@ pub fn set_translation(
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
/// Reads one translation entry from a workbench.
|
||||
pub fn get_translation_entry(
|
||||
workbench_path: &Path,
|
||||
entry_id: &str,
|
||||
) -> anyhow::Result<TranslationWorkbenchEntry> {
|
||||
let workbench = read_translation_workbench(workbench_path)?;
|
||||
workbench
|
||||
.entries
|
||||
.into_iter()
|
||||
.find(|entry| entry.id == entry_id)
|
||||
.ok_or_else(|| anyhow::anyhow!("翻译工作台中不存在 TextUnit:{entry_id}"))
|
||||
}
|
||||
|
||||
/// Clears one reviewed translation and writes the workbench atomically.
|
||||
pub fn unset_translation(
|
||||
workbench_path: &Path,
|
||||
entry_id: &str,
|
||||
) -> anyhow::Result<TranslationWorkbenchEntry> {
|
||||
let mut workbench = read_translation_workbench(workbench_path)?;
|
||||
let entry = workbench
|
||||
.entries
|
||||
.iter_mut()
|
||||
.find(|entry| entry.id == entry_id)
|
||||
.ok_or_else(|| anyhow::anyhow!("翻译工作台中不存在 TextUnit:{entry_id}"))?;
|
||||
entry.translated_text = None;
|
||||
let updated = entry.clone();
|
||||
workbench.generated_unix_seconds = unix_seconds_now();
|
||||
write_translation_workbench(workbench_path, &workbench)?;
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
/// Validates a workbench against the current official TextUnit index.
|
||||
///
|
||||
/// This checks the release identity and every stored source/target location
|
||||
@@ -656,6 +687,23 @@ mod tests {
|
||||
assert!(error.to_string().contains("不存在 TextUnit"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translation_get_and_unset_round_trip_atomically() {
|
||||
let temp = tempfile::TempDir::new().unwrap();
|
||||
let path = temp.path().join("workbench.json");
|
||||
let mut workbench = workbench(temp.path());
|
||||
workbench.entries[0].translated_text = Some("译文".to_string());
|
||||
write_translation_workbench(&path, &workbench).unwrap();
|
||||
|
||||
let entry = get_translation_entry(&path, "unit-1").unwrap();
|
||||
assert_eq!(entry.translated_text.as_deref(), Some("译文"));
|
||||
|
||||
let updated = unset_translation(&path, "unit-1").unwrap();
|
||||
assert_eq!(updated.translated_text, None);
|
||||
let loaded = read_translation_workbench(&path).unwrap();
|
||||
assert_eq!(loaded.entries[0].translated_text, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validation_reports_publishable_and_unreviewed_entries() {
|
||||
let temp = tempfile::TempDir::new().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user