Skip to main content

Set

Struct Set 

Source
pub struct Set<W: Warning>(/* private fields */);
Expand description

A set of Warnings transported through the system using a Caveat.

Implementations§

Source§

impl Set<Warning>

Source

pub fn unexpected_fields(&self) -> PathSet<'_>

Collect the field paths of all Warning::UnexpectedField warnings into a set of json::Paths.

Source

pub fn missing_fields(&self) -> PathSet<'_>

Collect the field paths of all Warning::MissingField warnings into a set of json::Paths.

Source

pub fn remove_unexpected_fields(&mut self)

Remove all Warning::UnexpectedField warnings from the set.

Source

pub fn remove_missing_fields(&mut self)

Remove all Warning::MissingField warnings from the set.

Source

pub fn remove_type_mismatches(&mut self)

Remove all Warning::TypeMismatch warnings from the set.

Source

pub fn remove_null_fields(&mut self)

Remove all Warning::NullField warnings from the set.

Source

pub fn remove_cardinalities(&mut self)

Remove all Warning::Cardinality warnings from the set.

Source

pub fn remove_string_too_longs(&mut self)

Remove all Warning::StringTooLong warnings from the set.

Source§

impl<W: Warning> Set<W>

Source

pub fn is_empty(&self) -> bool

Return true if the Warning set is empty.

Examples found in repository?
examples/generate_cdr_from_tariff.rs (line 62)
61fn print_warnings(warnings: &warning::Set<generate::Warning>) {
62    if warnings.is_empty() {
63        return;
64    }
65
66    eprintln!(
67        "WARN: {} warnings from the linting:\n {}",
68        warnings.len_warnings(),
69        warning::SetWriter::new(warnings)
70    );
71}
More examples
Hide additional examples
examples/lint_tariff.rs (line 23)
22fn print_schema_warnings(warnings: &warning::Set<schema::Warning>) {
23    if warnings.is_empty() {
24        return;
25    }
26
27    eprintln!(
28        "WARN: {} schema warnings from the tariff:\n {}",
29        warnings.len_warnings(),
30        warning::SetWriter::new(warnings)
31    );
32}
33
34/// Print `lint::tariff::Warning`s to `stderr`.
35fn print_lint_warnings(warnings: &warning::Set<lint::tariff::Warning>) {
36    if warnings.is_empty() {
37        return;
38    }
39
40    eprintln!(
41        "WARN: {} warnings from the linting:\n {}",
42        warnings.len_warnings(),
43        warning::SetWriter::new(warnings)
44    );
45}
examples/price_cdr_with_known_version.rs (line 15)
7fn main() {
8    const CDR_JSON: &str = include_str!("data/cdr_time_and_parking_time.json");
9
10    // If you know the version of a CDR you can parse it into a `json::Document` and validate it
11    // against the schema for that version.
12    let doc = json::parse_object(CDR_JSON).expect("unable to parse CDR");
13    let (cdr, warnings) = cdr::build(doc, Version::V211).into_parts();
14
15    if !warnings.is_empty() {
16        eprintln!("The CDR has `{}` schema warnings.", warnings.len_warnings());
17    }
18
19    let report = match cdr::price(&cdr, price::TariffSource::UseCdr, Tz::Europe__Amsterdam) {
20        Ok(r) => r,
21        Err(set) => {
22            let (error, warnings) = set.into_parts();
23            print_pricing_error(&error);
24            print_pricing_warnings(&warnings);
25            return;
26        }
27    };
28
29    let (report, warnings) = report.into_parts();
30    print_pricing_warnings(&warnings);
31
32    // The various fields of the `price::Report` can be examined or converted to JSON.
33    let price::Report {
34        periods: _,
35        tariff_used: _,
36        tariff_reports: _,
37        timezone: _,
38        billed_energy: _,
39        billed_idle_time: _,
40        total_charging_time: _,
41        billed_charging_time: _,
42        total_cost: _,
43        total_fixed_cost: _,
44        total_time: _,
45        total_charging_time_cost: _,
46        total_energy: _,
47        total_energy_cost: _,
48        total_idle_time: _,
49        total_idle_cost: _,
50    } = report;
51}
52
53/// Print `price::Warning` that halted the example to `stderr`.
54fn print_pricing_error(error: &warning::Error<price::Warning>) {
55    eprintln!(
56        "ERR: Unable to price CDR due to error at path `{}`: {}",
57        error.element().path,
58        error.warning()
59    );
60}
61
62/// Print `price::Warning`s to `stderr`.
63fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
64    if warnings.is_empty() {
65        return;
66    }
67
68    eprintln!(
69        "WARN: {} warnings from the pricing:\n {}",
70        warnings.len_warnings(),
71        warning::SetWriter::new(warnings)
72    );
73}
examples/price_cdr_with_unknown_version.rs (line 33)
6fn main() {
7    const CDR_JSON: &str = include_str!("data/cdr_time_and_parking_time.json");
8
9    // First the raw JSON is parsed into a `json::Document` and the OCPI version of the CDR is
10    // guessed. The `guess::CdrVersion` returned from calling `cdr::infer_version` is either certain
11    // or uncertain about the version of the contained `cdr::Versioned` object.
12    let cdr = json::parse_object(CDR_JSON).expect("Unable to parse CDR JSON");
13    let cdr = cdr::infer_version(cdr);
14
15    // The guessed Version can be either certain or uncertain.
16    // If the version is uncertain then fallback to presuming the CDR is v211.
17    let cdr = cdr.certain_or(Version::V211);
18    let cdr = cdr::build_versioned(cdr).ignore_warnings();
19
20    // The timezone can be inferred or found in the CDR, but a versioned CDR is required.
21    let timezone = match timezone::find_or_infer(&cdr) {
22        Ok(tz) => tz,
23        Err(err_set) => {
24            let (error, warnings) = err_set.into_parts();
25            eprintln!("Unable to infer timezone");
26            print_timezone_error(&error);
27            print_timezone_warnings(&warnings);
28            return;
29        }
30    };
31    let (timezone_source, warnings) = timezone.into_parts();
32
33    if !warnings.is_empty() {
34        print_timezone_warnings(&warnings);
35    }
36
37    // We don't care whether the timezone was found or inferred.
38    let timezone = timezone_source.into_timezone();
39
40    let report = match cdr::price(&cdr, price::TariffSource::UseCdr, timezone) {
41        Ok(r) => r,
42        Err(set) => {
43            let (error, warnings) = set.into_parts();
44            print_pricing_error(&error);
45            print_pricing_warnings(&warnings);
46            return;
47        }
48    };
49
50    let (report, warnings) = report.into_parts();
51
52    print_pricing_warnings(&warnings);
53
54    // The various fields of the `price::Report` can be examined or converted to JSON.
55    let price::Report {
56        periods: _,
57        tariff_used: _,
58        tariff_reports: _,
59        timezone: _,
60        billed_energy: _,
61        billed_idle_time: _,
62        total_charging_time: _,
63        billed_charging_time: _,
64        total_cost: _,
65        total_fixed_cost: _,
66        total_time: _,
67        total_charging_time_cost: _,
68        total_energy: _,
69        total_energy_cost: _,
70        total_idle_time: _,
71        total_idle_cost: _,
72    } = report;
73}
74
75/// Print the `timezone::Warning` that halted the example to `stderr`.
76fn print_timezone_error(error: &warning::Error<timezone::Warning>) {
77    eprintln!(
78        "ERR: Unable to find timezone due to error at path `{}`: {}",
79        error.element().path,
80        error.warning()
81    );
82}
83
84/// Print the `timezone::Warning`s to `stderr`.
85fn print_timezone_warnings(warnings: &warning::Set<timezone::Warning>) {
86    if warnings.is_empty() {
87        return;
88    }
89
90    eprintln!(
91        "WARN: {} warnings from the timezone search",
92        warnings.len_warnings()
93    );
94
95    eprintln!(
96        "WARN: {} warnings from the timezone search:\n {}",
97        warnings.len_warnings(),
98        warning::SetWriter::new(warnings)
99    );
100}
101
102/// Print the `price::Warning` that halted the process to `stderr`.
103fn print_pricing_error(error: &warning::Error<price::Warning>) {
104    eprintln!(
105        "ERR: Unable to price CDR due to error at path `{}`: {}",
106        error.element().path,
107        error.warning()
108    );
109}
110
111/// Print the `price::Warning`s to `stderr`.
112fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
113    if warnings.is_empty() {
114        return;
115    }
116
117    eprintln!(
118        "WARN: {} warnings from the linting:\n {}",
119        warnings.len_warnings(),
120        warning::SetWriter::new(warnings)
121    );
122}
Source

pub fn len_elements(&self) -> usize

Return the amount of Elements in this set.

Each Element can have many Warnings associated with it.

Source

pub fn len_warnings(&self) -> usize

Return the total amount of Warnings in this set for all Elements.

Examples found in repository?
examples/generate_cdr_from_tariff.rs (line 68)
61fn print_warnings(warnings: &warning::Set<generate::Warning>) {
62    if warnings.is_empty() {
63        return;
64    }
65
66    eprintln!(
67        "WARN: {} warnings from the linting:\n {}",
68        warnings.len_warnings(),
69        warning::SetWriter::new(warnings)
70    );
71}
More examples
Hide additional examples
examples/lint_tariff.rs (line 29)
22fn print_schema_warnings(warnings: &warning::Set<schema::Warning>) {
23    if warnings.is_empty() {
24        return;
25    }
26
27    eprintln!(
28        "WARN: {} schema warnings from the tariff:\n {}",
29        warnings.len_warnings(),
30        warning::SetWriter::new(warnings)
31    );
32}
33
34/// Print `lint::tariff::Warning`s to `stderr`.
35fn print_lint_warnings(warnings: &warning::Set<lint::tariff::Warning>) {
36    if warnings.is_empty() {
37        return;
38    }
39
40    eprintln!(
41        "WARN: {} warnings from the linting:\n {}",
42        warnings.len_warnings(),
43        warning::SetWriter::new(warnings)
44    );
45}
examples/price_cdr_with_unknown_version.rs (line 92)
85fn print_timezone_warnings(warnings: &warning::Set<timezone::Warning>) {
86    if warnings.is_empty() {
87        return;
88    }
89
90    eprintln!(
91        "WARN: {} warnings from the timezone search",
92        warnings.len_warnings()
93    );
94
95    eprintln!(
96        "WARN: {} warnings from the timezone search:\n {}",
97        warnings.len_warnings(),
98        warning::SetWriter::new(warnings)
99    );
100}
101
102/// Print the `price::Warning` that halted the process to `stderr`.
103fn print_pricing_error(error: &warning::Error<price::Warning>) {
104    eprintln!(
105        "ERR: Unable to price CDR due to error at path `{}`: {}",
106        error.element().path,
107        error.warning()
108    );
109}
110
111/// Print the `price::Warning`s to `stderr`.
112fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
113    if warnings.is_empty() {
114        return;
115    }
116
117    eprintln!(
118        "WARN: {} warnings from the linting:\n {}",
119        warnings.len_warnings(),
120        warning::SetWriter::new(warnings)
121    );
122}
examples/price_cdr_with_known_version.rs (line 16)
7fn main() {
8    const CDR_JSON: &str = include_str!("data/cdr_time_and_parking_time.json");
9
10    // If you know the version of a CDR you can parse it into a `json::Document` and validate it
11    // against the schema for that version.
12    let doc = json::parse_object(CDR_JSON).expect("unable to parse CDR");
13    let (cdr, warnings) = cdr::build(doc, Version::V211).into_parts();
14
15    if !warnings.is_empty() {
16        eprintln!("The CDR has `{}` schema warnings.", warnings.len_warnings());
17    }
18
19    let report = match cdr::price(&cdr, price::TariffSource::UseCdr, Tz::Europe__Amsterdam) {
20        Ok(r) => r,
21        Err(set) => {
22            let (error, warnings) = set.into_parts();
23            print_pricing_error(&error);
24            print_pricing_warnings(&warnings);
25            return;
26        }
27    };
28
29    let (report, warnings) = report.into_parts();
30    print_pricing_warnings(&warnings);
31
32    // The various fields of the `price::Report` can be examined or converted to JSON.
33    let price::Report {
34        periods: _,
35        tariff_used: _,
36        tariff_reports: _,
37        timezone: _,
38        billed_energy: _,
39        billed_idle_time: _,
40        total_charging_time: _,
41        billed_charging_time: _,
42        total_cost: _,
43        total_fixed_cost: _,
44        total_time: _,
45        total_charging_time_cost: _,
46        total_energy: _,
47        total_energy_cost: _,
48        total_idle_time: _,
49        total_idle_cost: _,
50    } = report;
51}
52
53/// Print `price::Warning` that halted the example to `stderr`.
54fn print_pricing_error(error: &warning::Error<price::Warning>) {
55    eprintln!(
56        "ERR: Unable to price CDR due to error at path `{}`: {}",
57        error.element().path,
58        error.warning()
59    );
60}
61
62/// Print `price::Warning`s to `stderr`.
63fn print_pricing_warnings(warnings: &warning::Set<price::Warning>) {
64    if warnings.is_empty() {
65        return;
66    }
67
68    eprintln!(
69        "WARN: {} warnings from the pricing:\n {}",
70        warnings.len_warnings(),
71        warning::SetWriter::new(warnings)
72    );
73}
Source

pub fn iter(&self) -> Iter<'_, W>

Return an iterator of Warnings grouped by json::Element.

Source

pub fn id_path_map(&self, config: Limit) -> IdPathMap<'_>

Return a collection of Ids mapped to the paths they occurred at.

Source

pub fn msg_path_map(&self, config: Limit) -> MsgPathMap<'_>

Return a collection of warning messages mapped to the paths they occurred at.

Source

pub fn path_map(&self) -> BTreeMap<&str, Vec<&W>>

Return a map of json::Element paths to a list of Warning.

This is designed to be used to print out maps of warnings associated with elements. You can use the debug alternate format {:#?} to print the map ‘pretty’ over multiple lines with indentation.

Source

pub fn into_path_map(self) -> BTreeMap<Path, Vec<W>>

Consume the Set and return a map of json::Element paths to a list of Warnings.

This is designed to be used to print out maps of warnings associated with elements.

Source

pub fn path_id_map(&self) -> BTreeMap<&str, Vec<Id>>

Return a map of json::Element paths to a list of Warning ids as Strings.

This is designed to be used to print out maps of warnings associated with elements. You can use the debug alternate format {:#?} to print the map ‘pretty’ over multiple lines with indentation.

Note: This representation is also valid JSON and can be copied directly to a test expectation file.

Source

pub fn path_msg_map(&self) -> BTreeMap<&str, Vec<String>>

Return a map of json::Element paths to a list of Warning messages as Strings.

This is designed to be used to print out maps of warnings associated with elements. You can use the debug alternate format {:#?} to print the map ‘pretty’ over multiple lines with indentation.

Trait Implementations§

Source§

impl<W: Debug + Warning> Debug for Set<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<W: Warning> IntoIterator for Set<W>

Source§

type Item = Group<W>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<W>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, W: Warning> IntoIterator for &'a Set<W>

Source§

type Item = &'a Group<W>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, W>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<W> Freeze for Set<W>

§

impl<W> RefUnwindSafe for Set<W>
where W: RefUnwindSafe,

§

impl<W> Send for Set<W>

§

impl<W> Sync for Set<W>

§

impl<W> Unpin for Set<W>

§

impl<W> UnsafeUnpin for Set<W>

§

impl<W> UnwindSafe for Set<W>
where W: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoCaveat for T

Source§

fn into_caveat<W>(self, warnings: Set<W>) -> Caveat<T, W>
where W: Warning,

Any type can be converted to Caveat<T> by supplying a list of Warnings.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more