Lines
100 %
Functions
66.67 %
Branches
//! The guard of commands that write to the directory they are aimed at.
/// Directory names the preflight restores create; anything else is refused.
pub const PREFIXES: [&str; 2] = ["nomisync_preflight_", "nomisync_prodsnap"];
fn database_name(url: &str) -> &str {
let path = url.split('?').next().unwrap_or(url);
path.rsplit('/').next().unwrap_or(path)
}
/// Allows a writing run only when `--disposable` was given and the directory
/// is named like a restored snapshot.
pub fn require(disposable: bool, database: Option<&str>) -> Result<(), String> {
if !disposable {
return Err(
"check-forms saves every transaction and so writes to the database: \
pass --disposable and aim it at a restored copy"
.to_string(),
);
let name = database_name(database.unwrap_or_default());
match PREFIXES.iter().any(|prefix| name.starts_with(prefix)) {
true => Ok(()),
false => Err(format!(
"database `{name}` is not a restored copy: its name must start with {}",
PREFIXES.join(" or ")
)),
#[cfg(test)]
mod tests;