use core::hash::{Hash, Hasher};
use alloc::vec::Vec;
use crate::path::VdirPath;
pub mod copy;
pub mod delete;
pub mod get;
pub mod list;
pub mod locate;
pub mod r#move;
pub mod store;
pub(crate) const VCF: &str = "vcf";
pub(crate) const ICS: &str = "ics";
pub(crate) const TMP: &str = "tmp";
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum VdirItemKind {
Ical,
Vcard,
}
impl VdirItemKind {
pub fn extension(&self) -> &'static str {
match self {
Self::Ical => ICS,
Self::Vcard => VCF,
}
}
pub fn from_extension(ext: &str) -> Option<Self> {
match ext {
ICS => Some(Self::Ical),
VCF => Some(Self::Vcard),
_ => None,
}
}
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VdirItem {
pub path: VdirPath,
pub kind: VdirItemKind,
pub contents: Vec<u8>,
}
impl VdirItem {
pub fn id(&self) -> Option<&str> {
let name = self.path.file_name()?;
Some(match name.rsplit_once('.') {
Some((stem, _)) if !stem.is_empty() => stem,
_ => name,
})
}
pub fn contents(&self) -> &[u8] {
&self.contents
}
}
impl Hash for VdirItem {
fn hash<H: Hasher>(&self, state: &mut H) {
self.path.hash(state);
}
}
impl AsRef<VdirPath> for VdirItem {
fn as_ref(&self) -> &VdirPath {
&self.path
}
}
impl From<(VdirPath, VdirItemKind, Vec<u8>)> for VdirItem {
fn from((path, kind, contents): (VdirPath, VdirItemKind, Vec<u8>)) -> Self {
Self {
path,
kind,
contents,
}
}
}