jingle_ft: Add constructors and setters.

Emmanuel Gil Peyrot created

Change summary

src/jingle_ft.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+), 1 deletion(-)

Detailed changes

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<Hash>,
 }
 
+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<File, Error> {
+        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<Element> for File {
     type Err = Error;