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
//! A value paired with its origin. Lets downstream consumers decide
//! whether to honor the value as author intent (explicit) or override
//! it with an inferred default (auto-detected).
//!
//! Generic over `T` so any wrapped value participates: in v4 used for
//! `children_group` and `children_style` on `ParsedDocument`; future
//! fields with the same shape (e.g. cascaded layout fields) can adopt
//! it.
use serde::{Deserialize, Serialize};
/// Origin of a resolved value — used to gate whether a downstream
/// override should fire (auto-detected values lose to inference;
/// explicit author intent wins).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub enum ResolvedOrigin {
/// Declared in the document's own frontmatter.
Frontmatter,
/// Inherited from an ancestor folder's `cascade:` block.
Cascade,
/// Auto-detected by a default-derivation rule.
Auto,
}
/// A value plus the rule that produced it.
///
/// Use [`Resolved::is_explicit`] to branch: explicit author intent
/// (frontmatter or cascade) survives downstream overrides; auto-detected
/// defaults can be replaced.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct Resolved<T> where T: Clone {
pub value: T,
pub origin: ResolvedOrigin,
}
impl<T: Clone> Resolved<T> {
pub fn frontmatter(value: T) -> Self {
Self { value, origin: ResolvedOrigin::Frontmatter }
}
pub fn cascade(value: T) -> Self {
Self { value, origin: ResolvedOrigin::Cascade }
}
pub fn auto(value: T) -> Self {
Self { value, origin: ResolvedOrigin::Auto }
}
/// True iff origin is [`ResolvedOrigin::Frontmatter`] or
/// [`ResolvedOrigin::Cascade`] — i.e. some author (current doc or
/// ancestor) explicitly set this value, as opposed to the build
/// pipeline auto-deriving it.
pub fn is_explicit(&self) -> bool {
matches!(self.origin, ResolvedOrigin::Frontmatter | ResolvedOrigin::Cascade)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_explicit_classifies_origins() {
assert!(Resolved::frontmatter("x".to_string()).is_explicit());
assert!(Resolved::cascade("x".to_string()).is_explicit());
assert!(!Resolved::auto("x".to_string()).is_explicit());
}
#[test]
fn roundtrips_through_serde() {
let r = Resolved::frontmatter("year".to_string());
let json = serde_json::to_string(&r).unwrap();
let back: Resolved<String> = serde_json::from_str(&json).unwrap();
assert_eq!(r, back);
}
}