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.
4
pub const PREFIXES: [&str; 2] = ["nomisync_preflight_", "nomisync_prodsnap"];
5

            
6
4
fn database_name(url: &str) -> &str {
7
4
    let path = url.split('?').next().unwrap_or(url);
8
4
    path.rsplit('/').next().unwrap_or(path)
9
4
}
10

            
11
/// Allows a writing run only when `--disposable` was given and the directory
12
/// is named like a restored snapshot.
13
6
pub fn require(disposable: bool, database: Option<&str>) -> Result<(), String> {
14
6
    if !disposable {
15
2
        return Err(
16
2
            "check-forms saves every transaction and so writes to the database: \
17
2
             pass --disposable and aim it at a restored copy"
18
2
                .to_string(),
19
2
        );
20
4
    }
21
4
    let name = database_name(database.unwrap_or_default());
22
7
    match PREFIXES.iter().any(|prefix| name.starts_with(prefix)) {
23
2
        true => Ok(()),
24
2
        false => Err(format!(
25
2
            "database `{name}` is not a restored copy: its name must start with {}",
26
2
            PREFIXES.join(" or ")
27
2
        )),
28
    }
29
6
}
30

            
31
#[cfg(test)]
32
mod tests;