use std::path::{Path, PathBuf};
use crate::obsidian::Error;
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Note {
name: String,
path: PathBuf,
}
impl Note {
pub fn name(&self) -> &str {
&self.name
}
pub fn path(&self) -> &Path {
&self.path
}
}
impl TryFrom<(&str, PathBuf)> for Note {
type Error = Error;
fn try_from((name, path): (&str, PathBuf)) -> Result<Self, Self::Error> {
match path.is_file() {
true => Ok(Self {
name: name.to_string(),
path,
}),
false => Err(Error::Io(std::io::ErrorKind::IsADirectory.into())),
}
}
}
impl TryFrom<(String, PathBuf)> for Note {
type Error = Error;
fn try_from((name, path): (String, PathBuf)) -> Result<Self, Self::Error> {
Self::try_from((name.as_str(), path))
}
}
impl Note {
pub fn new_unchecked(name: &str, path: &Path) -> Self {
Self {
name: name.to_string(),
path: path.to_path_buf(),
}
}
}