use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use crate::config::Config;
use crate::config_resolve::ConfigTier;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TierSource {
pub tier: &'static str,
pub path: Option<PathBuf>,
pub text: Option<String>,
pub from_file: bool,
}
#[derive(Debug, Clone)]
pub struct ConfigSources {
pub prepared_bind_snapshot_id: Option<u64>,
pub approved_user: Option<crate::subc_config::ApprovedUser>,
pub user: TierSource,
pub project: TierSource,
pub connected: Config,
pub owns_user_watch: bool,
}
impl ConfigSources {
pub fn from_configure(
user_config_path: Option<PathBuf>,
project_root: &Path,
tiers: &[ConfigTier],
connected: &Config,
) -> Self {
let project_path =
crate::subc_config::project_config_read_path(project_root, connected.harness.as_ref());
Self::from_observed_paths(user_config_path, project_path, tiers, connected)
}
pub(crate) fn from_observed_paths(
user_config_path: Option<PathBuf>,
project_path: PathBuf,
tiers: &[ConfigTier],
connected: &Config,
) -> Self {
let owns_user_watch = user_config_path.is_some() && process_user_config_path().is_none();
let user_path = user_config_path.or_else(process_user_config_path);
let source_for = |tier: &'static str, path: Option<PathBuf>| {
let used = tiers.iter().find(|candidate| candidate.tier == tier);
let from_file = match (used, path.as_ref()) {
(Some(used), Some(path)) => Path::new(&used.source) == path.as_path(),
(None, _) => true,
(Some(_), None) => false,
};
TierSource {
tier,
path,
text: used.map(|used| used.doc.clone()),
from_file,
}
};
Self {
prepared_bind_snapshot_id: None,
approved_user: None,
user: source_for("user", user_path),
project: source_for("project", Some(project_path)),
connected: connected.clone(),
owns_user_watch,
}
}
}
static PROCESS_USER_CONFIG_PATH: OnceLock<PathBuf> = OnceLock::new();
pub fn set_process_user_config_path(path: PathBuf) {
let _ = PROCESS_USER_CONFIG_PATH.set(path);
}
fn process_user_config_path() -> Option<PathBuf> {
PROCESS_USER_CONFIG_PATH.get().cloned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn observed_sources_keep_exact_paths_and_distinguish_relayed_text() {
let user = PathBuf::from("user.jsonc");
let project = PathBuf::from("project.jsonc");
let config = Config::default();
let tiers = vec![
ConfigTier {
tier: "user".into(),
source: "user.jsonc".into(),
doc: "{}".into(),
},
ConfigTier {
tier: "project".into(),
source: "wire".into(),
doc: "{ }".into(),
},
];
let sources = ConfigSources::from_observed_paths(
Some(user.clone()),
project.clone(),
&tiers,
&config,
);
assert_eq!(sources.user.path, Some(user));
assert_eq!(sources.project.path, Some(project));
assert_eq!(sources.user.text.as_deref(), Some("{}"));
assert_eq!(sources.project.text.as_deref(), Some("{ }"));
assert!(sources.user.from_file);
assert!(!sources.project.from_file);
assert_eq!(sources.prepared_bind_snapshot_id, None);
}
#[test]
fn absent_tiers_record_absent_text_as_file_backed() {
let sources = ConfigSources::from_observed_paths(
Some(PathBuf::from("user.jsonc")),
PathBuf::from("project.jsonc"),
&[],
&Config::default(),
);
assert_eq!(sources.user.text, None);
assert_eq!(sources.project.text, None);
assert!(sources.user.from_file);
assert!(sources.project.from_file);
}
}