use std::path::{Path, PathBuf};
use indexmap::IndexMap;
use sapphire_workspace::AppContext;
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
pub const SELECTION_FILE: &str = "workspace.toml";
const LEGACY_LIST_FILE: &str = "workspaces.toml";
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Selection {
pub root: PathBuf,
#[serde(default)]
pub sync: bool,
}
#[derive(Clone, Debug)]
pub struct SelectionFile {
path: PathBuf,
}
impl SelectionFile {
pub fn for_app(ctx: &AppContext) -> SelectionFile {
SelectionFile::at(ctx.config_dir().join(SELECTION_FILE))
}
pub fn at(path: PathBuf) -> SelectionFile {
SelectionFile { path }
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn load(&self, app_name: &str) -> Result<Option<Selection>> {
match std::fs::read_to_string(&self.path) {
Ok(text) if text.trim().is_empty() => Ok(None),
Ok(text) => toml::from_str(&text)
.map(Some)
.map_err(|e| Error::Config(format!("{}: {e}", self.path.display()))),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
let legacy = self.path.with_file_name(LEGACY_LIST_FILE);
let migrated = migrate(&legacy, app_name)?;
if let Some(selection) = &migrated {
tracing::info!(
root = %selection.root.display(),
"one workspace per server now; {} becomes this server's workspace",
selection.root.display()
);
self.save(selection)?;
}
Ok(migrated)
}
Err(e) => Err(Error::Io(e)),
}
}
pub fn save(&self, selection: &Selection) -> Result<()> {
let text = toml::to_string_pretty(selection)
.map_err(|e| Error::Config(format!("{}: {e}", self.path.display())))?;
self.write(&text)
}
pub fn clear(&self) -> Result<()> {
self.write("")
}
fn write(&self, text: &str) -> Result<()> {
if let Some(dir) = self.path.parent() {
std::fs::create_dir_all(dir).map_err(Error::Io)?;
}
let tmp = self.path.with_extension("toml.tmp");
std::fs::write(&tmp, text).map_err(Error::Io)?;
std::fs::rename(&tmp, &self.path).map_err(Error::Io)
}
}
fn migrate(legacy: &Path, app_name: &str) -> Result<Option<Selection>> {
#[derive(Deserialize)]
struct Entry {
root: PathBuf,
#[serde(default)]
synced: bool,
}
#[derive(Default, Deserialize)]
struct List {
#[serde(default)]
workspace: IndexMap<String, Entry>,
}
let list: List = match std::fs::read_to_string(legacy) {
Ok(text) => toml::from_str(&text)
.map_err(|e| Error::Config(format!("{}: {e}", legacy.display())))?,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(Error::Io(e)),
};
let marker = format!(".{app_name}");
let entries: Vec<Entry> = list.workspace.into_values().collect();
let chosen = entries
.iter()
.find(|e| e.synced)
.or_else(|| entries.iter().find(|e| e.root.join(&marker).is_dir()));
Ok(chosen.map(|e| Selection {
root: e.root.clone(),
sync: e.synced,
}))
}
pub(crate) fn slug(root: &Path) -> String {
let base: String = root
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| "workspace".to_owned())
.trim()
.to_lowercase()
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect();
let base = base.trim_matches('-').to_owned();
if base.is_empty() {
"workspace".to_owned()
} else {
base
}
}
#[cfg(test)]
mod tests {
use super::*;
fn file(tmp: &tempfile::TempDir) -> SelectionFile {
SelectionFile::at(tmp.path().join("config").join(SELECTION_FILE))
}
#[test]
fn no_file_is_no_selection_and_a_saved_one_reads_back() {
let tmp = tempfile::tempdir().unwrap();
let f = file(&tmp);
assert_eq!(f.load("app").unwrap(), None);
let s = Selection {
root: PathBuf::from("/x/notes"),
sync: true,
};
f.save(&s).unwrap();
assert_eq!(f.load("app").unwrap(), Some(s));
}
#[test]
fn a_corrupt_file_is_an_error() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join("config")).unwrap();
std::fs::write(tmp.path().join("config").join(SELECTION_FILE), "root = [").unwrap();
assert!(file(&tmp).load("app").is_err());
}
#[test]
fn the_old_list_migrates_to_its_synced_workspace() {
let tmp = tempfile::tempdir().unwrap();
let config = tmp.path().join("config");
std::fs::create_dir_all(&config).unwrap();
std::fs::write(
config.join(LEGACY_LIST_FILE),
"[workspace.a]\nroot = \"/x/a\"\n\n[workspace.b]\nroot = \"/x/b\"\nsynced = true\n",
)
.unwrap();
let f = file(&tmp);
let s = f.load("app").unwrap().unwrap();
assert_eq!(s.root, PathBuf::from("/x/b"));
assert!(s.sync);
assert!(config.join(SELECTION_FILE).is_file(), "written down");
}
#[test]
fn a_cleared_selection_stays_cleared_beside_the_old_list() {
let tmp = tempfile::tempdir().unwrap();
let config = tmp.path().join("config");
std::fs::create_dir_all(&config).unwrap();
std::fs::write(
config.join(LEGACY_LIST_FILE),
"[workspace.a]\nroot = \"/x/a\"\nsynced = true\n",
)
.unwrap();
let f = file(&tmp);
assert!(f.load("app").unwrap().is_some(), "migrated");
f.clear().unwrap();
assert_eq!(f.load("app").unwrap(), None, "not migrated again");
}
#[test]
fn without_a_synced_one_the_first_reachable_workspace_migrates() {
let tmp = tempfile::tempdir().unwrap();
let config = tmp.path().join("config");
std::fs::create_dir_all(&config).unwrap();
let here = tmp.path().join("here");
std::fs::create_dir_all(here.join(".app")).unwrap();
std::fs::write(
config.join(LEGACY_LIST_FILE),
format!(
"[workspace.gone]\nroot = \"/x/gone\"\n\n[workspace.here]\nroot = {:?}\n",
here.display().to_string()
),
)
.unwrap();
let s = file(&tmp).load("app").unwrap().unwrap();
assert_eq!(s.root, here);
assert!(!s.sync);
}
#[test]
fn slugs_are_lowercase_dashed_names() {
assert_eq!(slug(Path::new("/x/My Notes!")), "my-notes");
assert_eq!(slug(Path::new("/")), "workspace");
}
}