d00cea2477
improvise-e0ex. `Margin = Profit / Revenue` drew as `1`: `View::new` seeded
`,.0` — zero decimals — so a 0.66 ratio rounded half-away-from-zero. The cause
is the format, not the arithmetic; exact decimal math would not change the
rendering by one character, so nothing here touches the evaluator and gotcha 8
is unchanged.
Two halves, per the user's ruling:
* `View::new` seeds `,.2`. Revenue draws `18,400.00` and Margin `0.66`. The
noise on whole numbers is the cheaper error — a currency column with two
redundant zeroes is legible, a ratio with none is wrong — and a format cannot
be guessed from a value.
* A formula may carry its own format, which wins for its target; a raw measure
keeps the view's. `formula.pest` gains
`format_clause = ${ ^"FORMAT" ~ sp+ ~ format_spec }`, with `sp+` for `WHERE`'s
reason (a predicate is invisible to the grammar-walking generator) and
`FORMAT` added to `quote::KEYWORDS`.
`improv.pest` is deliberately unchanged: `formula_raw` already carries the
`## Formulas` line verbatim, so the format rides in the formula text with
nothing to add — and the generator, which reads `improv.pest`, sees no new
spelling to violate.
Resolution has one home. `FormatPlan::of(model, view)` replaces the
`(comma, decimals)` pair every render path threaded — `GridLayout::display_text`,
`viewport::grid_body`/`compute_col_widths`, `project::grid_in` and
`persistence/export.rs::render_csv` — rather than being added beside it, which
would be two answers to "what format is this cell". It is asked of the cell's
`CellKey`, so a cell aggregating over `_Measure` keeps the view's format by
construction. `GridBody::col_formats` carries the answer to the totals strip,
which is formatted on its own path.
Persistence is untouched except for metadata: `format_f64` is display,
`persistence/format.rs::format_number` writes round-trip precision, and the two
have no call between them. Newly created views write `format: ,.2`; existing
documents are unaffected, because `format_md` has always written the line
explicitly. `web/demo.improv` moves to `,.2` on both views.
The trap this sets is gotcha 34's: `format_md` writes `Formula::raw`, so a
`FORMAT` suffix round-trips whether or not the walker parsed it. The round-trip
test therefore asserts the parsed field — injecting a walker that drops the
clause fails that one test and nothing else in `improvise-io`, 242 tests
including the whole characterization suite and the grammar properties.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FC2jVn8Au7E9JPkw6bwXXH
91 lines
2.3 KiB
Rust
91 lines
2.3 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub enum AggFunc {
|
|
Sum,
|
|
Avg,
|
|
Min,
|
|
Max,
|
|
Count,
|
|
}
|
|
|
|
/// Arithmetic and comparison operators. An enum rather than a `String`, so an
|
|
/// invalid operator is caught at parse time — design-principles §2.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum BinOp {
|
|
Add,
|
|
Sub,
|
|
Mul,
|
|
Div,
|
|
Pow,
|
|
Eq,
|
|
Ne,
|
|
Lt,
|
|
Gt,
|
|
Le,
|
|
Ge,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Filter {
|
|
pub category: String,
|
|
pub item: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub enum Expr {
|
|
Number(f64),
|
|
Ref(String),
|
|
BinOp(BinOp, Box<Expr>, Box<Expr>),
|
|
UnaryMinus(Box<Expr>),
|
|
Agg(AggFunc, Box<Expr>, Option<Filter>),
|
|
If(Box<Expr>, Box<Expr>, Box<Expr>),
|
|
/// `Cat IS Item` — 1.0 when the cell's own `Cat` coordinate is `Item`, else
|
|
/// 0.0. A coordinate predicate as a number; `context/code/core.md` §13.
|
|
Is(Filter),
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Formula {
|
|
/// The raw formula text, e.g. "Profit = Revenue - Cost"
|
|
pub raw: String,
|
|
/// The item/dimension name this formula computes, e.g. "Profit"
|
|
pub target: String,
|
|
/// The category containing the target item
|
|
pub target_category: String,
|
|
/// The expression to evaluate
|
|
pub expr: Expr,
|
|
/// Optional WHERE filter
|
|
pub filter: Option<Filter>,
|
|
/// How this formula's target is drawn, overriding the view's `format:` for
|
|
/// that one measure. `None` means the view answers — `context/code/core.md`.
|
|
#[serde(default)]
|
|
pub format: Option<String>,
|
|
}
|
|
|
|
impl Formula {
|
|
pub fn new(
|
|
raw: impl Into<String>,
|
|
target: impl Into<String>,
|
|
target_category: impl Into<String>,
|
|
expr: Expr,
|
|
filter: Option<Filter>,
|
|
) -> Self {
|
|
Self {
|
|
raw: raw.into(),
|
|
target: target.into(),
|
|
target_category: target_category.into(),
|
|
expr,
|
|
filter,
|
|
format: None,
|
|
}
|
|
}
|
|
|
|
/// The same formula drawn with `format`. Separate from [`Formula::new`] so
|
|
/// the five-argument constructor every caller already spells stays put.
|
|
pub fn with_format(mut self, format: Option<String>) -> Self {
|
|
self.format = format;
|
|
self
|
|
}
|
|
}
|