extern crate zakform;
+use zakform::filters;
+
use config::{Config, ConfigError};
use serde_derive::Deserialize;
let mut fi = zakform::Field::new(zakform::FType::Text, "name");
fi.set_label("Name");
fi.set_value(form.name.as_str());
+ fi.add_filter(filters::filter_uppercase);
+ fi.filter();
f.add_field(fi);
let mut fi = zakform::Field::new(zakform::FType::TextArea, "notes");
fi.set_label("Notes");
fi.set_rows(15);
fi.set_value(form.notes.as_str());
+ fi.add_filter(filters::filter_trim);
+ fi.filter();
f.add_field(fi);
let fs = f.fields();
--- /dev/null
+pub type Filter = fn(value: &String) -> String;
+
+pub fn filter_trim(value: &String) -> String {
+ String::from(value.trim())
+}
+
+pub fn filter_uppercase(value: &String) -> String {
+ value.to_uppercase()
+}
use tera::Tera;
+pub mod filters;
+
pub enum FType {
Text,
TextArea
help: String,
value: String,
tmpl: tera::Tera,
+ filters: Vec<filters::Filter>,
}
impl Field {
help: String::new(),
value: String::new(),
tmpl: Tera::default(),
+ filters: vec![],
};
match f.ftype {
self.value = String::from(value);
}
+ pub fn add_filter(&mut self, filter: filters::Filter) {
+ self.filters.push(filter);
+ }
+
+ pub fn filter(&mut self) {
+ for f in &self.filters {
+ self.value = (f)(&self.value);
+ }
+ }
+
pub fn render(&self) -> String {
let mut s = String::new();