diff --git a/crates/summit-core/src/profiles.rs b/crates/summit-core/src/profiles.rs index bf25e57..1c8cd46 100644 --- a/crates/summit-core/src/profiles.rs +++ b/crates/summit-core/src/profiles.rs @@ -77,6 +77,29 @@ pub fn slugify(name: &str) -> String { s.trim_end_matches('-').to_string() } +/// Profile files lying in `dirs` (not below them): every `.toml` that reads as a profile and has a `name`, with that +/// name. What "Import" offers without a file dialog. +pub fn importable(dirs: &[PathBuf]) -> Vec<(PathBuf, String)> { + let mut v: Vec<(PathBuf, String)> = vec![]; + for d in dirs { + for e in std::fs::read_dir(d).into_iter().flatten().flatten().take(2000) { + let p = e.path(); + if p.extension().and_then(|x| x.to_str()) != Some("toml") || std::fs::metadata(&p).map(|m| m.len() > 1 << 20).unwrap_or(true) { continue; } + let Ok(text) = std::fs::read_to_string(&p) else { continue }; + let Ok(l) = profile::parse_layer(&text) else { continue }; + if l.version.is_some_and(|x| x > LAYER_VERSION) { continue; } + if let Some(name) = l.name.filter(|n| !n.trim().is_empty()) { if !v.iter().any(|(q, _)| *q == p) { v.push((p, name)); } } + } + } + v.sort(); + v +} + +/// Where exported profiles are looked for: Documents, Downloads and the home folder. +pub fn exchange_dirs() -> Vec { + [dirs::document_dir(), dirs::download_dir(), dirs::home_dir()].into_iter().flatten().filter(|d| d.is_dir()).collect() +} + /// A built-in profile's settings: the preset's values, as a layer. pub fn builtin_layer(p: &crate::presets::Preset) -> Layer { let mut l = Layer::default(); @@ -432,6 +455,14 @@ impl Store { self.create(&name, &l) } + /// Where an exported profile is put when no place is given: the user's Documents folder (their home without one). + pub fn export_path(&self, r: &Ref) -> Result { + let s = self.get(r)?; + let dir = dirs::document_dir().filter(|d| d.is_dir()).or_else(dirs::home_dir).context("no home folder")?; + let base = match r { Ref::User(slug) => slug.clone(), _ => slugify(&s.name) }; + Ok(dir.join(format!("summit-profile-{base}.toml"))) + } + /// Bring the files of an older Summit up to this one, once (`profiles.toml` says it was done). Before anything is /// written, `profiles/` is copied to `backups/profiles-v2-