Skip to content

Commit

Permalink
address_object: Add birthday parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lennart-k committed Jan 6, 2025
1 parent de14a6f commit d582d0d
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions crates/store/src/addressbook/address_object.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
use crate::Error;
use std::{collections::HashMap, io::BufReader};

use crate::{calendar::CalDateTime, Error};
use ical::parser::{
vcard::{self, component::VcardContact},
Component,
};
use sha2::{Digest, Sha256};

#[derive(Debug, Clone)]
pub struct AddressObject {
id: String,
vcf: String,
vcard: VcardContact,
}

impl AddressObject {
pub fn from_vcf(object_id: String, vcf: String) -> Result<Self, Error> {
Ok(Self { id: object_id, vcf })
let mut parser = vcard::VcardParser::new(BufReader::new(vcf.as_bytes()));
let vcard = parser.next().ok_or(Error::NotFound)??;
if parser.next().is_some() {
return Err(Error::InvalidData(
"multiple vcards, only one allowed".to_owned(),
));
}
Ok(Self {
id: object_id,
vcf,
vcard,
})
}

pub fn get_id(&self) -> &str {
&self.id
}

pub fn get_etag(&self) -> String {
let mut hasher = Sha256::new();
hasher.update(&self.id);
Expand All @@ -24,4 +44,9 @@ impl AddressObject {
pub fn get_vcf(&self) -> &str {
&self.vcf
}

pub fn get_birthday(&self) -> Option<CalDateTime> {
let prop = self.vcard.get_property("BDAY")?;
CalDateTime::parse_prop(prop, &HashMap::default()).unwrap_or(None)
}
}

0 comments on commit d582d0d

Please sign in to comment.