From 74008b1a548ed520cc5fdf2440c92416d39b59c5 Mon Sep 17 00:00:00 2001 From: Andrea Zagli <azagli@libero.it> Date: Sat, 11 Feb 2023 11:31:02 +0100 Subject: [PATCH] Refactoring files structure. --- src/fields.rs | 155 ++++++++++++++++++++++++++++++++++++++++++ src/form.rs | 24 +++++++ src/lib.rs | 183 +------------------------------------------------- 3 files changed, 181 insertions(+), 181 deletions(-) create mode 100644 src/fields.rs create mode 100644 src/form.rs 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<super::filters::Filter>, +} + +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 %}<div class="mb-3"> + <label for="{{ name }}" class="form-label">{{ label }}</label>{% endif %} + <input type="text" class="form-control{% if help %} is-invalid{% endif %}" name="{{ name }}" id="{{ name }}" {% if value %} value="{{ value }}" {% endif %} {% if disabled %} readonly {% endif %} {% if maxlen > 0 %}maxlength="{{ maxlen }}"{% endif %} {% if invisible %}style="display: none;"{% endif %}/> + {% if help %}<div id="helpBox_{{ name }}_" class="invalid-feedback">{{ help }}</div>{% endif %} + {% if label %}</div>{% 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<FOption>, +} + +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 %}<div class="mb-3"> + <label for="{{ name }}" class="form-label">{{ label }}</label><br/>{% endif %} + {% for o in options %} + <div class="form-check{% if help %} is-invalid{% endif %}"> + <input type="radio" class="form-check-input{% if help %} is-invalid{% endif %}" name="{{ name }}" id="{{ name }}_{{ loop.index }}" value="{{ o.value }}"{% if value == o.value %} checked{% endif %}/> + <label class="form-label" for="{{ name }}_{{ loop.index }}">{{ o.label }}</label> + </div> + {% endfor %} + {% if help %}<div id="helpBox_{{ name }}_" class="invalid-feedback">{{ help }}</div>{% endif %} + {% if label %}</div>{% endif %}"#) { +Err(e) => { println!("{:?}", e) }, +_ => {}, +}; + + match f.field.tmpl.add_raw_template("field_single", r#"<div class="form-check{% if help %} is-invalid{% endif %}"> + <input type="radio" class="form-check-input{% if help %} is-invalid{% endif %}" name="{{ name }}" id="{{ name }}_{{ idx }}" value="{{ value }}"{% if checked != "" %} checked{% endif %}/> + <label class="form-label" for="{{ name }}_{{ idx }}">{{ label }}</label> + </div>"#) { +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<TFields>, +} + +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<TFields> { + &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<filters::Filter>, -} - -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 %}<div class="mb-3"> - <label for="{{ name }}" class="form-label">{{ label }}</label>{% endif %} - <input type="text" class="form-control{% if help %} is-invalid{% endif %}" name="{{ name }}" id="{{ name }}" {% if value %} value="{{ value }}" {% endif %} {% if disabled %} readonly {% endif %} {% if maxlen > 0 %}maxlength="{{ maxlen }}"{% endif %} {% if invisible %}style="display: none;"{% endif %}/> - {% if help %}<div id="helpBox_{{ name }}_" class="invalid-feedback">{{ help }}</div>{% endif %} - {% if label %}</div>{% 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<FOption>, -} - -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 %}<div class="mb-3"> - <label for="{{ name }}" class="form-label">{{ label }}</label><br/>{% endif %} - {% for o in options %} - <div class="form-check{% if help %} is-invalid{% endif %}"> - <input type="radio" class="form-check-input{% if help %} is-invalid{% endif %}" name="{{ name }}" id="{{ name }}_{{ loop.index }}" value="{{ o.value }}"{% if value == o.value %} checked{% endif %}/> - <label class="form-label" for="{{ name }}_{{ loop.index }}">{{ o.label }}</label> - </div> - {% endfor %} - {% if help %}<div id="helpBox_{{ name }}_" class="invalid-feedback">{{ help }}</div>{% endif %} - {% if label %}</div>{% endif %}"#) { -Err(e) => { println!("{:?}", e) }, -_ => {}, -}; - - match f.field.tmpl.add_raw_template("field_single", r#"<div class="form-check{% if help %} is-invalid{% endif %}"> - <input type="radio" class="form-check-input{% if help %} is-invalid{% endif %}" name="{{ name }}" id="{{ name }}_{{ idx }}" value="{{ value }}"{% if checked != "" %} checked{% endif %}/> - <label class="form-label" for="{{ name }}_{{ idx }}">{{ label }}</label> - </div>"#) { -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<TFields>, -} - -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<TFields> { - &self.fields - } -} +pub mod fields; +pub mod form; -- 2.49.0