mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 10:32:19 +00:00
address_object: Add birthday parsing
This commit is contained in:
@@ -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};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct AddressObject {
|
pub struct AddressObject {
|
||||||
id: String,
|
id: String,
|
||||||
vcf: String,
|
vcf: String,
|
||||||
|
vcard: VcardContact,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AddressObject {
|
impl AddressObject {
|
||||||
pub fn from_vcf(object_id: String, vcf: String) -> Result<Self, Error> {
|
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 {
|
pub fn get_id(&self) -> &str {
|
||||||
&self.id
|
&self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_etag(&self) -> String {
|
pub fn get_etag(&self) -> String {
|
||||||
let mut hasher = Sha256::new();
|
let mut hasher = Sha256::new();
|
||||||
hasher.update(&self.id);
|
hasher.update(&self.id);
|
||||||
@@ -24,4 +44,9 @@ impl AddressObject {
|
|||||||
pub fn get_vcf(&self) -> &str {
|
pub fn get_vcf(&self) -> &str {
|
||||||
&self.vcf
|
&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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user