Skip to main content

Crate grabfood

Crate grabfood 

Source
Expand description

GrabFood: a typed API over grabcli_core::Context.

Every function takes a &mut Context and returns plain structs; nothing here reads config files, picks paths or prints. Carts are values the caller keeps (Cart); the grab CLI stores them as JSON files, a daemon can keep them anywhere.

Build a context with context, which registers the GrabFood URL allowlist (the order endpoint only when allow_ordering is set) and turns the response cache off; browse callers opt back in with Context::set_cache_mode. The cart → quote → order path never reads the cache.

§Example: read a merchant menu

use grabfood::{menu, MenuSource};

let menu = menu(ctx, "merchant-id", MenuSource::Live).await?;
println!("{}", menu.name);

Create ctx with context using your configuration, session store, and cache. Reading a menu makes a network request but places no order. Use real merchant IDs returned by browse calls. cart_add validates cart IDs against the menu; quote() fetches current prices before checkout.

Re-exports§

pub use cart::Cart;
pub use cart::CartItem;
pub use cart::CartModifier;
pub use menu::Menu;
pub use quote::Quote;
pub use tracking::Tracking;

Modules§

api
GrabFood endpoint URLs and the URL allowlist.
browse
Discovery: search, nearby, recommended, cuisines and category, each returning a typed Listing (printing is the CLI’s). Lists are fetched page by page through the response cache (paced, stopping cleanly on a 429), indexed into the local catalog, then filtered/sorted client-side. If the live call fails, results fall back to the last stored response or the local catalog, marked with their age.
cart
A GrabFood cart: one merchant at a time, validated against the live menu before quoting. The library passes carts by value; where one is kept is the caller’s choice (the grab CLI stores <state>/food/cart.json).
datetime
Tiny UTC→PHT (UTC+8) date math, no external date crate.
discover
Discovery lists: parsing /search, /category and /recommended/merchants responses into Merchants (with any dish hits), plus the client-side filters and sorting the upstream lacks (see docs/discovery.md).
history
grab food history sync --vault <dir>: export completed GrabFood orders to a Markdown/Obsidian vault, idempotently.
index
Mapping GrabFood responses into the core catalog (restaurants, sightings, menu categories, items) and catalog rows back into Merchants for --local results. Indexing is best-effort: callers ignore its errors.
menu
Menu parsing: categories, items, modifier groups, merchant hours/ETA and the offer carousel.
order
Order submission — gated, and disabled by default.
quote
Price quotes via POST /order/cart.
tracking
A stable, versioned view of a live order, combined from the track and detail endpoints.

Structs§

Added
What cart_add added.
BestVoucher
The cheapest voucher for a cart.
OrderSummary
One order in the order list.
Placed
A placed order.
PreparedOrder
A fresh quote and the exact order body that would be submitted for it.
VoucherList
Eligible voucher codes for a cart, with the base (no-voucher) quote.
VoucherTry
One code tried by best_voucher: its total, or None if the quote with it failed.

Enums§

MenuSource
Where a menu may come from.

Constants§

CACHE_NS
Cache namespace for GrabFood entries.
MAX_MENU_IMAGES
Most item images download_menu_images fetches per call.

Functions§

best_voucher
Re-quote cart with each eligible code (sequentially, ~1.2s apart) and pick the lowest total. To use it, set cart.voucher = best.best_code.
cart_add
Add an item to cart, validating the ids against the merchant’s menu. mods are GROUP_ID:OPTION_ID choices. A cached menu (≤ menu TTL) is fine here: the cart only stores ids, and quote() re-validates against a live menu before any price is shown.
cart_remove
Remove every line with item_id from cart. Errors if there was none.
context
A GrabFood context: Context::new plus the GrabFood allowlist, with the cache off (cart/quote/voucher/order/tracking/history always talk to Grab live). Browse callers may then Context::set_cache_mode.
download_menu_images
Download up to MAX_MENU_IMAGES item images into the image cache, one request at a time. Returns how many failed.
ensure_ordering_enabled
The ordering_disabled error (with the redacted body that would have been sent) unless allow_ordering is set.
history
Completed orders, newest first, up to limit scanned (paged gently).
menu
A merchant’s menu, modifiers, hours and offers.
menu_meta
A menu plus when it was fetched (cached_at set if it came from the cache). Live fetches through the cache are indexed into the catalog. For browsing only, an expired login falls back to the guest menu endpoint.
orders
Recent orders (only ongoing ones with ongoing).
parse_mods
Parse GROUP_ID:OPTION_ID modifier choices against a menu item.
payment_default
The payment method id used when none is given: config, then session.
place_order
Re-quote cart, and if the fresh quote id equals quote_id (the one the user approved) and ordering is enabled, submit it once and empty the cart. A mismatch returns quote_changed and nothing is sent.
prepare_order
Re-quote cart live and build the order body (nothing is sent). Show the quote to the user, then pass their confirmed quote id to submit_order.
quote
Price cart live (no order is placed). voucher overrides the cart’s stored code; payment overrides payment_default. The cart is validated against the live menu first.
quote_json
A quote as the CLI’s --json shape.
submit_order
Submit a prepared order. Refuses unless confirmed_quote_id equals the prepared (fresh) quote’s id and allow_ordering is set; both checks run before any request. Never retried: on an ambiguous failure the error says to check ongoing orders. Returns the order id.
track
One order’s live status: state, driver, ETA, route, items.
vouchers
List the voucher codes Grab says are eligible for cart.