1use std::marker::PhantomData;
5
6use crate::config::Config;
7use crate::names::same_name;
8use crate::node::{Field, Node, Shape, Unbuilt, section_entries};
9use crate::path::Path;
10use crate::problems::Problems;
11use crate::raw::{Raw, RawKey};
12use crate::source::{self, Loaded, Source};
13use crate::validate::Report;
14
15pub trait ExtraSection: Config {
17 type Host: Config;
19}
20
21pub trait Extras: Sized + Default {
24 fn each<'a>(&'a self, visit: &mut dyn FnMut(&'static str, &'static str, &'a dyn Node));
26
27 fn each_mut<'a>(
29 &'a mut self,
30 visit: &mut dyn FnMut(&'static str, &'static str, &'a mut (dyn Node + 'a)),
31 );
32}
33
34impl Extras for () {
35 fn each<'a>(&'a self, _visit: &mut dyn FnMut(&'static str, &'static str, &'a dyn Node)) {}
36
37 fn each_mut<'a>(
38 &'a mut self,
39 _visit: &mut dyn FnMut(&'static str, &'static str, &'a mut (dyn Node + 'a)),
40 ) {
41 }
42}
43
44impl<X: ExtraSection, E: Extras> Extras for (Option<X>, E) {
45 fn each<'a>(&'a self, visit: &mut dyn FnMut(&'static str, &'static str, &'a dyn Node)) {
46 visit(X::KEY, X::DOC, &self.0);
47 self.1.each(visit);
48 }
49
50 fn each_mut<'a>(
51 &'a mut self,
52 visit: &mut dyn FnMut(&'static str, &'static str, &'a mut (dyn Node + 'a)),
53 ) {
54 visit(X::KEY, X::DOC, &mut self.0);
55 self.1.each_mut(visit);
56 }
57}
58
59#[derive(Debug, Clone)]
65pub struct Extensible<H, E> {
66 pub host: H,
68 pub extras: E,
70}
71
72impl<H: Config, E: Extras> Default for Extensible<H, E> {
73 fn default() -> Self {
74 Self {
75 host: H::default(),
76 extras: E::default(),
77 }
78 }
79}
80
81impl<H: Config, E: Extras> Config for Extensible<H, E> {
82 const NAME: &'static str = H::NAME;
83 const KEY: &'static str = H::KEY;
84 const DOC: &'static str = H::DOC;
85}
86
87impl<H: Config, E: Extras> Node for Extensible<H, E> {
88 fn read(raw: &Raw, path: &Path, problems: &mut Problems) -> Result<Self, Unbuilt> {
89 let mut value = Self::default();
90 Self::overlay(&mut value, raw, path, problems)?;
91 Ok(value)
92 }
93
94 fn overlay(&mut self, raw: &Raw, path: &Path, problems: &mut Problems) -> Result<(), Unbuilt> {
95 let Some(entries) = section_entries(raw, path, problems) else {
96 return Err(Unbuilt);
97 };
98 let mut own: Vec<(RawKey, Raw)> = Vec::new();
100 for (key, item) in entries {
101 let mut claimed = false;
102 self.extras.each_mut(&mut |name, _, extra| {
103 if !claimed && same_name(name, key.text()) {
104 claimed = true;
105 extra.overlay(item, &path.field(name), problems).ok();
106 }
107 });
108 if !claimed {
109 own.push((key.clone(), item.clone()));
110 }
111 }
112 if !own.is_empty() {
113 let rest = Raw::mapping(own, raw.mark().clone());
114 H::overlay(&mut self.host, &rest, path, problems)?;
115 }
116 Ok(())
117 }
118
119 fn empty() -> Option<Self> {
120 Some(Self::default())
121 }
122
123 fn fill(&mut self) {
124 self.host.fill();
125 self.extras.each_mut(&mut |_, _, extra| extra.fill());
126 }
127
128 fn check(&self, path: &Path, problems: &mut Problems) {
129 self.host.check(path, problems);
132 self.extras
133 .each(&mut |name, _, extra| extra.check(&path.field(name), problems));
134 }
135
136 fn shape(&self) -> Shape<'_> {
137 let fields = match self.host.shape() {
138 Shape::Section(fields) => fields,
139 _ => Vec::new(),
140 };
141 let mut fields = fields;
142 self.extras.each(&mut |name, doc, node| {
143 fields.push(Field {
145 name,
146 doc,
147 obsolete: None,
148 required: false,
149 node,
150 })
151 });
152 Shape::Section(fields)
153 }
154}
155
156pub trait ExtensibleConfig: Config + Sized {
158 fn open(source: &Source) -> Opening<'_, Self, ()> {
160 Opening {
161 source,
162 list: PhantomData,
163 }
164 }
165}
166
167impl<T: Config> ExtensibleConfig for T {}
168
169pub struct Opening<'a, H: Config, E = ()> {
171 source: &'a Source,
172 list: PhantomData<fn() -> (H, E)>,
173}
174
175impl<'a, H: Config, E: Extras> Opening<'a, H, E> {
176 pub fn with<X: ExtraSection<Host = H>>(self) -> Opening<'a, H, (Option<X>, E)> {
178 Opening {
179 source: self.source,
180 list: PhantomData,
181 }
182 }
183
184 pub fn checking(
186 self,
187 checker: impl Fn(&Extensible<H, E>, &mut Report<'_>) + 'a,
188 ) -> Checked<'a, H, E> {
189 Checked {
190 opening: self,
191 checker: Box::new(checker),
192 }
193 }
194
195 pub fn load(self) -> Result<Loaded<Extensible<H, E>>, Problems> {
197 source::read(self.source, |_, _| {})
198 }
199}
200
201type Checker<'a, H, E> = Box<dyn Fn(&Extensible<H, E>, &mut Report<'_>) + 'a>;
203
204pub struct Checked<'a, H: Config, E = ()> {
206 opening: Opening<'a, H, E>,
207 checker: Checker<'a, H, E>,
208}
209
210impl<H: Config, E: Extras> Checked<'_, H, E> {
211 pub fn load(self) -> Result<Loaded<Extensible<H, E>>, Problems> {
213 let checker = self.checker;
214 source::read(self.opening.source, move |value, problems| {
215 checker(value, &mut Report::new(&Path::new(), problems));
216 })
217 }
218}