basalt_core/obsidian/config.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
use dirs::config_local_dir;
use serde::{Deserialize, Deserializer};
use std::result;
use std::{collections::HashMap, fs, path::PathBuf};
use crate::obsidian::{Error, Result, Vault};
/// Represents the Obsidian configuration, typically loaded from an `obsidian.json` file.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ObsidianConfig {
/// A mapping of vault (folder) names to [`Vault`] definitions.
vaults: HashMap<String, Vault>,
}
impl ObsidianConfig {
/// Attempts to locate and load the system's `obsidian.json` file as an [`ObsidianConfig`].
///
/// Returns an [`Error`] if the filepath doesn't exist or JSON parsing failed.
pub fn load() -> Result<Self> {
obsidian_config_dir()
.map(ObsidianConfig::load_from)
.ok_or_else(|| Error::PathNotFound("Obsidian config directory".to_string()))?
}
/// Attempts to load `obsidian.json` file as an [`ObsidianConfig`] from the given directory
/// [`PathBuf`].
///
/// Returns an [`Error`] if the filepath doesn't exist or JSON parsing failed.
///
/// # Examples
///
/// ```
/// use basalt_core::obsidian::ObsidianConfig;
/// use std::path::PathBuf;
///
/// _ = ObsidianConfig::load_from(PathBuf::from("./dir-with-config-file"));
/// ```
pub fn load_from(dir: PathBuf) -> Result<Self> {
let contents = fs::read_to_string(dir.join("obsidian.json"))?;
Ok(serde_json::from_str(&contents)?)
}
/// Returns an iterator over the vaults in the configuration.
///
/// # Examples
///
/// ```
/// use basalt_core::obsidian::{ObsidianConfig, Vault};
///
/// let config = ObsidianConfig::from([
/// ("Obsidian", Vault::default()),
/// ("Work", Vault::default()),
/// ]);
///
/// _ = config.vaults();
/// ```
pub fn vaults(&self) -> impl Iterator<Item = (String, Vault)> {
self.vaults.clone().into_iter()
}
/// Finds a vault by name, returning a reference if it exists.
///
/// # Examples
///
/// ```
/// use basalt_core::obsidian::{ObsidianConfig, Vault};
///
/// let config = ObsidianConfig::from([
/// ("Obsidian", Vault::default()),
/// ("Work", Vault::default()),
/// ]);
///
/// _ = config.vault_by_name("Obsidian");
/// ```
pub fn vault_by_name(&self, name: &str) -> Option<&Vault> {
self.vaults.get(name)
}
}
impl<const N: usize> From<[(&str, Vault); N]> for ObsidianConfig {
/// # Examples
///
/// ```
/// use basalt_core::obsidian::{ObsidianConfig, Vault};
///
/// let config_1 = ObsidianConfig::from([
/// ("Obsidian", Vault::default()),
/// ("My Vault", Vault::default()),
/// ]);
///
/// let config_2: ObsidianConfig = [
/// ("Obsidian", Vault::default()),
/// ("My Vault", Vault::default()),
/// ].into();
///
/// assert_eq!(config_1, config_2);
/// ```
fn from(arr: [(&str, Vault); N]) -> Self {
Self {
vaults: HashMap::from(arr.map(|(name, vault)| (name.to_owned(), vault))),
}
}
}
impl<const N: usize> From<[(String, Vault); N]> for ObsidianConfig {
/// # Examples
///
/// ```
/// use basalt_core::obsidian::{ObsidianConfig, Vault};
///
/// let config_1 = ObsidianConfig::from([
/// (String::from("Obsidian"), Vault::default()),
/// (String::from("My Vault"), Vault::default()),
/// ]);
///
/// let config_2: ObsidianConfig = [
/// (String::from("Obsidian"), Vault::default()),
/// (String::from("My Vault"), Vault::default()),
/// ].into();
///
/// assert_eq!(config_1, config_2);
/// ```
fn from(arr: [(String, Vault); N]) -> Self {
Self {
vaults: HashMap::from(arr),
}
}
}
impl<'de> Deserialize<'de> for ObsidianConfig {
fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Json {
vaults: HashMap<String, Vault>,
}
impl From<Json> for ObsidianConfig {
fn from(value: Json) -> Self {
ObsidianConfig {
vaults: value
.vaults
.into_values()
.map(|vault| (vault.name.clone(), vault))
.collect(),
}
}
}
Ok(Json::from(Deserialize::deserialize(deserializer)?).into())
}
}
/// Returns the system path to Obsidian's config folder, if any.
///
/// For reference:
/// - macOS: `/Users/username/Library/Application Support/obsidian`
/// - Windows: `%APPDATA%\Obsidian\`
/// - Linux: `$XDG_CONFIG_HOME/Obsidian/` or `~/.config/Obsidian/`
///
/// More info: [https://help.obsidian.md/Files+and+folders/How+Obsidian+stores+data]
fn obsidian_config_dir() -> Option<PathBuf> {
#[cfg(target_os = "macos")]
const OBSIDIAN_CONFIG_DIR_NAME: &str = "obsidian";
#[cfg(any(target_os = "windows", target_os = "linux"))]
const OBSIDIAN_CONFIG_DIR_NAME: &str = "Obsidian";
config_local_dir().map(|config_path| config_path.join(OBSIDIAN_CONFIG_DIR_NAME))
}