basalt_core/obsidian.rs
1//! This module provides functionality operating with Obsidian. It lets you read and manipulate
2//! Obsidian's configuration, vaults, and notes.
3//!
4//! Currently supports reading vaults, notes, and writing to note path.
5//!
6//! # Example
7//!
8//! ```
9//! use basalt_core::obsidian::{ObsidianConfig, Error, Vault};
10//!
11//! let config = ObsidianConfig::from([
12//! ("Obsidian", Vault::default()),
13//! ("My Vault", Vault::default()),
14//! ]);
15//!
16//! _ = config.get_vault_by_name("Obsidian");
17//! ```
18use std::{io, result};
19
20mod config;
21mod note;
22mod vault;
23
24pub use config::ObsidianConfig;
25pub use note::Note;
26pub use vault::Vault;
27
28/// A [`std::result::Result`] type for fallible operations in [`crate::obsidian`].
29///
30/// For convenience of use and to avoid writing [`Error`] directly.
31/// All fallible operations return [`Error`] as the error variant.
32///
33/// # Examples
34///
35/// ```
36/// use basalt_core::obsidian::{ObsidianConfig, Result};
37///
38/// fn get_vault_names() -> Result<Vec<String>> {
39/// let config = ObsidianConfig::load()?;
40/// Ok(config.vaults().map(|(name,_)| name).collect())
41/// }
42///
43/// fn main() -> Result<()> {
44/// let vaults = get_vault_names()?;
45/// println!("Found vaults: {:?}", vaults);
46/// Ok(())
47/// }
48/// ```
49pub type Result<T> = result::Result<T, Error>;
50
51/// Error type for fallible operations in this [`crate`].
52///
53/// Implements [`std::error::Error`] via [thiserror](https://docs.rs/thiserror).
54#[derive(thiserror::Error, Debug)]
55pub enum Error {
56 /// Expected resource behind a path was not found.
57 #[error("Path not found: {0}")]
58 PathNotFound(String),
59
60 /// JSON (de)serialization error, from [`serde_json::Error`].
61 #[error("JSON (de)serialization error: {0}")]
62 Json(#[from] serde_json::Error),
63
64 /// I/O error, from [`std::io::Error`].
65 #[error("I/O error: {0}")]
66 Io(#[from] io::Error),
67}