Skip to main content

Report

Struct Report 

Source
pub struct Report {
    pub sort_by_revenue: bool,
    /* private fields */
}
Expand description

Holds sales data.

To create a new, empty Report, use Report::new.

To add group configuration, use Report::add_group or Report::read_groups.

To add sales data, use Report::read_csv.

To get a printable version of the report, use its Display implementation.

Fields§

§sort_by_revenue: bool

Implementations§

Source§

impl Report

Source

pub fn new() -> Report

Creates a new, empty report with no data or group configuration.

Source

pub fn read_groups(&mut self, path: impl AsRef<Path>) -> Result<()>

Reads product group configuration from path.

The configuration file consists of group specifications, one per line, in the following format:

GROUP_NAME | GROUP_REGEX
§Examples

A very simple example:

Foo | foo

With this group defined, when analysing the sales data, all products whose name contains foo will be counted as a single product named Foo.

GROUP_REGEX can be any regular expression supported by regex::Regex.

§Errors

Returns errors if:

  • The file cannot be opened
  • The file cannot be read
  • There is a line with an invalid format (no | character)
  • GROUP_REGEX is an invalid regular expression
Source

pub fn product_group(&self, line_item: &str) -> Option<String>

Returns the product group for line_item, if any.

If the product name line_item matches the regular expression for any defined product group, then this function returns the name of that group.

§Examples
let mut report = Report::new();
report.add_group("Foo", "foo").unwrap();
assert_eq!(report.product_group("foo variant 1"), Some("Foo".into()));
assert_eq!(report.product_group("ungrouped product"), None);
Source

pub fn add_group(&mut self, name: &str, regex_str: &str) -> Result<()>

Adds a new group configuration.

Products whose name matches regex_str will be reported as part of product group name, rather than their own line item names.

§Errors

Returns any errors from compiling regex_str with Regex::new.

Source

pub fn read_csv(&mut self, path: impl AsRef<Path>) -> Result<()>

Reads sales data from the CSV files at paths, and updates the report.

§Errors

Returns any errors from opening or parsing a CSV file.

§Panics

If units or revenue values or totals overflow usize.

Source

pub fn products_by_unit_sales(&self) -> Vec<&str>

Returns product names sorted by unit sales, descending.

The name of the best-selling product (by units, as opposed to revenue) is given first, and then the remaining names in descending order of unit sales. Products with identical sales are sorted alphabetically.

§Panics

If a product is removed from the map during sorting.

Source

pub fn products_by_revenue(&self) -> Vec<&str>

Returns product names sorted by revenue, descending.

The name of the best-selling product (by revenue, as opposed to units) is given first, and then the remaining names in descending order of revenue. Products with identical sales are sorted alphabetically.

§Panics

If a product is removed from the map during sorting.

Trait Implementations§

Source§

impl Debug for Report

Source§

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

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

impl Default for Report

Source§

fn default() -> Report

Returns the “default value” for a type. Read more
Source§

impl Display for Report

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.