use super::super::parameters::{PrefParam, TypeParam, ValueParam};
use crate::common::VCardValue;
use sp_vcard_derive::{vcard_property_type, CommonTypeParamMixin};
#[vcard_property_type("ADR")]
#[derive(CommonTypeParamMixin)]
pub struct Address {
street: ValueParam,
locality: ValueParam,
region: ValueParam,
code: ValueParam,
country: ValueParam,
type_param: TypeParam,
pref_param: PrefParam,
}
impl Address {
pub fn new() -> Self {
Self {
street: ValueParam::new(),
locality: ValueParam::new(),
region: ValueParam::new(),
code: ValueParam::new(),
country: ValueParam::new(),
type_param: TypeParam::new(),
pref_param: PrefParam::new(),
}
}
pub fn street(mut self, street: &str) -> Self {
self.street.set(street);
self
}
pub fn locality(mut self, locality: &str) -> Self {
self.locality.set(locality);
self
}
pub fn region(mut self, region: &str) -> Self {
self.region.set(region);
self
}
pub fn code(mut self, code: &str) -> Self {
self.code.set(code);
self
}
pub fn country(mut self, country: &str) -> Self {
self.country.set(country);
self
}
pub fn set_prefer(mut self, preferred: u8) -> Self {
self.pref_param.set(preferred).unwrap();
self
}
fn format_adr(&self) -> String {
format!(
";;{};{};{};{};{}",
self.street, self.locality, self.region, self.code, self.country
)
}
}
impl VCardValue for Address {
fn format_value(&self) -> String {
let value = self.format_adr();
if value.len() > 6 {
format!(
"{}{}{}:{}\n",
Self::get_value_type(),
self.pref_param,
self.type_param,
value
)
} else {
"".into()
}
}
}