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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! The venture descriptor: who this backend belongs to (issue #2).
use crate::config::ConfigError;
/// Deployment environment. Mirrors the `ENV` config key; `Production`
/// drives the mandatory-captcha rule (architecture section 11).
///
/// Ordered least to most protected, so [`VentureEnv::strictest`] can take
/// the safer of two answers (issue #143).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum VentureEnv {
#[default]
Development,
Staging,
Production,
}
impl VentureEnv {
pub fn as_str(&self) -> &'static str {
match self {
VentureEnv::Development => "development",
VentureEnv::Staging => "staging",
VentureEnv::Production => "production",
}
}
/// The safer of two answers about one deployment (issue #143).
///
/// A venture carries a **compiled** environment — a builder default
/// the operator cannot change without a rebuild — while the
/// deployment carries an `ENV` binding it can. They disagreed
/// silently, and the disagreement always resolved toward the weaker
/// answer: `ventures/cratefield-waitlist` ships `ENV = "production"`
/// and never calls [`Venture::env`], so every production-only rule
/// read `Development` and did not apply to the one venture actually
/// in production. Neither source may downgrade the other: if either
/// says `Production`, the deployment is production.
#[must_use]
pub fn strictest(self, other: Self) -> Self {
self.max(other)
}
/// Parses the `ENV` config value; anything unknown is `None`.
pub fn parse(value: &str) -> Option<Self> {
match value.trim().to_ascii_lowercase().as_str() {
"development" => Some(VentureEnv::Development),
"staging" => Some(VentureEnv::Staging),
"production" => Some(VentureEnv::Production),
_ => None,
}
}
}
/// Venture branding for mail templates (issue #12). Defaults are
/// text-only: factory-zero orange accent, no logo, no footer line.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Brand {
/// Accent colour as a `#rrggbb` string (links and rule lines).
pub accent: String,
/// Optional logo image URL; mails must read fine with images off.
pub logo_url: Option<String>,
/// Optional footer line (e.g. a company address).
pub footer: Option<String>,
}
impl Default for Brand {
fn default() -> Self {
Self {
accent: "#FF5A36".to_owned(),
logo_url: None,
footer: None,
}
}
}
/// Identity and CORS configuration for the venture this harness serves.
///
/// ```
/// use cratefield_core::Venture;
///
/// let v = Venture::new("factory0", "factory0.ventures")
/// .public_url("https://factory0.ventures")
/// .cors_origins(["https://factory0.ventures"]);
/// assert_eq!(v.name, "factory0");
/// assert_eq!(v.problem_type_base(), "https://factory0.ventures/problems/");
/// ```
#[derive(Debug, Clone)]
pub struct Venture {
/// Kebab-case venture name (`factory0`).
pub name: String,
/// Apex domain (`factory0.ventures`); the API serves `api.<domain>`.
pub domain: String,
/// Absolute public URL the API is linked from.
pub public_url: String,
/// CORS allowlist. Never `*` (architecture section 6).
pub cors_origins: Vec<String>,
/// Deployment environment.
pub env: VentureEnv,
/// Mail branding (issue #12).
pub brand: Brand,
/// Explicit [`Venture::problem_type_base`] override, for a venture that
/// serves its problems from somewhere other than `<public_url>/problems/`.
pub problem_base: Option<String>,
}
impl Venture {
pub fn new(name: impl Into<String>, domain: impl Into<String>) -> Self {
let domain = domain.into();
Self {
name: name.into(),
public_url: format!("https://{domain}"),
domain,
cors_origins: Vec::new(),
env: VentureEnv::default(),
brand: Brand::default(),
problem_base: None,
}
}
#[must_use]
pub fn public_url(mut self, url: impl Into<String>) -> Self {
self.public_url = url.into();
self
}
/// Overrides the base every problem `type` of this venture is served
/// under (RFC 9457): the full base URI, ending in `/`. Unset, the base
/// derives from [`Venture::public_url`] — see
/// [`Venture::problem_type_base`].
#[must_use]
pub fn problem_base(mut self, url: impl Into<String>) -> Self {
self.problem_base = Some(url.into());
self
}
/// The base URI every problem `type` of this venture is served under:
/// the [`Venture::problem_base`] override when set, else
/// `<public_url>/problems/`, else `about:blank`.
///
/// A venture with no public URL has no URI of its own to name, and it
/// must never name another venture's domain: RFC 9457 §4.2.1 reserves
/// `about:blank` for exactly this, so that is the fallback. Callers
/// branch on the slug — the part after the base; under the default base
/// everything after `/problems/`, `auth/…` namespaces included — which
/// is the stable part
/// (docs/ERRORS.md).
///
/// ```
/// use cratefield_core::Venture;
///
/// // The default: under the venture's own public URL.
/// let v = Venture::new("acme", "acme.example").public_url("https://acme.example/");
/// assert_eq!(v.problem_type_base(), "https://acme.example/problems/");
///
/// // An explicit override wins, normalized to one trailing slash.
/// let v = v.problem_base("https://errors.acme.example/types/");
/// assert_eq!(v.problem_type_base(), "https://errors.acme.example/types/");
/// let v = v.problem_base("https://errors.acme.example/types");
/// assert_eq!(v.problem_type_base(), "https://errors.acme.example/types/");
///
/// // No public URL: the RFC 9457 default, never someone else's domain.
/// let v = Venture::new("acme", "acme.example").public_url(" ");
/// assert_eq!(v.problem_type_base(), "about:blank");
/// ```
#[must_use]
pub fn problem_type_base(&self) -> String {
if let Some(base) = self
.problem_base
.as_deref()
.map(str::trim)
.filter(|base| !base.is_empty())
{
if base == crate::problem::ABOUT_BLANK {
return base.to_owned();
}
// One trailing slash, so the slug is appended, not glued on.
return format!("{}/", base.trim_end_matches('/'));
}
let public = self.public_url.trim();
if public.is_empty() {
return crate::problem::ABOUT_BLANK.to_owned();
}
format!("{}/problems/", public.trim_end_matches('/'))
}
#[must_use]
pub fn cors_origins(mut self, origins: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.cors_origins = origins.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn env(mut self, env: VentureEnv) -> Self {
self.env = env;
self
}
/// Mail branding for templates (accent, logo, footer).
#[must_use]
pub fn brand(mut self, brand: Brand) -> Self {
self.brand = brand;
self
}
/// Appends every rule violation to `errors` (issue #2: collect all
/// problems, report them together).
pub(crate) fn validate(&self, errors: &mut ConfigError) {
if self.name.is_empty() {
errors.push("venture: name must not be empty");
} else if !is_kebab_case(&self.name) {
errors.push(format!(
"venture: name `{}` must be kebab-case ([a-z0-9]+ separated by '-')",
self.name
));
}
if self.domain.trim().is_empty() {
errors.push("venture: domain must not be empty");
}
if self.cors_origins.is_empty() {
errors.push(format!(
"venture `{}`: at least one CORS origin is required",
self.name
));
} else {
for origin in &self.cors_origins {
if origin == "*" {
errors.push(format!(
"venture `{}`: wildcard CORS origin `*` is not allowed",
self.name
));
} else if !is_valid_origin(origin) {
errors.push(format!(
"venture `{}`: CORS origin `{origin}` must be scheme://host[:port]",
self.name
));
}
}
}
}
}
fn is_kebab_case(name: &str) -> bool {
!name.is_empty()
&& name.split('-').all(|part| {
!part.is_empty()
&& part
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
})
}
fn is_valid_origin(origin: &str) -> bool {
// scheme://host[:port] with no path, query or fragment.
let Some((scheme, rest)) = origin.split_once("://") else {
return false;
};
if scheme.is_empty() || !scheme.chars().all(|c| c.is_ascii_alphanumeric()) {
return false;
}
if rest.contains(['/', '?', '#']) {
return false;
}
let hostport = rest;
let host = hostport.split_once(':').map_or(hostport, |(h, _)| h);
!host.is_empty()
&& host
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == ':')
}
#[cfg(test)]
mod tests {
use super::*;
fn errors(v: &Venture) -> Vec<String> {
let mut errs = ConfigError::default();
v.validate(&mut errs);
errs.problems
}
#[test]
fn valid_venture_has_no_errors() {
let v = Venture::new("factory0", "factory0.ventures")
.cors_origins(["https://factory0.ventures"]);
assert!(errors(&v).is_empty());
}
#[test]
fn name_must_be_kebab_case() {
let v = Venture::new("Factory0", "factory0.ventures")
.cors_origins(["https://factory0.ventures"]);
assert!(errors(&v).iter().any(|e| e.contains("kebab-case")));
}
#[test]
fn domain_must_be_non_empty() {
let v = Venture::new("factory0", " ").cors_origins(["https://x.dev"]);
assert!(errors(&v).iter().any(|e| e.contains("domain")));
}
#[test]
fn at_least_one_cors_origin() {
let v = Venture::new("factory0", "factory0.ventures");
assert!(errors(&v).iter().any(|e| e.contains("CORS origin")));
}
#[test]
fn wildcard_origin_rejected() {
let v = Venture::new("factory0", "factory0.ventures").cors_origins(["*"]);
assert!(errors(&v).iter().any(|e| e.contains("wildcard")));
}
}