From 19a778d004de584a09fa1d4c6c4bd8803ca80048 Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 11 Jun 2015 12:39:45 +0200 Subject: Some inbetween state of confusion --- src/format/binary.rs | 23 ++++---- src/format/energymech.rs | 135 +++++++++++++++++++++++++++++++++++++++++++ src/format/mod.rs | 26 +++++++-- src/format/weechat3.rs | 147 ++++++++++++++++++++++++++++------------------- 4 files changed, 257 insertions(+), 74 deletions(-) create mode 100644 src/format/energymech.rs (limited to 'src/format') diff --git a/src/format/binary.rs b/src/format/binary.rs index e8d880f..df57781 100644 --- a/src/format/binary.rs +++ b/src/format/binary.rs @@ -14,35 +14,38 @@ use std::io::{ BufRead, Write }; use std::iter::Iterator; +use std::marker::PhantomData; -use log::Event; +use event::Event; +use context::Context; use format::{ Encode, Decode }; use bincode::{ self, SizeLimit }; pub struct Binary; -pub struct Iter where R: BufRead { +pub struct Iter<'a, R: 'a> where R: BufRead { + _phantom: PhantomData<&'a ()>, input: R } -impl Iterator for Iter where R: BufRead { - type Item = ::Result; - fn next(&mut self) -> Option<::Result> { +impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { + type Item = ::Result>; + fn next(&mut self) -> Option<::Result>> { Some(bincode::decode_from::(&mut self.input, SizeLimit::Infinite) .map_err(|_| ::IlcError::BincodeDecode)) } } -impl Encode for Binary where W: Write { - fn encode(&self, mut output: W, event: &Event) -> ::Result<()> { +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<()> { bincode::encode_into(event, &mut output, SizeLimit::Infinite) .map_err(|_| ::IlcError::BincodeEncode) } } -impl Decode> for Binary where R: BufRead { - fn decode(&mut self, input: R) -> Iter { - Iter { input: input } +impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Binary where R: BufRead { + fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { + Iter { _phantom: PhantomData, input: input } } } diff --git a/src/format/energymech.rs b/src/format/energymech.rs new file mode 100644 index 0000000..a044344 --- /dev/null +++ b/src/format/energymech.rs @@ -0,0 +1,135 @@ +// Copyright 2015 Till Höppner +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::io::{ BufRead, Write }; +use std::borrow::ToOwned; +use std::iter::{ Iterator }; + +use event::{ Event, rejoin }; +use context::Context; +use format::{ Encode, Decode }; + +use l::LogLevel::Info; + +use chrono::*; + +pub struct Energymech; + +static TIME_FORMAT: &'static str = "%H:%M:%S"; + +pub struct Iter<'a, R: 'a> where R: BufRead { + context: &'a Context, + input: R, + buffer: String +} + +impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { + type Item = ::Result>; + fn next(&mut self) -> Option<::Result>> { + fn timestamp(context: &Context, time: &str) -> i64 { + context.timezone.from_local_date(&context.override_date) + .and_time(NaiveTime::from_hms(time[0..2].parse::().unwrap(), + time[3..5].parse::().unwrap(), + time[6..8].parse::().unwrap())) + .single() + .expect("Transformed log times can't be represented, due to timezone transitions") + .timestamp() + } + fn join(s: &[&str], splits: &[char]) -> String { + 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(); out + } + fn mask(s: &str) -> String { + if s.len() >= 2 { s[1..(s.len() - 1)].to_owned() } else { String::new() } + } + + loop { + self.buffer.clear(); + match self.input.read_line(&mut self.buffer) { + Ok(0) | Err(_) => return None, + Ok(_) => () + } + + let mut split_tokens: Vec = Vec::new(); + let tokens = self.buffer.split( |c: char| { + if c.is_whitespace() { split_tokens.push(c); true } else { false } + }).collect::>(); + if log_enabled!(Info) { + info!("Original: `{}`", self.buffer); + info!("Parsing: {:?}", tokens); + } + match tokens[..tokens.len() - 1].as_ref() { + [time, "*", nick, content..] => return Some(Ok(Event::Action { + from: nick.to_owned(), content: join(content, &split_tokens[3..]), + time: timestamp(&self.context, &mask(time)) + })), + [time, "***", old, "is", "now", "known", "as", new] => return Some(Ok(Event::Nick { + old: old.to_owned(), new: new.to_owned(), + time: timestamp(&self.context, &mask(time)) + })), + [time, "***", "Joins:", nick, host] => return Some(Ok(Event::Join { + nick: nick.to_owned(), mask: mask(host) + })), + [time, "***", "Quits:", nick, host, reason..] => return Some(Ok(Event::Quit { + nick: nick.to_owned(), mask: mask(host), + reason: mask(&join(reason, &split_tokens[5..])), + time: timestamp(&self.context, &mask(time)) + })), + [time, nick, content..] + if nick.starts_with('<') && nick.ends_with('>') + => return Some(Ok(Event::Msg { + from: mask(nick), content: join(content, &split_tokens[2..]), + time: timestamp(&self.context, &mask(time)) + })), + _ => () + } + } + } +} + +impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Energymech where R: BufRead { + fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { + Iter { + context: context, + input: input, + buffer: String::new() + } + } +} + +impl<'a, W> Encode<'a, W> for Energymech where W: Write { + fn encode(&'a self, context: &'a Context, mut output: W, event: &'a Event) -> ::Result<()> { + fn date(t: i64) -> String { + format!("[{}]", UTC.timestamp(t, 0).format(TIME_FORMAT)) + } + match event { + &Event::Msg { ref from, ref content, ref time } => { + try!(writeln!(&mut output, "{} <{}> {}", date(*time), from, content)) + }, + &Event::Action { ref from, ref content, ref time } => { + try!(writeln!(&mut output, "{} * {} {}", date(*time), from, content)) + }, + &Event::Nick { ref old, ref new, ref time } => { + try!(writeln!(&mut output, "{} *** {} is now known as {}", date(*time), old, new)) + }, + &Event::Quit { ref nick, ref mask, ref reason, ref time } => { + try!(writeln!(&mut output, "{} *** Quits: {} ({}) ({})", date(*time), nick, mask, reason)) + }, + _ => () + } + Ok(()) + } +} diff --git a/src/format/mod.rs b/src/format/mod.rs index f5692a0..bc1e7df 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 binary; +//pub mod energymech; +//pub mod binary; -pub trait Encode 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 where R: BufRead, O: Iterator> { - fn decode(&mut self, input: R) -> O; +pub trait Decode<'a, R, O> where R: BufRead, O: Iterator>> { + 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() } } diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index bc6e968..947bd61 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -13,11 +13,13 @@ // limitations under the License. use std::io::{ BufRead, Write }; -use std::borrow::ToOwned; +use std::borrow::{ ToOwned, Cow, IntoCow }; use std::iter::{ Iterator }; +use std::marker::PhantomData; -use log::Event; -use format::{ Encode, Decode }; +use event::{ Event, Type, Time }; +use context::Context; +use format::{ Encode, Decode, rejoin, strip_one }; use l::LogLevel::Info; @@ -27,25 +29,18 @@ pub struct Weechat3; static TIME_DATE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S"; -pub struct Iter where R: BufRead { +pub struct Iter<'a, R: 'a> where R: BufRead { + _phantom: PhantomData<&'a ()>, + context: &'a Context, input: R, buffer: String } -impl Iterator for Iter where R: BufRead { - type Item = ::Result; - fn next(&mut self) -> Option<::Result> { - fn timestamp(date: &str, time: &str) -> i64 { - UTC.datetime_from_str(&format!("{} {}", date, time), TIME_DATE_FORMAT).unwrap().timestamp() - } - fn join(s: &[&str], splits: &[char]) -> String { - 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(); out - } - fn mask(s: &str) -> String { - if s.len() >= 2 { s[1..(s.len() - 1)].to_owned() } else { String::new() } +impl<'a: 'b, 'b, R: 'a> Iterator for Iter<'a, R> where R: BufRead { + type Item = ::Result>; + fn next(&mut self) -> Option<::Result>> { + fn parse_time<'b, 'c>(c: &Context, date: &'b str, time: &'c str) -> Time { + Time::from_format(&c.timezone, &format!("{} {}", date, time), TIME_DATE_FORMAT) } loop { @@ -56,80 +51,116 @@ impl Iterator for Iter where R: BufRead { } let mut split_tokens: Vec = Vec::new(); - let tokens = self.buffer.split( |c: char| { + let tokens: Vec<&'b str> = self.buffer.split(|c: char| { if c.is_whitespace() { split_tokens.push(c); true } else { false } }).collect::>(); - if log_enabled!(Info) { + + /*if log_enabled!(Info) { info!("Original: `{}`", self.buffer); info!("Parsing: {:?}", tokens); - } - match tokens[..tokens.len() - 1].as_ref() { - [date, time, "-->", nick, host, "has", "joined", channel, _..] => return Some(Ok(Event::Join { - nick: nick.to_owned(), channel: channel.to_owned(), mask: mask(host), - time: timestamp(date, time) + }*/ + + match &tokens[..tokens.len() - 1] as &'b [&'b str] { + /*[date, time, "-->", nick, host, "has", "joined", channel, _..] + => return Some(Ok(Event { + ty: Type::Join { + nick: nick.to_owned(), + mask: Some(strip_one(host)), + time: timestamp(date, time) + }, + channel: Some(channel.into_cow()), })), - [date, time, "<--", nick, host, "has", "left", channel, reason..] => return Some(Ok(Event::Part { - nick: nick.to_owned(), channel: channel.to_owned(), mask: mask(host), - reason: mask(&join(reason, &split_tokens[8..])), time: timestamp(date, time) + [date, time, "<--", nick, host, "has", "left", channel, reason..] + => return Some(Ok(Event { + ty: Type::Part { + nick: nick.to_owned(), + mask: Some(strip_one(host)), + reason: Some(strip_one(&rejoin(reason, &split_tokens[8..]))), + }, + channel: Some(channel.to_owned()), + time: timestamp(date, time) })), - [date, time, "<--", nick, host, "has", "quit", reason..] => return Some(Ok(Event::Quit { - nick: nick.to_owned(), mask: mask(host), - reason: mask(&join(reason, &split_tokens[7..])), time: timestamp(date, time) + [date, time, "<--", nick, host, "has", "quit", reason..] + => return Some(Ok(Event { + ty: Type::Quit { + nick: nick.to_owned(), + mask: Some(strip_one(host)), + reason: Some(strip_one(&rejoin(reason, &split_tokens[7..]))), + } })), [date, time, "--", notice, content..] if notice.starts_with("Notice(") - => return Some(Ok(Event::Notice { - nick: notice["Notice(".len()..notice.len() - 2].to_owned(), - content: join(content, &split_tokens[4..]), - time: timestamp(date, time) + => return Some(Ok(Event { + ty: Type::Notice { + nick: notice["Notice(".len()..notice.len() - 2].to_owned(), + content: rejoin(content, &split_tokens[4..]), + time: timestamp(date, time) + } })), - [date, time, "--", "irc:", "disconnected", "from", "server", _..] => return Some(Ok(Event::Disconnect { - time: timestamp(date, time) + [date, time, "--", "irc:", "disconnected", "from", "server", _..] + => return Some(Ok(Event { + ty: Type::Disconnect { + time: timestamp(date, time) + } })), [date, time, "--", nick, verb, "now", "known", "as", new_nick] if verb == "is" || verb == "are" - => return Some(Ok(Event::Nick { + => return Some(Ok(Event { + ty: Type::Nick { old: nick.to_owned(), new: new_nick.to_owned(), time: timestamp(date, time) - })), + } + })),*/ [date, time, sp, "*", nick, msg..] - if sp.is_empty() - => return Some(Ok(Event::Action { - from: nick.to_owned(), content: join(msg, &split_tokens[5..]), - time: timestamp(date, time) - })), - [date, time, nick, msg..] => return Some(Ok(Event::Msg { - from: nick.to_owned(), - content: join(msg, &split_tokens[3..]), - time: timestamp(date, time) + if sp.clone().is_empty() + => return Some(Ok(Event { + ty: Type::Action { + from: nick.clone().into_cow(), + content: rejoin(msg, &split_tokens[5..]), + }, + time: parse_time(&self.context, &date.clone().to_owned(), &time.clone().to_owned()), + channel: None })), + /*[date, time, nick, msg..] + => return Some(Ok(Event { + ty: Type::Msg { + from: nick.into(), + content: rejoin(msg, &split_tokens[3..]), + }, + time: parse_time(&self.context, &date, &time), + channel: None + })),*/ _ => () } } } } -impl Decode> for Weechat3 where R: BufRead { - fn decode(&mut self, input: R) -> Iter { +impl<'a, R: 'a> Decode<'static, R, Iter<'a, R>> for Weechat3 where R: BufRead { + fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { Iter { + _phantom: PhantomData, + context: context, input: input, buffer: String::new() } } } -impl Encode for Weechat3 where W: Write { - fn encode(&self, mut output: W, event: &Event) -> ::Result<()> { +impl<'a, W> Encode<'a, W> for Weechat3 where W: Write { + fn encode(&'a self, context: &'a Context, mut output: W, event: &'a Event) -> ::Result<()> { fn date(t: i64) -> String { format!("{}", UTC.timestamp(t, 0).format(TIME_DATE_FORMAT)) } match event { - &Event::Msg { ref from, ref content, ref time } => { - try!(writeln!(&mut output, "{}\t{}\t{}", date(*time), from, content)) + &Event { ty: Type::Msg { ref from, ref content, .. }, ref time, .. } => { + try!(writeln!(&mut output, "{}\t{}\t{}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) }, - &Event::Action { ref from, ref content, ref time } => { - try!(writeln!(&mut output, "{}\t *\t{} {}", date(*time), from, content)) + &Event { ty: Type::Action { ref from, ref content, .. }, ref time, .. } => { + try!(writeln!(&mut output, "{}\t *\t{} {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) }, - &Event::Join { ref nick, ref mask, ref channel, ref time } => { + /*&Event::Join { ref nick, ref mask, ref channel, ref time } => { try!(writeln!(&mut output, "{}\t-->\t{} ({}) has joined {}", date(*time), nick, mask, channel)) }, @@ -153,7 +184,7 @@ impl Encode for Weechat3 where W: Write { }, &Event::Notice { ref nick, ref content, ref time } => { try!(writeln!(&mut output, "{}\t--\tNotice({}): {}", date(*time), nick, content)) - }, + },*/ _ => () } Ok(()) -- cgit v1.2.3 From f429b909db8624d9202e0e43c9732e957a7e93bc Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 11 Jun 2015 15:55:32 +0200 Subject: Reduction? --- src/format/weechat3.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/format') diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index 947bd61..164afc4 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -30,28 +30,27 @@ pub struct Weechat3; static TIME_DATE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S"; pub struct Iter<'a, R: 'a> where R: BufRead { - _phantom: PhantomData<&'a ()>, context: &'a Context, input: R, buffer: String } -impl<'a: 'b, 'b, R: 'a> Iterator for Iter<'a, R> where R: BufRead { +impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { type Item = ::Result>; fn next(&mut self) -> Option<::Result>> { - fn parse_time<'b, 'c>(c: &Context, date: &'b str, time: &'c str) -> Time { + fn parse_time(c: &Context, date: &str, time: &str) -> Time { Time::from_format(&c.timezone, &format!("{} {}", date, time), TIME_DATE_FORMAT) } loop { - self.buffer.clear(); - match self.input.read_line(&mut self.buffer) { + let mut buffer = String::new(); + match self.input.read_line(&mut buffer) { Ok(0) | Err(_) => return None, Ok(_) => () } let mut split_tokens: Vec = Vec::new(); - let tokens: Vec<&'b str> = self.buffer.split(|c: char| { + let tokens = buffer.split(|c: char| { if c.is_whitespace() { split_tokens.push(c); true } else { false } }).collect::>(); @@ -60,7 +59,7 @@ impl<'a: 'b, 'b, R: 'a> Iterator for Iter<'a, R> where R: BufRead { info!("Parsing: {:?}", tokens); }*/ - match &tokens[..tokens.len() - 1] as &'b [&'b str] { + match &tokens[..tokens.len() - 1] { /*[date, time, "-->", nick, host, "has", "joined", channel, _..] => return Some(Ok(Event { ty: Type::Join { @@ -138,7 +137,6 @@ impl<'a: 'b, 'b, R: 'a> Iterator for Iter<'a, R> where R: BufRead { impl<'a, R: 'a> Decode<'static, R, Iter<'a, R>> for Weechat3 where R: BufRead { fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { Iter { - _phantom: PhantomData, context: context, input: input, buffer: String::new() -- cgit v1.2.3 From abf8d8c5dad168a5f4f79c41200b0879726b9bde Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 11 Jun 2015 17:23:07 +0200 Subject: Figure out that .to_owned().into() is needed, instead of .into() --- src/format/weechat3.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/format') diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index 164afc4..fcb85dd 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -113,21 +113,21 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { if sp.clone().is_empty() => return Some(Ok(Event { ty: Type::Action { - from: nick.clone().into_cow(), + from: nick.to_owned().into(), content: rejoin(msg, &split_tokens[5..]), }, time: parse_time(&self.context, &date.clone().to_owned(), &time.clone().to_owned()), channel: None })), - /*[date, time, nick, msg..] + [date, time, nick, msg..] => return Some(Ok(Event { ty: Type::Msg { - from: nick.into(), + from: nick.to_owned().into(), content: rejoin(msg, &split_tokens[3..]), }, time: parse_time(&self.context, &date, &time), channel: None - })),*/ + })), _ => () } } -- cgit v1.2.3 From f67492f582215d177a7a5bb7c45c0f7e01628a7a Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 11 Jun 2015 18:08:17 +0200 Subject: Adapt weechat3 to new Event API --- src/format/weechat3.rs | 102 ++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 44 deletions(-) (limited to 'src/format') diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index fcb85dd..1534166 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -54,61 +54,67 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { if c.is_whitespace() { split_tokens.push(c); true } else { false } }).collect::>(); - /*if log_enabled!(Info) { + if log_enabled!(Info) { info!("Original: `{}`", self.buffer); info!("Parsing: {:?}", tokens); - }*/ + } match &tokens[..tokens.len() - 1] { - /*[date, time, "-->", nick, host, "has", "joined", channel, _..] + [date, time, "-->", nick, host, "has", "joined", channel, _..] => return Some(Ok(Event { ty: Type::Join { - nick: nick.to_owned(), - mask: Some(strip_one(host)), - time: timestamp(date, time) + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), }, - channel: Some(channel.into_cow()), + channel: Some(channel.to_owned().into()), + time: parse_time(&self.context, date, time) })), [date, time, "<--", nick, host, "has", "left", channel, reason..] => return Some(Ok(Event { ty: Type::Part { - nick: nick.to_owned(), - mask: Some(strip_one(host)), - reason: Some(strip_one(&rejoin(reason, &split_tokens[8..]))), + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[8..])).into()), }, - channel: Some(channel.to_owned()), - time: timestamp(date, time) + channel: Some(channel.to_owned().into()), + time: parse_time(&self.context, date, time) })), [date, time, "<--", nick, host, "has", "quit", reason..] => return Some(Ok(Event { ty: Type::Quit { - nick: nick.to_owned(), - mask: Some(strip_one(host)), - reason: Some(strip_one(&rejoin(reason, &split_tokens[7..]))), - } + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[7..])).into()), + }, + time: parse_time(&self.context, date, time), + channel: None })), [date, time, "--", notice, content..] if notice.starts_with("Notice(") => return Some(Ok(Event { ty: Type::Notice { - nick: notice["Notice(".len()..notice.len() - 2].to_owned(), + from: notice["Notice(".len()..notice.len() - 2].to_owned().into(), content: rejoin(content, &split_tokens[4..]), - time: timestamp(date, time) - } + }, + time: parse_time(&self.context, date, time), + channel: None })), [date, time, "--", "irc:", "disconnected", "from", "server", _..] => return Some(Ok(Event { - ty: Type::Disconnect { - time: timestamp(date, time) - } + ty: Type::Disconnect, + time: parse_time(&self.context, date, time), + channel: None })), [date, time, "--", nick, verb, "now", "known", "as", new_nick] if verb == "is" || verb == "are" => return Some(Ok(Event { ty: Type::Nick { - old: nick.to_owned(), new: new_nick.to_owned(), time: timestamp(date, time) - } - })),*/ + old_nick: nick.to_owned().into(), + new_nick: new_nick.to_owned().into() + }, + time: parse_time(&self.context, date, time), + channel: None + })), [date, time, sp, "*", nick, msg..] if sp.clone().is_empty() => return Some(Ok(Event { @@ -116,7 +122,7 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { from: nick.to_owned().into(), content: rejoin(msg, &split_tokens[5..]), }, - time: parse_time(&self.context, &date.clone().to_owned(), &time.clone().to_owned()), + time: parse_time(&self.context, date, time), channel: None })), [date, time, nick, msg..] @@ -125,7 +131,7 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { from: nick.to_owned().into(), content: rejoin(msg, &split_tokens[3..]), }, - time: parse_time(&self.context, &date, &time), + time: parse_time(&self.context, date, time), channel: None })), _ => () @@ -152,37 +158,45 @@ impl<'a, W> Encode<'a, W> for Weechat3 where W: Write { match event { &Event { ty: Type::Msg { ref from, ref content, .. }, ref time, .. } => { try!(writeln!(&mut output, "{}\t{}\t{}", - time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) }, &Event { ty: Type::Action { ref from, ref content, .. }, ref time, .. } => { try!(writeln!(&mut output, "{}\t *\t{} {}", - time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) }, - /*&Event::Join { ref nick, ref mask, ref channel, ref time } => { + &Event { ty: Type::Join { ref nick, ref mask, .. }, ref channel, ref time } => { try!(writeln!(&mut output, "{}\t-->\t{} ({}) has joined {}", - date(*time), nick, mask, channel)) + time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, + mask.as_ref().expect("Hostmask not present, but required."), + channel.as_ref().expect("Channel not present, but required."))) }, - &Event::Part { ref nick, ref mask, ref channel, ref time, ref reason } => { + &Event { ty: Type::Part { ref nick, ref mask, ref reason }, ref channel, ref time } => { try!(write!(&mut output, "{}\t<--\t{} ({}) has left {}", - date(*time), nick, mask, channel)); - if reason.len() > 0 { - try!(write!(&mut output, " ({})", reason)); + time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, + mask.as_ref().expect("Hostmask not present, but required."), + channel.as_ref().expect("Channel not present, but required."))); + if reason.is_some() && reason.as_ref().unwrap().len() > 0 { + try!(write!(&mut output, " ({})", reason.as_ref().unwrap())); } try!(write!(&mut output, "\n")) }, - &Event::Quit { ref nick, ref mask, ref time, ref reason } => { - try!(write!(&mut output, "{}\t<--\t{} ({}) has quit", date(*time), nick, mask)); - if reason.len() > 0 { - try!(write!(&mut output, " ({})", reason)); + &Event { ty: Type::Quit { ref nick, ref mask, ref reason }, ref time, .. } => { + try!(write!(&mut output, "{}\t<--\t{} ({}) has quit", + time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, + mask.as_ref().expect("Hostmask not present, but required."))); + if reason.is_some() && reason.as_ref().unwrap().len() > 0 { + try!(write!(&mut output, " ({})", reason.as_ref().unwrap())); } try!(write!(&mut output, "\n")) }, - &Event::Disconnect { ref time } => { - try!(writeln!(&mut output, "{}\t--\tirc: disconnected from server", date(*time))) + &Event { ty: Type::Disconnect, ref time, .. } => { + try!(writeln!(&mut output, "{}\t--\tirc: disconnected from server", + time.with_format(&context.timezone, TIME_DATE_FORMAT))) + }, + &Event { ty: Type::Notice { ref from, ref content }, ref time, .. } => { + try!(writeln!(&mut output, "{}\t--\tNotice({}): {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) }, - &Event::Notice { ref nick, ref content, ref time } => { - try!(writeln!(&mut output, "{}\t--\tNotice({}): {}", date(*time), nick, content)) - },*/ _ => () } Ok(()) -- cgit v1.2.3 From 27e53e57777fac2fe47f1a2610d0122dc60ed97d Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 11 Jun 2015 18:28:44 +0200 Subject: Adapt energymech to new Event API --- src/format/energymech.rs | 100 ++++++++++++++++++++++++++++------------------- src/format/mod.rs | 2 +- src/format/weechat3.rs | 6 +-- 3 files changed, 63 insertions(+), 45 deletions(-) (limited to 'src/format') diff --git a/src/format/energymech.rs b/src/format/energymech.rs index a044344..c52a654 100644 --- a/src/format/energymech.rs +++ b/src/format/energymech.rs @@ -13,12 +13,12 @@ // limitations under the License. use std::io::{ BufRead, Write }; -use std::borrow::ToOwned; +use std::borrow::{ ToOwned, Cow }; use std::iter::{ Iterator }; -use event::{ Event, rejoin }; +use event::{ Event, Type, Time }; use context::Context; -use format::{ Encode, Decode }; +use format::{ Encode, Decode, rejoin, strip_one }; use l::LogLevel::Info; @@ -37,23 +37,14 @@ pub struct Iter<'a, R: 'a> where R: BufRead { impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { type Item = ::Result>; fn next(&mut self) -> Option<::Result>> { - fn timestamp(context: &Context, time: &str) -> i64 { - context.timezone.from_local_date(&context.override_date) - .and_time(NaiveTime::from_hms(time[0..2].parse::().unwrap(), - time[3..5].parse::().unwrap(), - time[6..8].parse::().unwrap())) + fn parse_time(context: &Context, time: &str) -> Time { + Time::Timestamp(context.timezone.from_local_date(&context.override_date) + .and_time(NaiveTime::from_hms(time[1..3].parse::().unwrap(), + time[4..6].parse::().unwrap(), + time[7..9].parse::().unwrap())) .single() .expect("Transformed log times can't be represented, due to timezone transitions") - .timestamp() - } - fn join(s: &[&str], splits: &[char]) -> String { - 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(); out - } - fn mask(s: &str) -> String { - if s.len() >= 2 { s[1..(s.len() - 1)].to_owned() } else { String::new() } + .timestamp()) } loop { @@ -72,27 +63,48 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { info!("Parsing: {:?}", tokens); } match tokens[..tokens.len() - 1].as_ref() { - [time, "*", nick, content..] => return Some(Ok(Event::Action { - from: nick.to_owned(), content: join(content, &split_tokens[3..]), - time: timestamp(&self.context, &mask(time)) + [time, "*", nick, content..] => return Some(Ok(Event { + ty: Type::Action { + from: nick.to_owned().into(), + content: rejoin(content, &split_tokens[3..]) + }, + time: parse_time(&self.context, time), + channel: None })), - [time, "***", old, "is", "now", "known", "as", new] => return Some(Ok(Event::Nick { - old: old.to_owned(), new: new.to_owned(), - time: timestamp(&self.context, &mask(time)) + [time, "***", old, "is", "now", "known", "as", new] => return Some(Ok(Event { + ty: Type::Nick { + old_nick: old.to_owned().into(), + new_nick: new.to_owned().into() + }, + time: parse_time(&self.context, time), + channel: None })), - [time, "***", "Joins:", nick, host] => return Some(Ok(Event::Join { - nick: nick.to_owned(), mask: mask(host) + [time, "***", "Joins:", nick, host] => return Some(Ok(Event { + ty: Type::Join { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()) + }, + time: parse_time(&self.context, time), + channel: None })), - [time, "***", "Quits:", nick, host, reason..] => return Some(Ok(Event::Quit { - nick: nick.to_owned(), mask: mask(host), - reason: mask(&join(reason, &split_tokens[5..])), - time: timestamp(&self.context, &mask(time)) + [time, "***", "Quits:", nick, host, reason..] => return Some(Ok(Event { + ty: Type::Quit { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[5..])).into()) + }, + time: parse_time(&self.context, time), + channel: None })), [time, nick, content..] if nick.starts_with('<') && nick.ends_with('>') - => return Some(Ok(Event::Msg { - from: mask(nick), content: join(content, &split_tokens[2..]), - time: timestamp(&self.context, &mask(time)) + => return Some(Ok(Event { + ty: Type::Msg { + from: strip_one(nick).into(), + content: rejoin(content, &split_tokens[2..]) + }, + time: parse_time(&self.context, time), + channel: None })), _ => () } @@ -116,17 +128,23 @@ impl<'a, W> Encode<'a, W> for Energymech where W: Write { format!("[{}]", UTC.timestamp(t, 0).format(TIME_FORMAT)) } match event { - &Event::Msg { ref from, ref content, ref time } => { - try!(writeln!(&mut output, "{} <{}> {}", date(*time), from, content)) + &Event { ty: Type::Msg { ref from, ref content }, ref time, .. } => { + try!(writeln!(&mut output, "{} <{}> {}", + time.with_format(&context.timezone, TIME_FORMAT), from, content)) }, - &Event::Action { ref from, ref content, ref time } => { - try!(writeln!(&mut output, "{} * {} {}", date(*time), from, content)) + &Event { ty: Type::Action { ref from, ref content }, ref time, .. } => { + try!(writeln!(&mut output, "{} * {} {}", + time.with_format(&context.timezone, TIME_FORMAT), from, content)) }, - &Event::Nick { ref old, ref new, ref time } => { - try!(writeln!(&mut output, "{} *** {} is now known as {}", date(*time), old, new)) + &Event { ty: Type::Nick { ref old_nick, ref new_nick }, ref time, .. } => { + try!(writeln!(&mut output, "{} *** {} is now known as {}", + time.with_format(&context.timezone, TIME_FORMAT), old_nick, new_nick)) }, - &Event::Quit { ref nick, ref mask, ref reason, ref time } => { - try!(writeln!(&mut output, "{} *** Quits: {} ({}) ({})", date(*time), nick, mask, reason)) + &Event { ty: Type::Quit { ref nick, ref mask, ref reason }, ref time, .. } => { + try!(writeln!(&mut output, "{} *** Quits: {} ({}) ({})", + time.with_format(&context.timezone, TIME_FORMAT), nick, + mask.as_ref().expect("Mask not present, but required."), + reason.as_ref().expect("Reason not present, but required."))) }, _ => () } diff --git a/src/format/mod.rs b/src/format/mod.rs index bc1e7df..e6c4897 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -23,7 +23,7 @@ use event::Event; use context::Context; pub mod weechat3; -//pub mod energymech; +pub mod energymech; //pub mod binary; pub trait Encode<'a, W> where W: Write { diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index 1534166..6ff5d5a 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -43,14 +43,14 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { } loop { - let mut buffer = String::new(); - match self.input.read_line(&mut buffer) { + self.buffer.clear(); + match self.input.read_line(&mut self.buffer) { Ok(0) | Err(_) => return None, Ok(_) => () } let mut split_tokens: Vec = Vec::new(); - let tokens = buffer.split(|c: char| { + let tokens = self.buffer.split(|c: char| { if c.is_whitespace() { split_tokens.push(c); true } else { false } }).collect::>(); -- cgit v1.2.3 From 8361e508303d1573db7a0a5e892cfe2bdf42ffbb Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 11 Jun 2015 18:29:25 +0200 Subject: Re-enable binary --- src/format/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/format') diff --git a/src/format/mod.rs b/src/format/mod.rs index e6c4897..db86a1d 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -24,7 +24,7 @@ use context::Context; pub mod weechat3; pub mod energymech; -//pub mod binary; +pub mod binary; pub trait Encode<'a, W> where W: Write { fn encode(&'a self, context: &'a Context, output: W, event: &'a Event) -> ::Result<()>; -- cgit v1.2.3 From ccc9f5e8eaa84579da610ea0d90d18596078bac7 Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Thu, 11 Jun 2015 20:56:59 +0200 Subject: Fix binaries and eliminate warnings --- src/format/binary.rs | 4 ++-- src/format/energymech.rs | 15 ++++++--------- src/format/weechat3.rs | 14 ++++---------- 3 files changed, 12 insertions(+), 21 deletions(-) (limited to 'src/format') diff --git a/src/format/binary.rs b/src/format/binary.rs index df57781..4c2e151 100644 --- a/src/format/binary.rs +++ b/src/format/binary.rs @@ -38,14 +38,14 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { } 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<()> { + fn encode(&'a self, _context: &'a Context, mut output: W, event: &'a Event) -> ::Result<()> { bincode::encode_into(event, &mut output, SizeLimit::Infinite) .map_err(|_| ::IlcError::BincodeEncode) } } impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Binary where R: BufRead { - fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { + fn decode(&'a mut self, _context: &'a Context, input: R) -> Iter { Iter { _phantom: PhantomData, input: input } } } diff --git a/src/format/energymech.rs b/src/format/energymech.rs index c52a654..e6f7d80 100644 --- a/src/format/energymech.rs +++ b/src/format/energymech.rs @@ -13,7 +13,7 @@ // limitations under the License. use std::io::{ BufRead, Write }; -use std::borrow::{ ToOwned, Cow }; +use std::borrow::{ ToOwned }; use std::iter::{ Iterator }; use event::{ Event, Type, Time }; @@ -62,7 +62,7 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { info!("Original: `{}`", self.buffer); info!("Parsing: {:?}", tokens); } - match tokens[..tokens.len() - 1].as_ref() { + match &tokens[..tokens.len() - 1] { [time, "*", nick, content..] => return Some(Ok(Event { ty: Type::Action { from: nick.to_owned().into(), @@ -124,24 +124,21 @@ impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Energymech where R: BufRead { impl<'a, W> Encode<'a, W> for Energymech where W: Write { fn encode(&'a self, context: &'a Context, mut output: W, event: &'a Event) -> ::Result<()> { - fn date(t: i64) -> String { - format!("[{}]", UTC.timestamp(t, 0).format(TIME_FORMAT)) - } match event { &Event { ty: Type::Msg { ref from, ref content }, ref time, .. } => { - try!(writeln!(&mut output, "{} <{}> {}", + try!(writeln!(&mut output, "[{}] <{}> {}", time.with_format(&context.timezone, TIME_FORMAT), from, content)) }, &Event { ty: Type::Action { ref from, ref content }, ref time, .. } => { - try!(writeln!(&mut output, "{} * {} {}", + try!(writeln!(&mut output, "[{}] * {} {}", time.with_format(&context.timezone, TIME_FORMAT), from, content)) }, &Event { ty: Type::Nick { ref old_nick, ref new_nick }, ref time, .. } => { - try!(writeln!(&mut output, "{} *** {} is now known as {}", + try!(writeln!(&mut output, "[{}] *** {} is now known as {}", time.with_format(&context.timezone, TIME_FORMAT), old_nick, new_nick)) }, &Event { ty: Type::Quit { ref nick, ref mask, ref reason }, ref time, .. } => { - try!(writeln!(&mut output, "{} *** Quits: {} ({}) ({})", + try!(writeln!(&mut output, "[{}] *** Quits: {} ({}) ({})", time.with_format(&context.timezone, TIME_FORMAT), nick, mask.as_ref().expect("Mask not present, but required."), reason.as_ref().expect("Reason not present, but required."))) diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index 6ff5d5a..20ee1dd 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -13,9 +13,8 @@ // limitations under the License. use std::io::{ BufRead, Write }; -use std::borrow::{ ToOwned, Cow, IntoCow }; +use std::borrow::{ ToOwned }; use std::iter::{ Iterator }; -use std::marker::PhantomData; use event::{ Event, Type, Time }; use context::Context; @@ -23,8 +22,6 @@ use format::{ Encode, Decode, rejoin, strip_one }; use l::LogLevel::Info; -use chrono::*; - pub struct Weechat3; static TIME_DATE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S"; @@ -36,8 +33,8 @@ pub struct Iter<'a, R: 'a> where R: BufRead { } impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { - type Item = ::Result>; - fn next(&mut self) -> Option<::Result>> { + type Item = ::Result>; + fn next(&mut self) -> Option<::Result>> { fn parse_time(c: &Context, date: &str, time: &str) -> Time { Time::from_format(&c.timezone, &format!("{} {}", date, time), TIME_DATE_FORMAT) } @@ -140,7 +137,7 @@ impl<'a, R: 'a> Iterator for Iter<'a, R> where R: BufRead { } } -impl<'a, R: 'a> Decode<'static, R, Iter<'a, R>> for Weechat3 where R: BufRead { +impl<'a, R: 'a> Decode<'a, R, Iter<'a, R>> for Weechat3 where R: BufRead { fn decode(&'a mut self, context: &'a Context, input: R) -> Iter { Iter { context: context, @@ -152,9 +149,6 @@ impl<'a, R: 'a> Decode<'static, R, Iter<'a, R>> for Weechat3 where R: BufRead { impl<'a, W> Encode<'a, W> for Weechat3 where W: Write { fn encode(&'a self, context: &'a Context, mut output: W, event: &'a Event) -> ::Result<()> { - fn date(t: i64) -> String { - format!("{}", UTC.timestamp(t, 0).format(TIME_DATE_FORMAT)) - } match event { &Event { ty: Type::Msg { ref from, ref content, .. }, ref time, .. } => { try!(writeln!(&mut output, "{}\t{}\t{}", -- cgit v1.2.3