Crates.io | shopping_list_parser |
lib.rs | shopping_list_parser |
version | 0.1.4 |
source | src |
created_at | 2024-11-06 21:34:23.550165 |
updated_at | 2024-11-16 11:50:18.783353 |
description | Shopping-list parser for edu purposes |
homepage | |
repository | |
max_upload_size | |
id | 1439057 |
size | 34,099 |
This project is a shopping_list_parser designed for educational purposes. It allows you to parse a structured list of shopping items using a grammar defined in pest.
Shopping List Grammar The following grammar defines the structure of each item in the shopping list, with rules for attributes like item names, quantities, units, and optional descriptions. This structure is used to parse and validate items, ensuring consistent formatting.
Grammar Rules
index: Numeric identifier for each item
.
index = { ASCII_DIGIT+ }
Example: 1, 25
quantity: The amount or count of the item.
quantity = { ASCII_DIGIT+ }
Example: 2, 5
name: Name of the item, allowing letters, spaces, and hyphens.
name = { (ASCII_ALPHA | " " | "-")+ }
Example: Apples, Brown Rice
brand: Optional brand information, placed in parentheses.
brand = { "(" ~ (ASCII_ALPHA | " ")+ ~ ")" }
Example: (Green Organic), (Local Brand)
description: Optional additional details, placed in curly braces.
description = { "{" ~ (ASCII_ALPHA | ASCII_DIGIT | " ")+ ~ "}" }
Example: {Sweet and crunchy}, {High in fiber}
unit: Measurement unit for the quantity.
unit = { "kg" | "g" | "ltr" | "ml" | "pcs" | "oz" }
Example: kg, pcs, oz
category: A label for organizing items, placed in square brackets.
category = { "[" ~ ASCII_ALPHA+ ~ ASCII_DIGIT* ~ "]" }
Example: [Fruits], [Snacks1]
item: Full definition of an item entry, including mandatory and optional elements.
item = { index ~ "." ~ WHITE_SPACE? ~ name ~ WHITE_SPACE? ~ quantity ~ WHITE_SPACE? ~ unit ~ (WHITE_SPACE? ~ brand)? ~ (WHITE_SPACE? ~ description)? }
Example: 1. Apples 2 kg (Green Organic) {Sweet and crunchy}
shopping_list: A collection of items and categories, with support for whitespace and line breaks.
shopping_list = { SOI ~ ((WHITE_SPACE* ~ (category | item) ~ WHITE_SPACE* ~ NEWLINE?)* ~ EOI) }
[SMTH]
1. Apples 2 kg (Green Organic) {Sweet and crunchy}
2. Milk 1 ltr (Dairy Best)
3. Bread 1 pcs {Whole grain, freshly baked}
[Fruits]
4. Oranges 3 kg
5. Bananas 1 kg
pest
parser generator library to define the grammarImport the shopping_list_parser
crate in your Rust project:
use shopping_list_parser::parse_shopping_list;
Call the parse_shopping_list
function with a string containing the shopping list:
let input = "1. Apples 5 pcs\n2. Bananas 3 kg\n3. Mango 1 pcs";
parse_shopping_list(&input)?;