Files
stripstream-librarian/crates/core/src/paths.rs
Froidefond Julien 38a0f56328 refactor: Phase A — extraction des helpers partagés et micro-fixes
- Centralise remap_libraries_path/unmap_libraries_path dans crates/core/paths.rs
  (supprime 4 copies dupliquées dans API + indexer)
- Centralise mode_to_interval_minutes/validate_schedule_mode dans crates/core/schedule.rs
  (remplace 8 match blocks + 4 validations inline)
- Ajoute helpers env_or<T>/env_string_or dans config.rs, utilise ThumbnailConfig::default()
  comme base dans from_env() (élimine la duplication des valeurs par défaut)
- Supprime std::mem::take inutile dans books.rs
- Cible #[allow(dead_code)] sur le champ plutôt que le struct (metadata.rs)
- Remplace eprintln! par tracing::warn! dans parsers
- Fix clippy boolean logic bug dans prowlarr.rs

10 nouveaux tests unitaires (paths + schedule)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 11:54:03 +02:00

94 lines
2.9 KiB
Rust

/// Remap a DB path (starting with `/libraries/`) to the local filesystem path
/// using the `LIBRARIES_ROOT_PATH` environment variable.
pub fn remap_libraries_path(path: &str) -> String {
if let Ok(root) = std::env::var("LIBRARIES_ROOT_PATH") {
if path.starts_with("/libraries/") {
return path.replacen("/libraries", &root, 1);
}
}
path.to_string()
}
/// Convert a local filesystem path back to a DB path (starting with `/libraries/`).
pub fn unmap_libraries_path(path: &str) -> String {
if let Ok(root) = std::env::var("LIBRARIES_ROOT_PATH") {
if path.starts_with(&root) {
return path.replacen(&root, "/libraries", 1);
}
}
path.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
// Serialize tests that modify env vars
static ENV_LOCK: Mutex<()> = Mutex::new(());
#[test]
fn remap_with_env_set() {
let _lock = ENV_LOCK.lock().unwrap();
std::env::set_var("LIBRARIES_ROOT_PATH", "/home/user/books");
assert_eq!(
remap_libraries_path("/libraries/BD/les geants/06. yatho.cbz"),
"/home/user/books/BD/les geants/06. yatho.cbz"
);
std::env::remove_var("LIBRARIES_ROOT_PATH");
}
#[test]
fn remap_without_env() {
let _lock = ENV_LOCK.lock().unwrap();
std::env::remove_var("LIBRARIES_ROOT_PATH");
assert_eq!(
remap_libraries_path("/libraries/BD/test.cbz"),
"/libraries/BD/test.cbz"
);
}
#[test]
fn remap_non_matching_prefix() {
let _lock = ENV_LOCK.lock().unwrap();
std::env::set_var("LIBRARIES_ROOT_PATH", "/home/user/books");
assert_eq!(
remap_libraries_path("/other/path/file.cbz"),
"/other/path/file.cbz"
);
std::env::remove_var("LIBRARIES_ROOT_PATH");
}
#[test]
fn unmap_with_env_set() {
let _lock = ENV_LOCK.lock().unwrap();
std::env::set_var("LIBRARIES_ROOT_PATH", "/home/user/books");
assert_eq!(
unmap_libraries_path("/home/user/books/BD/test.cbz"),
"/libraries/BD/test.cbz"
);
std::env::remove_var("LIBRARIES_ROOT_PATH");
}
#[test]
fn unmap_without_env() {
let _lock = ENV_LOCK.lock().unwrap();
std::env::remove_var("LIBRARIES_ROOT_PATH");
assert_eq!(
unmap_libraries_path("/home/user/books/BD/test.cbz"),
"/home/user/books/BD/test.cbz"
);
}
#[test]
fn roundtrip() {
let _lock = ENV_LOCK.lock().unwrap();
std::env::set_var("LIBRARIES_ROOT_PATH", "/mnt/data");
let original = "/libraries/manga/naruto/vol01.cbz";
let remapped = remap_libraries_path(original);
assert_eq!(remapped, "/mnt/data/manga/naruto/vol01.cbz");
assert_eq!(unmap_libraries_path(&remapped), original);
std::env::remove_var("LIBRARIES_ROOT_PATH");
}
}