diff options
author | Till Hoeppner | 2015-04-20 22:38:56 +0200 |
---|---|---|
committer | Till Hoeppner | 2015-04-20 22:38:56 +0200 |
commit | 5f1ffd753af42357c8b074c7a8bc9be2eade002e (patch) | |
tree | c8df5fc525c7bca1a4a5b91d35ad0899da377558 /build_replies.rs | |
parent | c9776e2ad7ce0173b0820dbe2487c3cf8f0f3d1c (diff) | |
download | irsc-5f1ffd753af42357c8b074c7a8bc9be2eade002e.tar.gz irsc-5f1ffd753af42357c8b074c7a8bc9be2eade002e.tar.xz irsc-5f1ffd753af42357c8b074c7a8bc9be2eade002e.zip |
Add reply generation
Diffstat (limited to 'build_replies.rs')
-rw-r--r-- | build_replies.rs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/build_replies.rs b/build_replies.rs new file mode 100644 index 0000000..a9c00e4 --- /dev/null +++ b/build_replies.rs @@ -0,0 +1,81 @@ +use std::fs::File; +use std::io::{ Read }; +use std::borrow::ToOwned; + +#[derive(Debug)] +struct Reply { + number: String, + reply: String, + doc: String +} + +fn main() { + let mut f = File::open("rfc2812_replies.txt").unwrap(); + + let mut content = String::new(); + f.read_to_string(&mut content).unwrap(); + + let mut lines = content.lines(); + let mut line = lines.next(); + + let mut replies = Vec::new(); + + while let Some(l) = line { + // if is new command + if l.chars().next().map(char::is_whitespace) == Some(false) { + let t = l.split(" ").filter(|s| !s.is_empty()).collect::<Vec<&str>>(); + assert_eq!(t.len(), 2); + replies.push(Reply { + number: t[0].to_owned(), + reply: t[1].to_owned(), + doc: String::new() + }); + } + + let len = replies.len(); + replies[len - 1].doc.push_str(l); + replies[len - 1].doc.push_str("\n"); + + line = lines.next(); + } + + println!("use ::{{ Result, IrscError }};"); + println!("use std::str::FromStr;"); + println!("use std::borrow::ToOwned;"); + + println!("#[allow(non_camel_case_types)]"); + println!("#[derive(Debug, Hash, PartialEq, Eq)]"); + println!("pub enum Reply {{"); + for r in &replies { + for l in r.doc.lines() { + println!(" /// {}", l); + } + + println!(" {} = {},\n", r.reply, r.number); + } + println!("}}\n\n"); + + println!("impl FromStr for Reply {{"); + println!(" type Err = IrscError;"); + println!(" fn from_str(s: &str) -> Result<Reply> {{"); + println!(" use self::Reply::*;"); + println!(" match s {{"); + for r in &replies { + println!(" \"{}\" => Ok({}),", r.number, r.reply); + } + println!(" _ => Err(IrscError::NotFound)"); + println!(" }}"); + println!(" }}"); + println!("}}"); + + println!("impl ToString for Reply {{"); + println!(" fn to_string(&self) -> String {{"); + println!(" use self::Reply::*;"); + println!(" match *self {{"); + for r in &replies { + println!(" {} => \"{}\".to_owned(),", r.reply, r.number); + } + println!(" }}"); + println!(" }}"); + println!("}}"); +} |