diff options
author | Till Höppner | 2015-06-11 20:57:39 +0200 |
---|---|---|
committer | Till Höppner | 2015-06-11 20:57:39 +0200 |
commit | bc755a4dedc520b672bc7168ff6ef9d088072d99 (patch) | |
tree | 52bed0ed50693bb9f5bbdc52fa81d7e1edfb855c /src/format/mod.rs | |
parent | 86fe3230866082d6207eb5253f2e89623b941f63 (diff) | |
parent | ccc9f5e8eaa84579da610ea0d90d18596078bac7 (diff) | |
download | ilc-bc755a4dedc520b672bc7168ff6ef9d088072d99.tar.gz ilc-bc755a4dedc520b672bc7168ff6ef9d088072d99.tar.xz ilc-bc755a4dedc520b672bc7168ff6ef9d088072d99.zip |
Merge pull request #1 from tilpner/cows
Update Event API with Option and Cow
Diffstat (limited to 'src/format/mod.rs')
-rw-r--r-- | src/format/mod.rs | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/format/mod.rs b/src/format/mod.rs index f5692a0..db86a1d 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -17,16 +17,30 @@ //! target format, all formats must allow for omittable information. use std::io::{ BufRead, Write }; +use std::borrow::Cow; -use log::Event; +use event::Event; +use context::Context; pub mod weechat3; +pub mod energymech; pub mod binary; -pub trait Encode<W> where W: Write { - fn encode(&self, output: W, event: &Event) -> ::Result<()>; +pub trait Encode<'a, W> where W: Write { + fn encode(&'a self, context: &'a Context, output: W, event: &'a Event) -> ::Result<()>; } -pub trait Decode<R, O> where R: BufRead, O: Iterator<Item = ::Result<Event>> { - fn decode(&mut self, input: R) -> O; +pub trait Decode<'a, R, O> where R: BufRead, O: Iterator<Item = ::Result<Event<'a>>> { + fn decode(&'a mut self, context: &'a Context, input: R) -> O; +} + +fn rejoin(s: &[&str], splits: &[char]) -> Cow<'static, str> { + let len = s.iter().map(|s| s.len()).sum(); + let mut out = s.iter().zip(splits.iter()).fold(String::with_capacity(len), + |mut s, (b, &split)| { s.push_str(b); s.push(split); s }); + out.pop(); Cow::Owned(out) +} + +fn strip_one(s: &str) -> String { + if s.len() >= 2 { s[1..(s.len() - 1)].to_owned() } else { String::new() } } |