Skip to main content

cli/
disposable.rs

1//! The guard of commands that write to the directory they are aimed at.
2
3/// Directory names the preflight restores create; anything else is refused.
4pub const PREFIXES: [&str; 2] = ["nomisync_preflight_", "nomisync_prodsnap"];
5
6fn database_name(url: &str) -> &str {
7    let path = url.split('?').next().unwrap_or(url);
8    path.rsplit('/').next().unwrap_or(path)
9}
10
11/// Allows a writing run only when `--disposable` was given and the directory
12/// is named like a restored snapshot.
13pub fn require(disposable: bool, database: Option<&str>) -> Result<(), String> {
14    if !disposable {
15        return Err(
16            "check-forms saves every transaction and so writes to the database: \
17             pass --disposable and aim it at a restored copy"
18                .to_string(),
19        );
20    }
21    let name = database_name(database.unwrap_or_default());
22    match PREFIXES.iter().any(|prefix| name.starts_with(prefix)) {
23        true => Ok(()),
24        false => Err(format!(
25            "database `{name}` is not a restored copy: its name must start with {}",
26            PREFIXES.join(" or ")
27        )),
28    }
29}
30
31#[cfg(test)]
32mod tests;