btctax_cli/lib.rs
1//! btctax-cli: the CLI + reconciliation library that wires the encrypted vault (btctax-store),
2//! ingest (btctax-adapters), and the pure projection (btctax-core) into the Phase-1 command surface
3//! (spec §11). The library is I/O-explicit and deterministic; the binary (`main.rs`) is a thin clap
4//! dispatch. PRIVACY: tests use only temp vaults + synthetic fixtures; no real user file is ever read.
5pub mod bulk_estimated;
6pub mod cli;
7pub mod cmd;
8pub mod config;
9pub mod donation_details;
10pub mod eventref;
11pub mod optimize_attest;
12pub mod render;
13pub mod session;
14pub mod tax_profile;
15
16pub use cli::Cli;
17pub use config::CliConfig;
18pub use session::{
19 BulkFilter, BulkIncomeFilter, BulkIncomePlan, BulkIncomeRow, BulkLinkPlan, BulkLinkRow,
20 BulkReclassifyOutflowPlan, BulkReclassifyOutflowRow, BulkResolvePlan, BulkResolveRow,
21 BulkStiFilter, BulkStiPlan, BulkStiRow, BulkVoidPlan, BulkVoidRow, Frame, MatchAction,
22 MatchProposal, Session,
23};
24
25#[derive(Debug, thiserror::Error)]
26pub enum CliError {
27 #[error(transparent)]
28 Store(#[from] btctax_store::StoreError),
29 #[error(transparent)]
30 Core(#[from] btctax_core::CoreError),
31 #[error(transparent)]
32 Adapter(#[from] btctax_adapters::AdapterError),
33 #[error("sqlite: {0}")]
34 Sqlite(#[from] rusqlite::Error),
35 /// C1: `write_csv_exports` (Task 15) uses `?` on `csv::Writer` ops (→ `csv::Error`); `csv::Error`
36 /// is its own type (NOT covered by `Io(#[from] io::Error)`, whose `From` goes the other way), so it
37 /// needs its own variant or Task 15 will not compile.
38 #[error("csv: {0}")]
39 Csv(#[from] csv::Error),
40 #[error("io: {0}")]
41 Io(#[from] std::io::Error),
42 /// A user-supplied event reference did not parse as a canonical `EventId` (eventref.rs).
43 #[error("not a valid event reference: {0:?}")]
44 BadEventRef(String),
45 /// A CLI argument was malformed (bad USD/date/enum/wallet spec, or a contradictory flag set).
46 #[error("usage: {0}")]
47 Usage(String),
48 /// M1: a `cli_config` row held an unrecognized value (corrupt DB, future-written value, or manual
49 /// edit gone wrong). Returning an error is safer than silently misreading the stored intent.
50 #[error("unrecognized stored config value: key={key:?} value={value:?}")]
51 BadConfigValue { key: String, value: String },
52 /// Sub-project 3 attestation gate: an export was attempted while the ledger is pseudo-reconciled
53 /// (a synthetic, non-persisted default contributes to the projection) and NO attestation phrase was
54 /// supplied. Producing a form/data file from a fictional draft requires typing the exact phrase.
55 /// (Supersedes sub-2's interim [I3] blanket refusal.)
56 #[error(
57 "export refused: the ledger is pseudo-reconciled (a synthetic default contributes to the \
58 projection). To export this draft ON PURPOSE, attest the exact phrase {:?} (pass --attest, or \
59 type it at the prompt). Otherwise run `reconcile pseudo off` (or approve + attest the defaults).",
60 ATTEST_PHRASE
61 )]
62 AttestationRequired,
63 /// Sub-project 3 attestation gate: an export was attempted while the ledger is pseudo-reconciled and
64 /// the supplied attestation phrase did NOT match (trimmed, case-sensitive, exact). A wrong phrase is
65 /// FAILED regardless of environment [R0-I1] — no fictional form leaves the machine.
66 #[error(
67 "export refused: the attestation phrase did not match. The ledger is pseudo-reconciled; type the \
68 phrase EXACTLY (trimmed, case-sensitive): {:?}.",
69 ATTEST_PHRASE
70 )]
71 AttestationFailed,
72}
73
74/// The exact phrase a user must affirm to export a form/data file while the ledger is pseudo-reconciled
75/// (sub-project 3). Compared TRIMMED, case-SENSITIVE, exact. The prompt + both error strings are BUILT
76/// from this constant [R0-M1] so there is no drift (a KAT asserts they contain it). `pub` so btctax-tui
77/// shares it [R0-r2-N2].
78pub const ATTEST_PHRASE: &str = "I attest this is true";
79
80/// PURE exact-compare attestation gate — NO I/O, NO TTY read [R0-I2]. The interactive prompt lives in
81/// the caller (the `export-snapshot` main.rs arm / the btctax-tui export modal); this helper only
82/// compares, keeping the library I/O-explicit and the KATs deterministic (no env-dependent branch).
83///
84/// - `attest.map(str::trim) == Some(ATTEST_PHRASE)` → `Ok(())`.
85/// - `Some(_)` non-matching → `Err(AttestationFailed)` (a wrong phrase FAILS regardless of env) [R0-I1].
86/// - `None` → `Err(AttestationRequired)`.
87///
88/// `pub` so btctax-tui shares the exact-compare [R0-r2-N2].
89pub fn require_attestation(attest: Option<&str>) -> Result<(), CliError> {
90 match attest.map(str::trim) {
91 Some(p) if p == ATTEST_PHRASE => Ok(()),
92 Some(_) => Err(CliError::AttestationFailed),
93 None => Err(CliError::AttestationRequired),
94 }
95}