pub struct Vault {
pub name: String,
pub path: PathBuf,
pub open: bool,
pub ts: u64,
}Expand description
Represents a single Obsidian vault.
A vault is a folder containing notes and other metadata.
Fields§
§name: StringThe name of the vault, inferred from its directory name.
path: PathBufFilesystem path to the vault’s directory.
open: boolWhether the vault is marked ‘open’ by Obsidian.
ts: u64Timestamp of last update or creation.
Implementations§
Source§impl Vault
impl Vault
Sourcepub fn entries(&self) -> Vec<VaultEntry>
pub fn entries(&self) -> Vec<VaultEntry>
Returns a Vec of Markdown vault entries in this vault as VaultEntry structs.
Entries can be either directories or files (notes). If the directory is marked hidden with
a dot (.) prefix it will be filtered out from the resulting Vec.
The returned entries are not sorted.
§Examples
use basalt_core::obsidian::{Vault, Note};
let vault = Vault {
name: "MyVault".into(),
path: "path/to/my_vault".into(),
..Default::default()
};
assert_eq!(vault.entries(), vec![]);Sourcepub fn create_note(&self, name: &str) -> Result<Note, Error>
pub fn create_note(&self, name: &str) -> Result<Note, Error>
Creates a new empty note with the provided name.
If a note with the given name already exists, a numbered suffix will be appended (e.g., “Note 1”, “Note 2”, etc.) to find an available name.
§Errors
Returns an error if:
- I/O operations fail (directory creation, file writing, or path checks)
- No available name is found after 999 attempts (
Error::MaxAttemptsExceeded)
§Examples
use std::fs;
use tempfile::tempdir;
use basalt_core::obsidian::{Vault, Note, Error};
let tmp_dir = tempdir()?;
let vault = Vault {
path: tmp_dir.path().to_path_buf(),
..Default::default()
};
let note = vault.create_note("Arbitrary Name")?;
assert_eq!(fs::exists(¬e.path)?, true);
Sourcepub fn find_available_note_name(
&self,
name: &str,
) -> Result<(String, PathBuf), Error>
pub fn find_available_note_name( &self, name: &str, ) -> Result<(String, PathBuf), Error>
Find available note name by incrementing number suffix at the end.
Increments until we find a ‘free’ name e.g. if “Untitled 1” exists we will try next “Untitled 2”, and then “Untitled 3” and so on.
§Errors
Returns Error::MaxAttemptsExceeded if no available name is found after 999 attempts.
§Examples
use std::fs;
use tempfile::tempdir;
use basalt_core::obsidian::{Vault, Note, Error};
let tmp_dir = tempdir()?;
let tmp_path = tmp_dir.path();
let vault = Vault {
path: tmp_path.to_path_buf(),
..Default::default()
};
let note_name = "Arbitrary Name";
fs::write(tmp_path.join(note_name).with_extension("md"), "")?;
let (name, path) = vault.find_available_note_name(note_name)?;
assert_eq!(&name, "Arbitrary Name 1");
assert_eq!(fs::exists(&path)?, false);
Sourcepub fn create_untitled_note(&self) -> Result<Note, Error>
pub fn create_untitled_note(&self) -> Result<Note, Error>
Creates a new empty note with name “Untitled” or “Untitled {n}”.
This is a convenience method that calls Vault::create_note with “Untitled” as the name.
§Errors
Returns an error if:
- I/O operations fail (file writing or path checks)
- No available name is found after 999 attempts (
Error::MaxAttemptsExceeded)
§Examples
use std::{fs, result};
use tempfile::tempdir;
use basalt_core::obsidian::{Vault, Note, Error};
let tmp_dir = tempdir()?;
let vault = Vault {
path: tmp_dir.path().to_path_buf(),
..Default::default()
};
let note = vault.create_untitled_note()?;
assert_eq!(¬e.name, "Untitled");
assert_eq!(fs::exists(¬e.path)?, true);
(1..=100).try_for_each(|n| -> result::Result<(), Error> {
let note = vault.create_untitled_note()?;
assert_eq!(note.name, format!("Untitled {n}"));
assert_eq!(fs::exists(¬e.path)?, true);
Ok(())
})?;