Skip to content

Commit e23850f

Browse files
committed
Add a helper for getting a list of values all with the same key.
1 parent 2512f5f commit e23850f

File tree

2 files changed

+18
-60
lines changed

2 files changed

+18
-60
lines changed

src/client.rs

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -482,61 +482,25 @@ impl<S: Read + Write> Client<S> {
482482
/// List all available commands
483483
pub fn commands(&mut self) -> Result<Vec<String>> {
484484
self.run_command("commands", ())
485-
.and_then(|_| {
486-
self.read_pairs()
487-
.filter(|r| {
488-
r.as_ref()
489-
.map(|&(ref a, _)| *a == "command")
490-
.unwrap_or(true)
491-
})
492-
.map(|r| r.map(|(_, b)| b))
493-
.collect()
494-
})
485+
.and_then(|_| self.read_list("command"))
495486
}
496487

497488
/// List all forbidden commands
498489
pub fn notcommands(&mut self) -> Result<Vec<String>> {
499490
self.run_command("notcommands", ())
500-
.and_then(|_| {
501-
self.read_pairs()
502-
.filter(|r| {
503-
r.as_ref()
504-
.map(|&(ref a, _)| *a == "command")
505-
.unwrap_or(true)
506-
})
507-
.map(|r| r.map(|(_, b)| b))
508-
.collect()
509-
})
491+
.and_then(|_| self.read_list("command"))
510492
}
511493

512494
/// List all available URL handlers
513495
pub fn urlhandlers(&mut self) -> Result<Vec<String>> {
514496
self.run_command("urlhandlers", ())
515-
.and_then(|_| {
516-
self.read_pairs()
517-
.filter(|r| {
518-
r.as_ref()
519-
.map(|&(ref a, _)| *a == "handler")
520-
.unwrap_or(true)
521-
})
522-
.map(|r| r.map(|(_, b)| b))
523-
.collect()
524-
})
497+
.and_then(|_| self.read_list("handler"))
525498
}
526499

527500
/// List all supported tag types
528501
pub fn tagtypes(&mut self) -> Result<Vec<String>> {
529502
self.run_command("tagtypes", ())
530-
.and_then(|_| {
531-
self.read_pairs()
532-
.filter(|r| {
533-
r.as_ref()
534-
.map(|&(ref a, _)| *a == "tagtype")
535-
.unwrap_or(true)
536-
})
537-
.map(|r| r.map(|(_, b)| b))
538-
.collect()
539-
})
503+
.and_then(|_| self.read_list("tagtype"))
540504
}
541505

542506
/// List all available decoder plugins
@@ -549,16 +513,8 @@ impl<S: Read + Write> Client<S> {
549513
/// List all channels available for current connection
550514
pub fn channels(&mut self) -> Result<Vec<Channel>> {
551515
self.run_command("channels", ())
552-
.and_then(|_| {
553-
self.read_pairs()
554-
.filter(|r| {
555-
r.as_ref()
556-
.map(|&(ref a, _)| *a == "channel")
557-
.unwrap_or(true)
558-
})
559-
.map(|r| r.map(|(_, b)| unsafe { Channel::new_unchecked(b) }))
560-
.collect()
561-
})
516+
.and_then(|_| self.read_list("channel"))
517+
.map(|v| v.into_iter().map(|b| unsafe { Channel::new_unchecked(b) }).collect())
562518
}
563519

564520
/// Read queued messages from subscribed channels
@@ -681,16 +637,7 @@ impl<S: Read + Write> Client<S> {
681637
/// with a tag set to given value
682638
pub fn find_sticker_eq(&mut self, typ: &str, uri: &str, name: &str, value: &str) -> Result<Vec<String>> {
683639
self.run_command("sticker find", (typ, uri, name, value))
684-
.and_then(|_| {
685-
self.read_pairs()
686-
.filter(|r| {
687-
r.as_ref()
688-
.map(|&(ref a, _)| *a == "file")
689-
.unwrap_or(true)
690-
})
691-
.map(|r| r.map(|(_, b)| b))
692-
.collect()
693-
})
640+
.and_then(|_| self.read_list("file"))
694641
}
695642
// }}}
696643
}

src/proto.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ pub trait Proto {
111111
self.read_pairs().split(key).map(|v| v.and_then(FromMap::from_map)).collect()
112112
}
113113

114+
fn read_list(&mut self, key: &'static str) -> Result<Vec<String>> {
115+
self.read_pairs()
116+
.filter(|r| {
117+
r.as_ref()
118+
.map(|&(ref a, _)| *a == key)
119+
.unwrap_or(true)
120+
})
121+
.map(|r| r.map(|(_, b)| b))
122+
.collect()
123+
}
124+
114125
fn read_struct<'a, T>(&'a mut self) -> Result<T>
115126
where T: 'a + FromIter,
116127
Self::Stream: 'a

0 commit comments

Comments
 (0)