pub struct Ruleset { /* private fields */ }
Expand description
An ordered set of Rule
s sorted by priority.
§Examples
use csv_sanity::{
Ruleset,
Rule,
TransformedRecord,
};
use csv_sanity::transformers::*;
let ruleset = {
let mut r = Ruleset::new();
r.add_rule(Rule::for_fields(&["First Name", "Last Name"], Transformers::Capitalize(
CapitalizeTransformer::new()
)));
r.add_rule(Rule::for_fields(&["Email"], Transformers::Email(
EmailTransformer::new()
)));
r
};
let headers = vec!["Id", "First Name", "Last Name", "Email"].iter().map(|s| s.to_string()).collect();
let record = vec!["1", " JOHN", "SNOW ", "\t JSNOW@EXAMPLE.COM "].iter().map(|s| s.to_string()).collect();
let transformed_record = ruleset.apply_rules(&headers, &record, 1);
assert_eq!(TransformedRecord {
field_values: vec!["1", "John", "Snow", "jsnow@example.com"].iter().map(|s| Some(s.to_string())).collect(),
errors: Vec::new(),
}, transformed_record);
Implementations§
Source§impl Ruleset
impl Ruleset
Sourcepub fn new() -> Ruleset
pub fn new() -> Ruleset
Construct a new Ruleset
with a default NoneTransformer
and TrimTransformer
global
rules.
The default trim and none rules should be appropriate for most CSV files. For CSV files
where these default rules are not desired use the Ruleset::without_default_rules
method.
Sourcepub fn without_default_rules() -> Ruleset
pub fn without_default_rules() -> Ruleset
Construct a new Ruleset
without any of the default rules.
Sourcepub fn validate_rules(
&self,
headers: &Vec<String>,
) -> Result<(), Vec<ValidationError>>
pub fn validate_rules( &self, headers: &Vec<String>, ) -> Result<(), Vec<ValidationError>>
Validate this ruleset against a CSV file by comparing it’s Rule
s against the headers.
Sourcepub fn apply_rules(
&self,
headers: &Vec<String>,
fields: &Vec<String>,
record_n: usize,
) -> TransformedRecord
pub fn apply_rules( &self, headers: &Vec<String>, fields: &Vec<String>, record_n: usize, ) -> TransformedRecord
Apply this Ruleset
to a record from a CSV file.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Ruleset
impl<'de> Deserialize<'de> for Ruleset
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Ruleset
impl !RefUnwindSafe for Ruleset
impl Send for Ruleset
impl Sync for Ruleset
impl Unpin for Ruleset
impl UnwindSafe for Ruleset
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)