shopping_list_parser

Crates.ioshopping_list_parser
lib.rsshopping_list_parser
version0.1.4
sourcesrc
created_at2024-11-06 21:34:23.550165
updated_at2024-11-16 11:50:18.783353
descriptionShopping-list parser for edu purposes
homepage
repository
max_upload_size
id1439057
size34,099
(Invop)

documentation

README

Shopping List Parser

https://crates.io/crates/shopping_list_parser

https://docs.rs/shopping_list_parser/latest/shopping_list_parser/

Description

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.

Grammar rules

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

## Features
  • Parses a structured shopping list with items, quantities, and units
  • Uses the pest parser generator library to define the grammar
  • Provides a simple API to parse the shopping list

Getting Started

Prerequisites

  • Rust programming language
  • Cargo package manager

Usage

  1. Import the shopping_list_parser crate in your Rust project:

    use shopping_list_parser::parse_shopping_list;
    
  2. 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)?;
    
Commit count: 0

cargo fmt