Don’t report empty files as directories; add more docs

This commit is contained in:
kageru 2020-07-24 17:16:03 +02:00
parent c3549cd230
commit 5889512732
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 22 additions and 4 deletions

View File

@ -109,6 +109,10 @@ pub fn parse_response_vec<'a, I: Iterator<Item = &'a str>, 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<Item = &'a str>>(input: I) -> MpdResult<Vec<&'a str>> {
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(),

View File

@ -117,19 +117,33 @@ pub struct Status {
pub error: Option<String>,
}
/// 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<FixedOffset>,
#[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
}
}