use std::fmt::Display;
use uuid::Uuid;
use crate::fs::path::SPath;
pub type BufferId = Uuid;
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct DocumentIdentifier {
pub buffer_id: BufferId,
pub file_path: Option<SPath>,
}
impl Display for DocumentIdentifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.file_path {
None => write!(f, "[unnamed] #{}", self.buffer_id),
Some(sp) => write!(f, "{} #{}", sp.label(), self.buffer_id),
}
}
}
impl DocumentIdentifier {
pub fn new_unique() -> DocumentIdentifier {
DocumentIdentifier {
buffer_id: Uuid::new_v4(),
file_path: None,
}
}
pub fn with_file_path(self, ff: SPath) -> Self {
Self {
file_path: Some(ff),
..self
}
}
pub fn label(&self) -> String {
match &self.file_path {
None => "[unnamed]".to_string(),
Some(sp) => format!("{}", sp.label()),
}
}
}
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for DocumentIdentifier {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self {
buffer_id: arbitrary::Arbitrary::arbitrary(u)?,
file_path: None,
})
}
}