jingle_ft: Document this module.

This commit is contained in:
Emmanuel Gil Peyrot 2018-08-08 20:52:27 +02:00
commit eeeae25cb1

View file

@ -4,8 +4,6 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
#![allow(missing_docs)]
use try_from::TryFrom; use try_from::TryFrom;
use std::str::FromStr; use std::str::FromStr;
@ -21,18 +19,25 @@ use error::Error;
use ns; use ns;
generate_element!( generate_element!(
/// Represents a range in a file.
#[derive(PartialEq, Default)] #[derive(PartialEq, Default)]
Range, "range", JINGLE_FT, Range, "range", JINGLE_FT,
attributes: [ attributes: [
/// The offset in bytes from the beginning of the file.
offset: u64 = "offset" => default, offset: u64 = "offset" => default,
/// The length in bytes of the range, or None to be the entire
/// remaining of the file.
length: Option<u64> = "length" => optional length: Option<u64> = "length" => optional
], ],
children: [ children: [
/// List of hashes for this range.
hashes: Vec<Hash> = ("hash", HASHES) => Hash hashes: Vec<Hash> = ("hash", HASHES) => Hash
] ]
); );
impl Range { impl Range {
/// Creates a new range.
pub fn new() -> Range { pub fn new() -> Range {
Default::default() Default::default()
} }
@ -40,20 +45,38 @@ impl Range {
type Lang = String; type Lang = String;
generate_id!(Desc); generate_id!(
/// Wrapper for a file description.
Desc
);
/// Represents a file to be transferred.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct File { pub struct File {
/// The date of last modification of this file.
pub date: Option<DateTime>, pub date: Option<DateTime>,
/// The MIME type of this file.
pub media_type: Option<String>, pub media_type: Option<String>,
/// The name of this file.
pub name: Option<String>, pub name: Option<String>,
/// The description of this file, possibly localised.
pub descs: BTreeMap<Lang, Desc>, pub descs: BTreeMap<Lang, Desc>,
/// The size of this file, in bytes.
pub size: Option<u64>, pub size: Option<u64>,
/// Used to request only a part of this file.
pub range: Option<Range>, pub range: Option<Range>,
/// A list of hashes matching this entire file.
pub hashes: Vec<Hash>, pub hashes: Vec<Hash>,
} }
impl File { impl File {
/// Creates a new file descriptor.
pub fn new() -> File { pub fn new() -> File {
File { File {
date: None, date: None,
@ -66,41 +89,50 @@ impl File {
} }
} }
/// Sets the date of last modification on this file.
pub fn with_date(mut self, date: DateTime) -> File { pub fn with_date(mut self, date: DateTime) -> File {
self.date = Some(date); self.date = Some(date);
self self
} }
/// Sets the date of last modification on this file from an ISO-8601
/// string.
pub fn with_date_str(mut self, date: &str) -> Result<File, Error> { pub fn with_date_str(mut self, date: &str) -> Result<File, Error> {
self.date = Some(DateTime::from_str(date)?); self.date = Some(DateTime::from_str(date)?);
Ok(self) Ok(self)
} }
/// Sets the MIME type of this file.
pub fn with_media_type(mut self, media_type: String) -> File { pub fn with_media_type(mut self, media_type: String) -> File {
self.media_type = Some(media_type); self.media_type = Some(media_type);
self self
} }
/// Sets the name of this file.
pub fn with_name(mut self, name: String) -> File { pub fn with_name(mut self, name: String) -> File {
self.name = Some(name); self.name = Some(name);
self self
} }
/// Sets a description for this file.
pub fn add_desc(mut self, lang: &str, desc: Desc) -> File { pub fn add_desc(mut self, lang: &str, desc: Desc) -> File {
self.descs.insert(Lang::from(lang), desc); self.descs.insert(Lang::from(lang), desc);
self self
} }
/// Sets the file size of this file, in bytes.
pub fn with_size(mut self, size: u64) -> File { pub fn with_size(mut self, size: u64) -> File {
self.size = Some(size); self.size = Some(size);
self self
} }
/// Request only a range of this file.
pub fn with_range(mut self, range: Range) -> File { pub fn with_range(mut self, range: Range) -> File {
self.range = Some(range); self.range = Some(range);
self self
} }
/// Add a hash on this file.
pub fn add_hash(mut self, hash: Hash) -> File { pub fn add_hash(mut self, hash: Hash) -> File {
self.hashes.push(hash); self.hashes.push(hash);
self self
@ -211,8 +243,11 @@ impl From<File> for Element {
root.build() root.build()
} }
} }
/// A wrapper element for a file.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Description { pub struct Description {
/// The actual file descriptor.
pub file: File, pub file: File,
} }
@ -247,10 +282,16 @@ impl From<Description> for Element {
} }
} }
/// A checksum for checking that the file has been transferred correctly.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Checksum { pub struct Checksum {
/// The identifier of the file transfer content.
pub name: ContentId, pub name: ContentId,
/// The creator of this file transfer.
pub creator: Creator, pub creator: Creator,
/// The file being checksummed.
pub file: File, pub file: File,
} }
@ -289,11 +330,17 @@ impl From<Checksum> for Element {
} }
} }
generate_element!(Received, "received", JINGLE_FT, generate_element!(
/// A notice that the file transfer has been completed.
Received, "received", JINGLE_FT,
attributes: [ attributes: [
/// The content identifier of this Jingle session.
name: ContentId = "name" => required, name: ContentId = "name" => required,
/// The creator of this file transfer.
creator: Creator = "creator" => required, creator: Creator = "creator" => required,
]); ]
);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {