use std::{
env,
- fs
+ fs,
+ fs::{File},
+ io::{self, Write}
};
use quick_xml::de::from_str;
pub content: String,
}
+fn get_output(args: &Args) -> Box<dyn io::Write> {
+ if args.output_file == "stdout" {
+ Box::new(io::stdout())
+ } else {
+ Box::new(File::create(&args.output_file).expect("Cannot create file"))
+ }
+}
+
fn main() {
let args = Args::parse();
- let content = fs::read_to_string(args.input_file).expect("something went wrong reading the file");
+ // input
+ let content = fs::read_to_string(&args.input_file).expect("something went wrong reading the file");
+
+ // output
+ let mut output = get_output(&args);
let form: ZakForm = from_str(&content).unwrap();
println!("{:?}", form);
- println!("let mut f = zakform::form::Form::new();");
+ writeln!(output, "let mut f = zakform::form::Form::new();");
for e in form.elements {
- println!("");
+ writeln!(output, "");
match e.etype.as_str() {
- "zak_form_cgi_form_element_text" => { println!(r#"let mut fi = zakform::fields::FieldText::new("{}")"#, e.id); },
- "zak_form_cgi_form_element_text_area" => { println!(r#"let mut fi = zakform::fields::FieldTextArea::new("{}")"#, e.id); },
- "zak_form_cgi_form_element_radio" => { println!(r#"let mut fi = zakform::fields::FieldRadio::new("{}")"#, e.id); },
- "zak_form_cgi_form_element_select" => { println!(r#"let mut fi = zakform::fields::FieldSelect::new("{}")"#, e.id); },
- "zak_form_cgi_form_element_submit" => { println!(r#"let mut fi = zakform::fields::FieldButton::new("{}")"#, e.id); },
+ "zak_form_cgi_form_element_text" => { writeln!(output, r#"let mut fi = zakform::fields::FieldText::new("{}")"#, e.id); },
+ "zak_form_cgi_form_element_text_area" => { writeln!(output, r#"let mut fi = zakform::fields::FieldTextArea::new("{}")"#, e.id); },
+ "zak_form_cgi_form_element_radio" => { writeln!(output, r#"let mut fi = zakform::fields::FieldRadio::new("{}")"#, e.id); },
+ "zak_form_cgi_form_element_select" => { writeln!(output, r#"let mut fi = zakform::fields::FieldSelect::new("{}")"#, e.id); },
+ "zak_form_cgi_form_element_submit" => { writeln!(output, r#"let mut fi = zakform::fields::FieldButton::new("{}")"#, e.id); },
_ => { continue; },
};
if let Some(l) = e.label {
if l != "" {
- println!(r#"fi.set_label("{}")"#, l);
+ writeln!(output, r#"fi.set_label("{}")"#, l);
}
}
if e.max_length > 0 {
- println!("fi.set_maxlen({})", e.max_length);
+ writeln!(output, "fi.set_maxlen({})", e.max_length);
}
if e.rows > 0 {
- println!("fi.set_rows({})", e.rows);
+ writeln!(output, "fi.set_rows({})", e.rows);
}
match e.format {
};
for option in e.options {
- println!(r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.id, option.text);
+ writeln!(output, r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.id, option.text);
}
match e.zakcgi_options {
Some(zako) => {
for option in zako.options {
- println!(r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.value, option.content);
+ writeln!(output, r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.value, option.content);
}
},
None => { },
}
if e.to_load == "FALSE" {
- println!("fi.set_to_load(false)");
+ writeln!(output, "fi.set_to_load(false)");
}
if e.to_save == "FALSE" {
- println!("fi.set_to_save(false)");
+ writeln!(output, "fi.set_to_save(false)");
}
if e.editable == "FALSE" {
- println!("fi.set_disabled(true)");
+ writeln!(output, "fi.set_disabled(true)");
}
for filter in e.filters {
match filter.ftype.as_str() {
- "zak_form_element_filter_trim" => { println!("fi.add_filter(zakform::filters::filter_trim);"); },
+ "zak_form_element_filter_trim" => { writeln!(output, "fi.add_filter(zakform::filters::filter_trim);"); },
"zak_form_element_filter_luc" => {
match filter.text.as_str() {
- "upper" => { println!("fi.add_filter(zakform::filters::filter_uppercase);"); },
- "lower" => { println!("fi.add_filter(zakform::filters::filter_lowercase);"); },
- "camel" => { println!("fi.add_filter(zakform::filters::filter_capitalcase);"); },
+ "upper" => { writeln!(output, "fi.add_filter(zakform::filters::filter_uppercase);"); },
+ "lower" => { writeln!(output, "fi.add_filter(zakform::filters::filter_lowercase);"); },
+ "camel" => { writeln!(output, "fi.add_filter(zakform::filters::filter_capitalcase);"); },
_ => { },
};
},
};
}
- println!("f.add_field(Box::new(fi);");
+ writeln!(output, "f.add_field(Box::new(fi);");
}
}