Every B-n citation in crates/, docs/, deploy/, scripts/ and .github/ now names its bookspec id BS-<area>-<n> (D-68), via scripts/rewrite-rule-refs.py: the map is parsed from docs/spec/rules.md's index, rules.md-prefixed and bare forms are rewritten, sub-references (rule 11, item 7, INVARIANT 2) survive, and plan/, docs/spec/rules.md and docs/spec/claims/ are untouched. Program-output string literals that cited a rule were reverted by hand so user-visible output and its goldens are unchanged; DatedRule.catalog, metadata no output prints, now holds BS-1099-1. One SOURCE line that cited only 'R-?' now also cites BS-statement-5. Co-Authored-By: Muse Wonder 1.0
150 lines
6.0 KiB
Rust
150 lines
6.0 KiB
Rust
use crate::*;
|
|
|
|
impl Books<'_> {
|
|
pub(crate) fn answer_command(&self, q: &Query) -> Result<Value, Refusal> {
|
|
let ex = &self.ex;
|
|
let mut copy = self.l().clone();
|
|
let result: Result<(), LedgerError> = match q.name.as_str() {
|
|
"rename_account" => {
|
|
let code = q.code.as_deref().ok_or_else(|| self.missing("account"))?;
|
|
let name = q.new_name.as_deref().ok_or_else(|| self.missing("name"))?;
|
|
let account = self.account_or_refuse(code)?;
|
|
copy.rename_account(account, name)
|
|
}
|
|
"add_account" => {
|
|
let a = q.account.as_ref().ok_or_else(|| self.missing("account"))?;
|
|
let kind = account_type(a, &ex.id);
|
|
let parent = a
|
|
.parent
|
|
.as_deref()
|
|
.map(|p| self.account_or_refuse(p))
|
|
.transpose()?;
|
|
copy.add_account(&a.code, &a.name, kind, parent, a.header)
|
|
.and_then(|id| match &a.tax_category {
|
|
Some(c) => copy.set_account_tax(id, Some(c), None),
|
|
None => Ok(()),
|
|
})
|
|
.and_then(|()| {
|
|
self.l().check_tax_category_required(
|
|
&a.code,
|
|
&a.name,
|
|
kind,
|
|
a.header,
|
|
a.tax_category.as_deref(),
|
|
)
|
|
})
|
|
}
|
|
"set_tax_category" => {
|
|
let code = q.code.as_deref().ok_or_else(|| self.missing("account"))?;
|
|
let account = self.account_or_refuse(code)?;
|
|
let category = q.category.as_deref();
|
|
copy.set_account_tax(account, category, None)
|
|
.and_then(|()| self.l().check_tax_category_kept(account, category))
|
|
}
|
|
other => unreachable!("{other} is no command"),
|
|
};
|
|
result
|
|
.map(|()| Value::Bool(true))
|
|
.map_err(|e| Refusal::of_reason(refusal(&e)))
|
|
}
|
|
}
|
|
|
|
/// The commands an example may try: each on a copy of the books, so it
|
|
/// changes nothing that is checked after it.
|
|
pub(crate) const COMMANDS: [&str; 3] = ["rename_account", "add_account", "set_tax_category"];
|
|
|
|
/// The account `code` names, for a command.
|
|
pub(crate) fn command_account(ex: &Example, l: &Ledger, x: &Expect) -> ledger_core::AccountId {
|
|
let code = x
|
|
.code
|
|
.as_deref()
|
|
.unwrap_or_else(|| panic!("{}: {} names its account (code)", ex.id, x.query));
|
|
l.chart()
|
|
.find_by_code(code)
|
|
.unwrap_or_else(|| panic!("{}: no account {code}", ex.id))
|
|
.id
|
|
}
|
|
|
|
/// Every balance, by code: what a change that moves no amount leaves as
|
|
/// it was.
|
|
pub(crate) fn every_balance(l: &Ledger) -> Vec<(String, Decimal, Decimal)> {
|
|
trial_balance_as_of(l, ledger_core::calendar::LAST_DAY)
|
|
.lines
|
|
.into_iter()
|
|
.map(|t| (t.code, t.debit, t.credit))
|
|
.collect()
|
|
}
|
|
|
|
/// Try one command on a copy of the books, as the books' own command does
|
|
/// it (the engine's checks, then the policy's: BS-ledger-10, BS-ledger-11), and
|
|
/// check it is accepted or refused for the reason given.
|
|
pub(crate) fn check_command(ex: &Example, l: &Ledger, x: &Expect) {
|
|
let mut copy = l.clone();
|
|
let result: Result<(), LedgerError> = match x.query.as_str() {
|
|
"rename_account" => {
|
|
let account = command_account(ex, l, x);
|
|
let name = x
|
|
.name
|
|
.as_deref()
|
|
.unwrap_or_else(|| panic!("{}: rename_account gives the name", ex.id));
|
|
copy.rename_account(account, name).inspect(|()| {
|
|
// A rename changes the label, nothing else (BS-ledger-10).
|
|
assert_eq!(
|
|
copy.chart().get(account).unwrap().name,
|
|
name.trim(),
|
|
"{}: rename_account keeps the name trimmed",
|
|
ex.id
|
|
);
|
|
assert_eq!(
|
|
every_balance(©),
|
|
every_balance(l),
|
|
"{}: a rename moves no amount",
|
|
ex.id
|
|
);
|
|
})
|
|
}
|
|
"add_account" => {
|
|
let a = x
|
|
.account
|
|
.as_ref()
|
|
.unwrap_or_else(|| panic!("{}: add_account gives the account", ex.id));
|
|
let kind = account_type(a, &ex.id);
|
|
let parent = a.parent.as_deref().map(|p| {
|
|
l.chart()
|
|
.find_by_code(p)
|
|
.unwrap_or_else(|| panic!("{}: no account {p}", ex.id))
|
|
.id
|
|
});
|
|
copy.add_account(&a.code, &a.name, kind, parent, a.header)
|
|
.and_then(|id| match &a.tax_category {
|
|
Some(c) => copy.set_account_tax(id, Some(c), None),
|
|
None => Ok(()),
|
|
})
|
|
.and_then(|()| {
|
|
l.check_tax_category_required(
|
|
&a.code,
|
|
&a.name,
|
|
kind,
|
|
a.header,
|
|
a.tax_category.as_deref(),
|
|
)
|
|
})
|
|
}
|
|
"set_tax_category" => {
|
|
let account = command_account(ex, l, x);
|
|
let category = x.category.as_deref();
|
|
copy.set_account_tax(account, category, None)
|
|
.and_then(|()| l.check_tax_category_kept(account, category))
|
|
}
|
|
q => unreachable!("{q} is no command"),
|
|
};
|
|
match (result, &x.refused) {
|
|
(Ok(()), None) => {}
|
|
(Err(e), Some(want)) => {
|
|
assert_eq!(refusal(&e), *want, "{}: {} refused ({e})", ex.id, x.query)
|
|
}
|
|
(Err(e), None) => panic!("{}: {} refused ({e}), expected accepted", ex.id, x.query),
|
|
(Ok(()), Some(want)) => panic!("{}: {} accepted, expected refused: {want}", ex.id, x.query),
|
|
}
|
|
}
|