2019-05-24 10:25:55 +02:00
|
|
|
use crate::common::*;
|
|
|
|
|
|
|
|
#[serde(untagged)]
|
2020-02-14 09:12:49 +01:00
|
|
|
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
|
2020-02-06 01:01:44 +01:00
|
|
|
pub(crate) enum Mode {
|
2020-02-11 12:08:57 +01:00
|
|
|
Single {
|
2020-02-14 09:12:49 +01:00
|
|
|
length: Bytes,
|
|
|
|
#[serde(
|
|
|
|
skip_serializing_if = "Option::is_none",
|
|
|
|
default,
|
|
|
|
with = "unwrap_or_skip"
|
|
|
|
)]
|
|
|
|
md5sum: Option<Md5Digest>,
|
2020-02-11 12:08:57 +01:00
|
|
|
},
|
|
|
|
Multiple {
|
|
|
|
files: Vec<FileInfo>,
|
|
|
|
},
|
2019-05-24 10:25:55 +02:00
|
|
|
}
|
2020-02-04 16:55:50 +01:00
|
|
|
|
|
|
|
impl Mode {
|
2020-03-13 06:05:49 +01:00
|
|
|
pub(crate) fn content_size(&self) -> Bytes {
|
2020-02-04 16:55:50 +01:00
|
|
|
match self {
|
2020-02-14 09:12:49 +01:00
|
|
|
Self::Single { length, .. } => *length,
|
|
|
|
Self::Multiple { files } => files.iter().map(|file| file.length).sum(),
|
2020-02-04 16:55:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 09:12:49 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_no_md5sum() {
|
|
|
|
let input = Mode::Single {
|
|
|
|
length: Bytes(10),
|
|
|
|
md5sum: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let have = bendy::serde::ser::to_bytes(&input).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(str::from_utf8(&have).unwrap(), "d6:lengthi10ee");
|
|
|
|
|
|
|
|
let output: Mode = bendy::serde::de::from_bytes(&have).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(output, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_with_md5sum() {
|
|
|
|
let input = Mode::Single {
|
|
|
|
length: Bytes(10),
|
|
|
|
md5sum: Some(Md5Digest::from_hex("000102030405060708090a0b0c0d0e0f")),
|
|
|
|
};
|
|
|
|
|
|
|
|
let have = bendy::serde::ser::to_bytes(&input).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&have).unwrap(),
|
|
|
|
"d6:lengthi10e6:md5sum32:000102030405060708090a0b0c0d0e0fe"
|
|
|
|
);
|
|
|
|
|
|
|
|
let output: Mode = bendy::serde::de::from_bytes(&have).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(output, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_single() {
|
|
|
|
let value = Mode::Single {
|
|
|
|
length: Bytes(10),
|
|
|
|
md5sum: Some(Md5Digest::from_hex("000102030405060708090a0b0c0d0e0f")),
|
|
|
|
};
|
|
|
|
|
|
|
|
let bencode = bendy::serde::ser::to_bytes(&value).unwrap();
|
|
|
|
|
|
|
|
let deserialized = bendy::serde::de::from_bytes(&bencode).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(value, deserialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_multiple() {
|
|
|
|
let value = Mode::Multiple {
|
|
|
|
files: vec![FileInfo {
|
|
|
|
length: Bytes(10),
|
|
|
|
path: FilePath::from_components(&["foo", "bar"]),
|
|
|
|
md5sum: Some(Md5Digest::from_hex("000102030405060708090a0b0c0d0e0f")),
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
|
|
|
|
let bencode = bendy::serde::ser::to_bytes(&value).unwrap();
|
|
|
|
|
|
|
|
let deserialized = bendy::serde::de::from_bytes(&bencode).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(value, deserialized);
|
|
|
|
}
|
|
|
|
}
|