diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/format/binary.rs | 22 | ||||
-rw-r--r-- | src/format/mod.rs | 12 | ||||
-rw-r--r-- | src/format/msgpack.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 29 |
4 files changed, 48 insertions, 37 deletions
diff --git a/src/format/binary.rs b/src/format/binary.rs index 367cdf9..a7ae7ca 100644 --- a/src/format/binary.rs +++ b/src/format/binary.rs @@ -14,7 +14,6 @@ use std::io::{ BufRead, Write }; use std::iter::Iterator; -use std::marker::PhantomData; use event::Event; use context::Context; @@ -24,29 +23,28 @@ use bincode::{ self, SizeLimit }; pub struct Binary; -pub struct Iter<'a, R: 'a> where R: BufRead { - _phantom: PhantomData<&'a ()>, - input: R +pub struct Iter<'a> { + input: &'a mut BufRead } -impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { +impl<'a> Iterator for Iter<'a> { type Item = ::Result<Event<'a>>; fn next(&mut self) -> Option<::Result<Event<'a>>> { - Some(bincode::rustc_serialize::decode_from::<R, Event>(&mut self.input, SizeLimit::Infinite) + Some(bincode::rustc_serialize::decode_from::<_, Event>(&mut self.input, SizeLimit::Infinite) .map_err(|_| ::IlcError::BincodeDecode)) } } -impl<'a, W> Encode<'a, W> for Binary where W: Write { - fn encode(&'a self, _context: &'a Context, mut output: W, event: &'a Event) -> ::Result<()> { +impl Encode for Binary { + fn encode<'a>(&'a self, _context: &'a Context, mut output: &'a mut Write, event: &'a Event) -> ::Result<()> { bincode::rustc_serialize::encode_into(event, &mut output, SizeLimit::Infinite) .map_err(|_| ::IlcError::BincodeEncode) } } -impl<'a, R: 'a> Decode<'a, R> for Binary where R: BufRead { - type Output = Iter<'a, R>; - fn decode(&'a mut self, _context: &'a Context, input: R) -> Iter<R> { - Iter { _phantom: PhantomData, input: input } +impl Decode for Binary { + fn decode<'a>(&'a mut self, _context: &'a Context, input: &'a mut BufRead) + -> Box<Iterator<Item = ::Result<Event<'a>>> + 'a> { + Box::new(Iter { input: input }) } } diff --git a/src/format/mod.rs b/src/format/mod.rs index 370a92b..ff3a328 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -25,8 +25,8 @@ use context::Context; pub mod energymech; pub mod weechat3; -//pub mod binary; -//pub mod msgpack; +pub mod binary; +pub mod msgpack; pub trait Encode { fn encode<'a>(&'a self, context: &'a Context, output: &'a mut Write, event: &'a Event) -> ::Result<()>; @@ -48,8 +48,8 @@ pub fn decoder(format: &str) -> Option<Box<Decode>> { match format { "energymech" => Some(Box::new(energymech::Energymech)), "weechat3" => Some(Box::new(weechat3::Weechat3)), -// "binary" => Some(Box::new(binary::Binary)), -// "msgpack" => Some(Box::new(msgpack::Msgpack)), + "binary" => Some(Box::new(binary::Binary)), + "msgpack" => Some(Box::new(msgpack::Msgpack)), _ => None } } @@ -58,8 +58,8 @@ pub fn encoder(format: &str) -> Option<Box<Encode>> { match format { "energymech" => Some(Box::new(energymech::Energymech)), "weechat3" => Some(Box::new(weechat3::Weechat3)), -// "binary" => Some(Box::new(binary::Binary)), -// "msgpack" => Some(Box::new(msgpack::Msgpack)), + "binary" => Some(Box::new(binary::Binary)), + "msgpack" => Some(Box::new(msgpack::Msgpack)), _ => None } } diff --git a/src/format/msgpack.rs b/src/format/msgpack.rs index fb71b99..022e373 100644 --- a/src/format/msgpack.rs +++ b/src/format/msgpack.rs @@ -14,7 +14,6 @@ use std::io::{ BufRead, Write }; use std::iter::Iterator; -use std::marker::PhantomData; use event::Event; use context::Context; @@ -26,12 +25,11 @@ use rmp::decode::ReadError; pub struct Msgpack; -pub struct Iter<'a, R: 'a> where R: BufRead { - _phantom: PhantomData<&'a ()>, - input: R +pub struct Iter<'a> { + input: &'a mut BufRead } -impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { +impl<'a> Iterator for Iter<'a> { type Item = ::Result<Event<'a>>; fn next(&mut self) -> Option<::Result<Event<'a>>> { use msgpack::decode; @@ -43,16 +41,16 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { } } -impl<'a, W> Encode<'a, W> for Msgpack where W: Write { - fn encode(&'a self, _context: &'a Context, mut output: W, event: &'a Event) -> ::Result<()> { - event.encode(&mut Encoder::new(&mut output)) +impl Encode for Msgpack { + fn encode<'a>(&'a self, _context: &'a Context, output: &'a mut Write, event: &'a Event) -> ::Result<()> { + event.encode(&mut Encoder::new(output)) .map_err(|e| ::IlcError::MsgpackEncode(e)) } } -impl<'a, R: 'a> Decode<'a, R> for Msgpack where R: BufRead { - type Output = Iter<'a, R>; - fn decode(&'a mut self, _context: &'a Context, input: R) -> Iter<R> { - Iter { _phantom: PhantomData, input: input } +impl Decode for Msgpack { + fn decode<'a>(&'a mut self, _context: &'a Context, input: &'a mut BufRead) + -> Box<Iterator<Item = ::Result<Event<'a>>> + 'a> { + Box::new(Iter { input: input }) } } diff --git a/src/main.rs b/src/main.rs index c97d8aa..d650158 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ extern crate glob; extern crate blist; use std::process; -use std::io::{ self, Read, BufRead, BufReader, Write, BufWriter }; +use std::io::{ self, BufRead, BufReader, Write, BufWriter }; use std::fs::File; use std::error::Error; use std::str::FromStr; @@ -190,11 +190,20 @@ fn main() { } else if args.cmd_freq { struct Person { lines: u32, + alpha_lines: u32, words: u32 } - fn words(s: &str) -> u32 { - s.split_whitespace().filter(|s| !s.is_empty()).count() as u32 + fn words_alpha(s: &str) -> (u32, bool) { + let mut alpha = false; + let mut words = 0; + for w in s.split_whitespace() { + if !w.is_empty() { + words += 1; + if w.chars().any(char::is_alphabetic) { alpha = true } + } + } + (words, alpha) } fn strip_nick_prefix(s: &str) -> &str { @@ -219,12 +228,16 @@ fn main() { let nick = strip_nick_prefix(from); if stats.contains_key(nick) { let p: &mut Person = stats.get_mut(nick).unwrap(); + let (words, alpha) = words_alpha(content); p.lines += 1; - p.words += words(content); + if alpha { p.alpha_lines += 1 } + p.words += words; } else { + let (words, alpha) = words_alpha(content); stats.insert(nick.to_owned(), Person { lines: 1, - words: words(content) + alpha_lines: if alpha { 1 } else { 0 }, + words: words }); } }, @@ -235,8 +248,10 @@ fn main() { let mut stats: Vec<(String, Person)> = stats.into_iter().collect(); stats.sort_by(|&(_, ref a), &(_, ref b)| b.words.cmp(&a.words)); - for &(ref name, ref stat) in stats.iter().take(10) { - let _ = write!(&mut output, "{}:\n\tLines: {}\n\tWords: {}\n", name, stat.lines, stat.words); + for &(ref name, ref stat) in stats.iter() { + let _ = write!(&mut output, + "{}:\n\tTotal lines: {}\n\tLines without alphabetic characters: {}\n\tTotal words: {}\n\tWords per line: {}\n", + name, stat.lines, stat.lines - stat.alpha_lines, stat.words, stat.words as f32 / stat.lines as f32); } } else if args.cmd_sort { let mut decoder = force_decoder(args.flag_inf); |