1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Resolve an [`argenv`] contract against a real invocation — argv and
//! envp — with precedence, defaults, and linting.
//!
//! `argenv` declares what a program accepts, as data, and does not read
//! argv or envp at all. This crate is the other half: it depends on
//! `argenv` for [`Input`]/[`Record`] and adds exactly the runtime concern
//! `argenv` deliberately leaves out. A program's contract never needs to
//! know how it gets resolved; this crate never needs to know anything
//! `argenv` doesn't already say.
//!
//! Re-exports everything from `argenv`, so `use argenv_resolve::*;` is the
//! only import most consumers ever need.
//!
//! # The common case
//!
//! ```
//! use argenv_resolve::*;
//!
//! contract! {
//! LOG_LEVEL: LogLevel = Input {
//! key: "log_level",
//! ty: Type::Enum,
//! default: Some(LogLevel::Info),
//! allowed: LogLevel::TOKENS,
//! env: Some(Env::new("APP_LOG_LEVEL")),
//! arg: Some(Arg { value_name: "LEVEL", ..Arg::pair("log-level", 'l') }),
//! summary: "Log verbosity.",
//! ..Input::EMPTY
//! };
//! }
//!
//! // In main(): let resolved = Contract::parse_and_lint(OnProblems::FailOnError);
//! let args = vec!["--log-level".to_string(), "warn".to_string()];
//! let env = std::collections::BTreeMap::new();
//! let resolved = Invocation { args: &args, env: &env }.resolve(&Contract::records());
//!
//! assert_eq!(Contract::LOG_LEVEL.get_from(&resolved), Some(LogLevel::Warn));
//! assert_eq!(resolved.source("log_level"), Some(Source::Arg));
//! ```
//!
//! # Reading this crate
//!
//! * [`contract!`] — declare a contract and get `records()`, `problems()`,
//! and `parse_and_lint()` generated for you. Start here.
//! * [`parse()`] — the one-liner: read process argv+env, resolve, done.
//! * [`Invocation`], [`Resolution`], [`Source`] — resolving argv and envp
//! directly, with provenance for every value. Use this in tests, to
//! inject specific args/env rather than mutating global process state.
//! * [`lint()`] — what an invocation gets wrong.
//! * [`OnProblems`], [`handle_problems()`] — named policies for what to do
//! about it, instead of a hand-rolled loop per project.
//! * [`InputResolveExt`] — the trait giving [`Input`] its `get`/`get_from`/
//! etc. methods. Implemented for every `Input<T, C>`; you will not
//! usually name this trait directly, just `use argenv_resolve::*;`.
/// Declare a program's invocation surface as a typed contract.
///
/// Generates a `Contract` struct with one `pub const` per input, plus
/// `records()`, `problems()`, and `parse_and_lint()` — the three things every
/// consumer needs and would otherwise write by hand for each project.
///
/// # Usage
///
/// ```
/// use argenv_resolve::*;
///
/// contract! {
/// /// Verbosity level — both a flag and an env var.
/// LOG_LEVEL: LogLevel = Input {
/// key: "log_level",
/// ty: Type::Enum,
/// default: Some(LogLevel::Info),
/// allowed: LogLevel::TOKENS,
/// env: Some(Env::new("APP_LOG_LEVEL")),
/// arg: Some(Arg { value_name: "LEVEL", ..Arg::pair("log-level", 'l') }),
/// summary: "Log verbosity.",
/// ..Input::EMPTY
/// };
///
/// /// HDR output switch.
/// HDR: bool = Input {
/// key: "hdr",
/// ty: Type::Bool,
/// default: Some(false),
/// env: Some(Env::new("APP_HDR")),
/// arg: Some(Arg { negatable: true, ..Arg::long("hdr") }),
/// summary: "Expose HDR output to the application.",
/// ..Input::EMPTY
/// };
/// }
///
/// let p = Contract::problems();
/// assert!(p.is_empty(), "{p:#?}");
///
/// // In main(), one line resolves and enforces the policy:
/// // let resolved = Contract::parse_and_lint(OnProblems::FailOnError);
/// // let level = Contract::LOG_LEVEL.get_from_or_default(&resolved);
/// ```
///
/// # What is generated
///
/// - `pub struct Contract;`
/// - `impl Contract { pub const $ID: Input<$T, ..> = ...; ... }` — one typed
/// constant per input, carrying both its declaration and (via
/// [`InputResolveExt`]) its accessor.
/// - `Contract::records() -> Vec<Record>` — every input as a portable contract record.
/// - `Contract::problems() -> Vec<String>` — every declaration error across
/// the contract (per-input `check()`, plus `check_unique`). Call this in a
/// test so a bad declaration fails the build rather than a user's launch.
/// - `Contract::parse_and_lint(policy: OnProblems) -> Resolution` — reads the
/// process argv and env, resolves, lints, and applies the given policy.
/// The one call that replaces manual `parse` + `lint` + error-loop wiring.
/// Every declaration error across the whole contract.
///
/// Empty means valid. Call this once at startup (or in a test)
/// so a bad declaration fails loudly rather than silently doing
/// the wrong thing at runtime.
/// Parse the process argv and env, lint the result, and apply
/// the given policy — all in one call.
///
/// Panics on declaration errors (bugs in this program, not user
/// input) before touching argv/env, so they never reach production.
/// Applies `policy` to invocation problems (user mistakes).
/// [`Self::parse_and_lint`] with this program's own [`LintPolicy`]:
/// which findings are errors and which are only warnings. The default
/// treats a misspelled flag or variable as an error.
}
};
}
pub use *;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// Parse the current process's argument vector and environment against a
/// declared model, returning a fully resolved [`Resolution`].
///
/// This is the one-liner for the common case — no manual argv slicing,
/// no `Invocation` construction, no `ProcessEnv` import needed:
///
/// ```no_run
/// # use argenv_resolve::*;
/// # pub const HOST: Input<String> = Input {
/// # key: "host", ty: Type::String,
/// # env: Some(Env::new("APP_HOST")),
/// # arg: Some(Arg { value_name: "HOST", ..Arg::long("host") }),
/// # ..Input::EMPTY
/// # };
/// let model = vec![HOST.to_record()];
/// let resolution = argenv_resolve::parse(&model);
/// let host = HOST.get_from(&resolution);
/// ```
///
/// For tests, inject specific args and env via [`Invocation`] directly
/// rather than mutating global process state.