use std::path::{Path, PathBuf};
use super::text::say;
use crate::remote::selection::Selection;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Pinned {
pub flags: Vec<String>,
pub identity: Option<String>,
}
pub fn pin(selected: &Selection) -> Pinned {
let word = |flag: &str, value: String| vec![flag.to_string(), value];
match selected {
Selection::Profile(profile) => Pinned {
flags: word("--profile", profile.name.clone()),
identity: Some(identity(profile)),
},
Selection::File(root) => Pinned {
flags: word("--root", absolute(root).display().to_string()),
identity: None,
},
Selection::Remote { url, .. } => Pinned {
flags: word("--url", url.clone()),
identity: None,
},
Selection::Postgres { schema, .. } => Pinned {
flags: word("--schema", schema.clone()),
identity: None,
},
}
}
fn absolute(path: &Path) -> PathBuf {
std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf())
}
pub fn identity(profile: &crate::profile::resolve::Resolved) -> String {
let endpoints: std::collections::BTreeMap<&String, _> = profile
.bindings
.iter()
.map(|(source, binding)| (source, &binding.endpoint))
.collect();
serde_json::json!({
"profile": profile.name,
"workspace": profile.profile.workspace_id,
"endpoints": endpoints,
"mounts": profile.profile.mounts,
"history": profile.profile.history,
})
.to_string()
}
pub fn digest(identity: &str) -> String {
crate::clients::receipt::sha256(identity.as_bytes())
}
pub fn expected(selected: &Selection, wanted: Option<&str>) -> Result<(), String> {
let Some(wanted) = wanted else {
return Ok(());
};
match selected {
Selection::Profile(profile) if digest(&identity(profile)) == wanted => Ok(()),
Selection::Profile(profile) => {
Err(say("child_store_changed", &[("profile", &profile.name)]))
}
_ => Err(say("child_store_changed", &[("profile", "(none)")])),
}
}
pub fn home() -> Option<PathBuf> {
crate::home::home().ok().map(|home| absolute(&home))
}
pub fn drifted(flags: &[String], identity: Option<&str>, directory: &Path) -> Result<(), String> {
let named = flags
.chunks(2)
.find(|pair| pair.len() == 2 && pair[0] == "--profile")
.map(|pair| pair[1].as_str());
let (Some(name), Some(identity)) = (named, identity) else {
return Ok(());
};
let now = crate::profile::resolve::selected(Some(name), directory)
.ok()
.flatten()
.map(|profile| self::identity(&profile));
if now.as_deref() == Some(identity) {
return Ok(());
}
Err(say("store_drifted", &[("profile", name)]))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_backend_is_named_once_and_postgres_gets_no_root() {
let postgres = Selection::Postgres {
dsn: "postgresql://x".to_string(),
schema: "neosian".to_string(),
};
assert_eq!(pin(&postgres).flags, ["--schema", "neosian"]);
let remote = Selection::Remote {
url: "https://h".to_string(),
token: "t".to_string(),
};
assert_eq!(pin(&remote).flags, ["--url", "https://h"]);
let file = pin(&Selection::File(PathBuf::from("relative/home")));
assert_eq!(file.flags[0], "--root");
assert!(Path::new(&file.flags[1]).is_absolute());
for pinned in [pin(&postgres), pin(&remote), file] {
assert_eq!(pinned.flags.len(), 2);
assert!(pinned.identity.is_none());
}
}
#[test]
fn a_selection_without_a_profile_never_drifts() {
let flags = ["--root".to_string(), "/r".to_string()];
assert_eq!(drifted(&flags, None, Path::new("/")), Ok(()));
}
}