diff --git a/src/lib.rs b/src/lib.rs index 50698bc..f4271de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,6 +109,10 @@ pub fn parse_response_vec<'a, I: Iterator, T: de::DeserializeOwn /// Parse the `playlist` command, a list of key-value pairs, as a vector of filenames. /// The playlist index of each item is *not* included because, if needed, /// it can easily be added with `.enumerate()`. +/// +/// Note: The MPD protocol documentation suggests using `playlistinfo` instead, +/// which returns a superset of this commands output, +/// but this isn’t deprecated, so if you only need the filenames, it should be all you need. pub fn parse_playlist<'a, I: Iterator>(input: I) -> MpdResult> { input // `iter.scan((), |(), item| predicate(item))` is equivalent to map_while(predicate), @@ -433,7 +437,7 @@ OK"; File { name: "Scans".into(), last_modified: DateTime::parse_from_rfc3339("2015-01-30T14:53:03Z").unwrap(), - size: 0 + size: -1 }, File { name: "15 風ハ旅スル(スマートフォンゲーム「風パズル 黒猫と白猫の夢見た世界」テーマ曲).flac".into(), diff --git a/src/structs.rs b/src/structs.rs index f5fb8e3..c554999 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -117,19 +117,33 @@ pub struct Status { pub error: Option, } +/// An object in the file system, as returned by the `listfiles` command. +/// For directories, the `size` will be `-1`, and [`is_directory`] is provided to check that. +/// +/// [`is_directory`]: #method.is_directory +/// +/// Properly parsing `listfiles` into a Vector of some enum with variants for file and directory +/// would make the deserialization code a lot more complicated, so I’m not interested in doing that +/// at this point in time. #[derive(Deserialize, Clone, Debug, PartialEq)] pub struct File { #[serde(alias = "directory", rename = "file")] pub name: String, #[serde(rename = "last-modified")] pub last_modified: DateTime, - #[serde(default)] - pub size: usize, + #[serde(default = "minus_one")] + pub size: i64, +} + +fn minus_one() -> i64 { + -1 } impl File { + /// Returns true if this file is a directory. + /// Internally, this just checks if `size == -1`. pub fn is_directory(&self) -> bool { - self.size == 0 + self.size == -1 } }