bookmarks_core/
toml_storage.rs1use anyhow::{Context, Result};
2use std::fs;
3use std::path::{Path, PathBuf};
4
5use crate::config::{Config, DEFAULT_CONFIG};
6use crate::storage::Storage;
7
8const CONFIG_DIR: &str = ".config";
9const APP_NAME: &str = "bookmarks";
10const CONFIG_FILENAME: &str = "bookmarks.toml";
11
12pub struct TomlStorage {
13 path: PathBuf,
14}
15
16impl TomlStorage {
17 pub fn new(path: PathBuf) -> Self {
18 Self { path }
19 }
20
21 pub fn default_path() -> Result<PathBuf> {
23 let home = dirs::home_dir().context("Failed to get home directory")?;
27 Ok(home.join(CONFIG_DIR).join(APP_NAME).join(CONFIG_FILENAME))
28 }
29
30 pub fn cwd_path() -> Option<PathBuf> {
32 std::env::current_dir()
33 .ok()
34 .map(|d| d.join(CONFIG_FILENAME))
35 }
36
37 pub fn with_default_path() -> Result<Self> {
38 Ok(Self::new(Self::default_path()?))
39 }
40}
41
42impl Storage for TomlStorage {
43 fn load(&self) -> Result<Config> {
44 let contents = fs::read_to_string(&self.path).context("Failed to read config file")?;
45 let config: Config = toml::from_str(&contents).context("Failed to parse config file")?;
46
47 for warning in config.validate() {
48 eprintln!("[bookmarks] warning: {warning}");
49 }
50
51 Ok(config)
52 }
53
54 fn save(&self, config: &Config) -> Result<()> {
55 let contents = toml::to_string(config).context("Failed to serialize config")?;
56 fs::write(&self.path, contents).context("Failed to write config file")?;
57 Ok(())
58 }
59
60 fn init(&self) -> Result<()> {
61 if !self.path.exists() {
62 let config_dir = self
63 .path
64 .parent()
65 .context("Invalid config path: no parent directory")?;
66 fs::create_dir_all(config_dir).context("Failed to create config directory")?;
67 fs::write(&self.path, DEFAULT_CONFIG).context("Failed to write default config")?;
68 }
69 Ok(())
70 }
71
72 fn backend_name(&self) -> &str {
73 "toml"
74 }
75
76 fn path(&self) -> Option<&Path> {
77 Some(&self.path)
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84 use std::io::Write;
85
86 #[test]
87 fn test_default_path() {
88 let path = TomlStorage::default_path().unwrap();
89 assert!(path.ends_with(".config/bookmarks/bookmarks.toml"));
90 }
91
92 #[test]
93 fn test_load_save_roundtrip() {
94 let dir = tempfile::tempdir().unwrap();
95 let path = dir.path().join("bookmarks.toml");
96
97 let storage = TomlStorage::new(path.clone());
98
99 let mut f = fs::File::create(&path).unwrap();
101 writeln!(
102 f,
103 r#"[aliases]
104gh = "github"
105
106[links]
107github = "https://github.com"
108
109[groups]
110dev = ["gh"]
111"#
112 )
113 .unwrap();
114
115 let config = storage.load().unwrap();
116 assert_eq!(config.aliases.get("gh"), Some(&"github".to_string()));
117
118 storage.save(&config).unwrap();
120 let reloaded = storage.load().unwrap();
121 assert_eq!(config.aliases, reloaded.aliases);
122 assert_eq!(config.links, reloaded.links);
123 assert_eq!(config.groups, reloaded.groups);
124 }
125
126 #[test]
127 fn test_init_creates_default_config() {
128 let dir = tempfile::tempdir().unwrap();
129 let path = dir.path().join("sub").join("bookmarks.toml");
130
131 let storage = TomlStorage::new(path.clone());
132 storage.init().unwrap();
133
134 assert!(path.exists());
135 let config = storage.load().unwrap();
136 assert!(!config.links.is_empty());
137 }
138
139 #[test]
140 fn test_init_does_not_overwrite() {
141 let dir = tempfile::tempdir().unwrap();
142 let path = dir.path().join("bookmarks.toml");
143
144 fs::write(&path, "[links]\nrust = \"https://rust-lang.org\"\n").unwrap();
145
146 let storage = TomlStorage::new(path);
147 storage.init().unwrap();
148
149 let config = storage.load().unwrap();
150 assert_eq!(
151 config.links.get("rust"),
152 Some(&"https://rust-lang.org".to_string())
153 );
154 }
155
156 #[test]
157 fn test_backend_name() {
158 let storage = TomlStorage::new(PathBuf::from("/tmp/test.toml"));
159 assert_eq!(storage.backend_name(), "toml");
160 }
161}