From 927df2fdb12fbe1e711f73f8a9a154f8717a84e5 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 4 May 2018 19:10:45 +0200 Subject: [PATCH] jingle_ft: Add constructors and setters. --- src/jingle_ft.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/jingle_ft.rs b/src/jingle_ft.rs index bf584380..c78e1d45 100644 --- a/src/jingle_ft.rs +++ b/src/jingle_ft.rs @@ -19,7 +19,7 @@ use error::Error; use ns; generate_element_with_children!( - #[derive(PartialEq)] + #[derive(PartialEq, Default)] Range, "range", ns::JINGLE_FT, attributes: [ offset: u64 = "offset" => default, @@ -30,6 +30,12 @@ generate_element_with_children!( ] ); +impl Range { + pub fn new() -> Range { + Default::default() + } +} + type Lang = String; generate_id!(Desc); @@ -45,6 +51,60 @@ pub struct File { pub hashes: Vec, } +impl File { + pub fn new() -> File { + File { + date: None, + media_type: None, + name: None, + descs: BTreeMap::new(), + size: None, + range: None, + hashes: Vec::new(), + } + } + + pub fn with_date(mut self, date: DateTime) -> File { + self.date = Some(date); + self + } + + pub fn with_date_str(mut self, date: &str) -> Result { + self.date = Some(DateTime::from_str(date)?); + Ok(self) + } + + pub fn with_media_type(mut self, media_type: String) -> File { + self.media_type = Some(media_type); + self + } + + pub fn with_name(mut self, name: String) -> File { + self.name = Some(name); + self + } + + pub fn add_desc(mut self, lang: &str, desc: Desc) -> File { + self.descs.insert(Lang::from(lang), desc); + self + } + + pub fn with_size(mut self, size: u64) -> File { + self.size = Some(size); + self + } + + pub fn with_range(mut self, range: Range) -> File { + self.range = Some(range); + self + } + + pub fn add_hash(mut self, hash: Hash) -> File { + self.hashes.push(hash); + self + } +} + impl TryFrom for File { type Err = Error;