use super::{Anchor, Description};
use derive_more::Constructor;
use serde::{Deserialize, Serialize};
use std::{fmt, path::PathBuf};
#[derive(
Constructor,
Clone,
Debug,
Default,
Eq,
PartialEq,
Hash,
Serialize,
Deserialize,
)]
pub struct WikiLink {
pub path: PathBuf,
pub description: Option<Description>,
pub anchor: Option<Anchor>,
}
impl WikiLink {
pub fn is_local_anchor(&self) -> bool {
self.path.as_os_str().is_empty() && self.anchor.is_some()
}
pub fn is_path_dir(&self) -> bool {
self.path
.to_string_lossy()
.chars()
.last()
.map(std::path::is_separator)
.unwrap_or_default()
}
}
impl fmt::Display for WikiLink {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(desc) = self.description.as_ref() {
write!(f, "{}", desc)
} else {
write!(f, "{}", self.path.to_string_lossy())?;
if let Some(anchor) = self.anchor.as_ref() {
write!(f, "{}", anchor)?;
}
Ok(())
}
}
}
impl From<PathBuf> for WikiLink {
fn from(path: PathBuf) -> Self {
Self::new(path, None, None)
}
}
impl From<String> for WikiLink {
fn from(str_path: String) -> Self {
Self::from(PathBuf::from(str_path))
}
}