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: boolImplementations§
Source§impl Report
impl Report
Sourcepub fn read_groups(&mut self, path: impl AsRef<Path>) -> Result<()>
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 | fooWith 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_REGEXis an invalid regular expression
Sourcepub fn product_group(&self, line_item: &str) -> Option<String>
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);Sourcepub fn add_group(&mut self, name: &str, regex_str: &str) -> Result<()>
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.
Sourcepub fn products_by_unit_sales(&self) -> Vec<&str>
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.
Sourcepub fn products_by_revenue(&self) -> Vec<&str>
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.