From: Andrea Zagli Date: Sat, 11 Feb 2023 10:31:02 +0000 (+0100) Subject: Refactoring files structure. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=74008b1a548ed520cc5fdf2440c92416d39b59c5;p=rust%2Fzakform Refactoring files structure. --- diff --git a/src/fields.rs b/src/fields.rs new file mode 100644 index 0000000..c65e839 --- /dev/null +++ b/src/fields.rs @@ -0,0 +1,155 @@ +use serde_derive::{Serialize}; + +use std::default::Default; + +#[derive(Default)] +struct Field { + name: String, + label: String, + disabled: bool, + invisible: bool, + help: String, + value: String, + tmpl: tera::Tera, + filters: Vec, +} + +pub trait TField { + fn new(name: &str) -> Self; + + fn render(&self) -> String; +} + +#[derive(Default)] +pub struct FieldText { + field: Field, + maxlen: i32, +} + +impl TField for FieldText { + fn new(name: &str) -> Self { + let mut f: FieldText = Default::default(); + + f.field.name = String::from(name); + f.field.label = String::from(name); + + match f.field.tmpl.add_raw_template("field", r#"{% if label %}
+ {% endif %} + 0 %}maxlength="{{ maxlen }}"{% endif %} {% if invisible %}style="display: none;"{% endif %}/> + {% if help %}
{{ help }}
{% endif %} + {% if label %}
{% endif %}"#) { +Err(e) => { println!("{:?}", e) }, +_ => {}, +}; + + f + } + + fn render(&self) -> String { + let mut s = String::new(); + + let mut context = tera::Context::new(); + context.insert("name", &self.field.name); + context.insert("label", &self.field.label); + context.insert("maxlen", &self.maxlen); + context.insert("disabled", &self.field.disabled); + context.insert("invisible", &self.field.invisible); + context.insert("help", &self.field.help); + context.insert("value", &self.field.value); + s.push_str(self.field.tmpl.render("field", &context).unwrap().as_str()); + + s + } +} + +#[derive(Serialize)] +pub struct FOption { + pub value: String, + pub label: String, +} + +#[derive(Default)] +pub struct FieldRadio { + field: Field, + options: Vec, +} + +impl TField for FieldRadio { + fn new(name: &str) -> Self { + let mut f: FieldRadio = Default::default(); + + f.field.name = String::from(name); + f.field.label = String::from(name); + + match f.field.tmpl.add_raw_template("field", r#"{% if label %}
+
{% endif %} + {% for o in options %} +
+ + +
+ {% endfor %} + {% if help %}
{{ help }}
{% endif %} + {% if label %}
{% endif %}"#) { +Err(e) => { println!("{:?}", e) }, +_ => {}, +}; + + match f.field.tmpl.add_raw_template("field_single", r#"
+ + +
"#) { +Err(e) => { println!("{:?}", e) }, +_ => {}, +}; + + f + } + + fn render(&self) -> String { + let mut s = String::new(); + + let mut context = tera::Context::new(); + context.insert("name", &self.field.name); + context.insert("label", &self.field.label); + context.insert("disabled", &self.field.disabled); + context.insert("invisible", &self.field.invisible); + context.insert("help", &self.field.help); + context.insert("value", &self.field.value); + context.insert("options", &self.options); + s.push_str(self.field.tmpl.render("field", &context).unwrap().as_str()); + + s + } +} + +impl FieldRadio { + pub fn add_option(&mut self, option: FOption) { + self.options.push(option); + } + + pub fn render_single(&self, idx: usize) -> String { + let mut s = String::new(); + + let mut context = tera::Context::new(); + + context.insert("name", &self.field.name); + context.insert("help", &self.field.help); + + let opt = &self.options[idx]; + context.insert("label", &opt.label); + context.insert("value", &opt.value); + + if opt.value == self.field.value { + context.insert("checked", "checked"); + } else { + context.insert("checked", ""); + } + + context.insert("idx", &idx); + + s.push_str(self.field.tmpl.render("field_single", &context).unwrap().as_str()); + + s + } +} diff --git a/src/form.rs b/src/form.rs new file mode 100644 index 0000000..af93c41 --- /dev/null +++ b/src/form.rs @@ -0,0 +1,24 @@ +pub enum TFields { + TFieldText(super::fields::FieldText), + TFieldRadio(super::fields::FieldRadio), +} + +pub struct Form { + fields: Vec, +} + +impl Form { + pub fn new() -> Form { + Form { + fields: vec![], + } + } + + pub fn add_field(&mut self, field: TFields) { + self.fields.push(field); + } + + pub fn fields(&self) -> &Vec { + &self.fields + } +} diff --git a/src/lib.rs b/src/lib.rs index 5fe8d55..39c5808 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,182 +1,3 @@ -use serde_derive::{Serialize}; - -use std::default::Default; - pub mod filters; - -#[derive(Default)] -struct Field { - name: String, - label: String, - disabled: bool, - invisible: bool, - help: String, - value: String, - tmpl: tera::Tera, - filters: Vec, -} - -pub trait TField { - fn new(name: &str) -> Self; - - fn render(&self) -> String; -} - -#[derive(Default)] -pub struct FieldText { - field: Field, - maxlen: i32, -} - -impl TField for FieldText { - fn new(name: &str) -> Self { - let mut f: FieldText = Default::default(); - - f.field.name = String::from(name); - f.field.label = String::from(name); - - match f.field.tmpl.add_raw_template("field", r#"{% if label %}
- {% endif %} - 0 %}maxlength="{{ maxlen }}"{% endif %} {% if invisible %}style="display: none;"{% endif %}/> - {% if help %}
{{ help }}
{% endif %} - {% if label %}
{% endif %}"#) { -Err(e) => { println!("{:?}", e) }, -_ => {}, -}; - - f - } - - fn render(&self) -> String { - let mut s = String::new(); - - let mut context = tera::Context::new(); - context.insert("name", &self.field.name); - context.insert("label", &self.field.label); - context.insert("maxlen", &self.maxlen); - context.insert("disabled", &self.field.disabled); - context.insert("invisible", &self.field.invisible); - context.insert("help", &self.field.help); - context.insert("value", &self.field.value); - s.push_str(self.field.tmpl.render("field", &context).unwrap().as_str()); - - s - } -} - -#[derive(Serialize)] -pub struct FOption { - pub value: String, - pub label: String, -} - -#[derive(Default)] -pub struct FieldRadio { - field: Field, - options: Vec, -} - -impl TField for FieldRadio { - fn new(name: &str) -> Self { - let mut f: FieldRadio = Default::default(); - - f.field.name = String::from(name); - f.field.label = String::from(name); - - match f.field.tmpl.add_raw_template("field", r#"{% if label %}
-
{% endif %} - {% for o in options %} -
- - -
- {% endfor %} - {% if help %}
{{ help }}
{% endif %} - {% if label %}
{% endif %}"#) { -Err(e) => { println!("{:?}", e) }, -_ => {}, -}; - - match f.field.tmpl.add_raw_template("field_single", r#"
- - -
"#) { -Err(e) => { println!("{:?}", e) }, -_ => {}, -}; - - f - } - - fn render(&self) -> String { - let mut s = String::new(); - - let mut context = tera::Context::new(); - context.insert("name", &self.field.name); - context.insert("label", &self.field.label); - context.insert("disabled", &self.field.disabled); - context.insert("invisible", &self.field.invisible); - context.insert("help", &self.field.help); - context.insert("value", &self.field.value); - context.insert("options", &self.options); - s.push_str(self.field.tmpl.render("field", &context).unwrap().as_str()); - - s - } -} - -impl FieldRadio { - pub fn add_option(&mut self, option: FOption) { - self.options.push(option); - } - - pub fn render_single(&self, idx: usize) -> String { - let mut s = String::new(); - - let mut context = tera::Context::new(); - - context.insert("name", &self.field.name); - context.insert("help", &self.field.help); - - let opt = &self.options[idx]; - context.insert("label", &opt.label); - context.insert("value", &opt.value); - - if opt.value == self.field.value { - context.insert("checked", "checked"); - } else { - context.insert("checked", ""); - } - - context.insert("idx", &idx); - - s.push_str(self.field.tmpl.render("field_single", &context).unwrap().as_str()); - - s - } -} - -pub enum TFields { - TFieldText(FieldText), - TFieldRadio(FieldRadio), -} - -pub struct Form { - fields: Vec, -} - -impl Form { - pub fn new() -> Form { - Form { - fields: vec![], - } - } - - pub fn add_field(&mut self, field: TFields) { - self.fields.push(field); - } - - pub fn fields(&self) -> &Vec { - &self.fields - } -} +pub mod fields; +pub mod form;