mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
carddav: Require UID in address object
This commit is contained in:
@@ -11,7 +11,6 @@ use std::{collections::HashMap, io::BufReader};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AddressObject {
|
||||
id: String,
|
||||
vcf: String,
|
||||
vcard: VcardContact,
|
||||
}
|
||||
@@ -20,19 +19,16 @@ impl TryFrom<VcardContact> for AddressObject {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(vcard: VcardContact) -> Result<Self, Self::Error> {
|
||||
let id = vcard
|
||||
.get_property("UID")
|
||||
.ok_or(Error::InvalidData("Missing UID".to_owned()))?
|
||||
.value
|
||||
.clone()
|
||||
.ok_or(Error::InvalidData("Missing UID".to_owned()))?;
|
||||
if vcard.get_uid().is_none() {
|
||||
return Err(Error::InvalidData("missing UID".to_owned()));
|
||||
}
|
||||
let vcf = vcard.generate();
|
||||
Ok(Self { id, vcf, vcard })
|
||||
Ok(Self { vcf, vcard })
|
||||
}
|
||||
}
|
||||
|
||||
impl AddressObject {
|
||||
pub fn from_vcf(object_id: String, vcf: String) -> Result<Self, Error> {
|
||||
pub fn from_vcf(vcf: String) -> Result<Self, Error> {
|
||||
let mut parser = vcard::VcardParser::new(BufReader::new(vcf.as_bytes()));
|
||||
let vcard = parser.next().ok_or(Error::MissingContact)??;
|
||||
if parser.next().is_some() {
|
||||
@@ -40,20 +36,22 @@ impl AddressObject {
|
||||
"multiple vcards, only one allowed".to_owned(),
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
id: object_id,
|
||||
vcf,
|
||||
vcard,
|
||||
})
|
||||
|
||||
if vcard.get_uid().is_none() {
|
||||
return Err(Error::InvalidData("missing UID".to_owned()));
|
||||
}
|
||||
Ok(Self { vcf, vcard })
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> &str {
|
||||
&self.id
|
||||
self.vcard
|
||||
.get_uid()
|
||||
.expect("We've validated before that UID exists")
|
||||
}
|
||||
|
||||
pub fn get_etag(&self) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(&self.id);
|
||||
hasher.update(self.get_id());
|
||||
hasher.update(self.get_vcf());
|
||||
format!("\"{:x}\"", hasher.finalize())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user