From 6ea95526bc3bc4e9589fa8378f73bc6eaee5a483 Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Tue, 21 Apr 2015 10:45:11 +0200 Subject: Move code generation code and sources to 'tool' --- tool/build_replies.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tool/build_replies.rs (limited to 'tool/build_replies.rs') diff --git a/tool/build_replies.rs b/tool/build_replies.rs new file mode 100644 index 0000000..a9c00e4 --- /dev/null +++ b/tool/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::>(); + 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 {{"); + 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!("}}"); +} -- cgit v1.2.3