Skip to content

Commit 2512f5f

Browse files
committed
Factor out plugin list decoding.
1 parent dcde6d1 commit 2512f5f

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

src/client.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -541,33 +541,7 @@ impl<S: Read + Write> Client<S> {
541541

542542
/// List all available decoder plugins
543543
pub fn decoders(&mut self) -> Result<Vec<Plugin>> {
544-
try!(self.run_command("decoders", ()));
545-
546-
let mut result = Vec::new();
547-
let mut plugin: Option<Plugin> = None;
548-
for reply in self.read_pairs() {
549-
let (a, b) = try!(reply);
550-
match &*a {
551-
"plugin" => {
552-
plugin.map(|p| result.push(p));
553-
554-
plugin = Some(Plugin {
555-
name: b,
556-
suffixes: Vec::new(),
557-
mime_types: Vec::new(),
558-
});
559-
}
560-
"mime_type" => {
561-
plugin.as_mut().map(|p| p.mime_types.push(b));
562-
}
563-
"suffix" => {
564-
plugin.as_mut().map(|p| p.suffixes.push(b));
565-
}
566-
_ => unreachable!(),
567-
}
568-
}
569-
plugin.map(|p| result.push(p));
570-
Ok(result)
544+
self.run_command("decoders", ()).and_then(|_| self.read_struct())
571545
}
572546
// }}}
573547

src/plugin.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! The module defines decoder plugin data structures
22
3+
use convert::FromIter;
4+
use error::Error;
5+
36
/// Decoder plugin
47
#[derive(Clone, Debug, PartialEq, RustcEncodable)]
58
pub struct Plugin {
@@ -10,3 +13,33 @@ pub struct Plugin {
1013
/// supported MIME-types
1114
pub mime_types: Vec<String>,
1215
}
16+
17+
impl FromIter for Vec<Plugin> {
18+
fn from_iter<I: Iterator<Item = Result<(String, String), Error>>>(iter: I) -> Result<Self, Error> {
19+
let mut result = Vec::new();
20+
let mut plugin: Option<Plugin> = None;
21+
for reply in iter {
22+
let (a, b) = try!(reply);
23+
match &*a {
24+
"plugin" => {
25+
plugin.map(|p| result.push(p));
26+
27+
plugin = Some(Plugin {
28+
name: b,
29+
suffixes: Vec::new(),
30+
mime_types: Vec::new(),
31+
});
32+
}
33+
"mime_type" => {
34+
plugin.as_mut().map(|p| p.mime_types.push(b));
35+
}
36+
"suffix" => {
37+
plugin.as_mut().map(|p| p.suffixes.push(b));
38+
}
39+
_ => unreachable!(),
40+
}
41+
}
42+
plugin.map(|p| result.push(p));
43+
Ok(result)
44+
}
45+
}

0 commit comments

Comments
 (0)