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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! Config template rendering and split-template target planning.
//!
//! The public entry points collect the template tree, preserve existing include
//! structure when regenerating templates, append schema-declared default child
//! includes for newly split sections, and optionally bind generated templates
//! to JSON Schemas.
use ;
use JsonSchema;
use crate::;
use ;
use ;
use template_for_target;
use section_path_for_target;
pub use template_for_path;
pub use ConfigTemplateTarget;
/// Collects all template targets that should be generated for a config tree.
///
/// The root template source is selected with [`select_template_source`]. Include
/// paths found in the source tree are mirrored under `output_path` for relative
/// includes. Nested `confique` sections marked with `x-tree-split = true` are
/// used to derive child template files with paths from
/// [`ConfigSchema::template_path_for_section`].
///
/// # Type Parameters
///
/// - `S`: Config schema type used to discover includes and render templates.
///
/// # Arguments
///
/// - `config_path`: Root config path preferred as the template source when it
/// exists.
/// - `output_path`: Root output path for generated templates.
///
/// # Returns
///
/// Returns all generated template targets in traversal order.
///
/// # Examples
///
/// ```
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, template_targets_for_paths};
/// use schemars::JsonSchema;
///
/// #[derive(Config, JsonSchema)]
/// struct AppConfig {
/// #[config(default = [])]
/// include: Vec<std::path::PathBuf>,
/// #[config(default = "demo")]
/// mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
/// fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
/// layer.include.clone().unwrap_or_default()
/// }
/// }
///
/// let targets =
/// template_targets_for_paths::<AppConfig>("config.yaml", "config.example.yaml")?;
///
/// assert_eq!(targets.len(), 1);
/// assert!(targets[0].content.contains("mode"));
/// # Ok::<(), rust_config_tree::ConfigError>(())
/// ```
/// Collects template targets and binds generated templates to JSON Schemas.
///
/// TOML targets receive a `#:schema` directive. YAML targets receive a YAML
/// Language Server modeline. JSON and JSON5 targets receive a top-level
/// `$schema` property. Root targets bind `schema_path`; nested section targets
/// bind their generated section schema path.
///
/// # Type Parameters
///
/// - `S`: Config schema type used to discover includes and render templates.
///
/// # Arguments
///
/// - `config_path`: Root config path preferred as the template source when it
/// exists.
/// - `output_path`: Root output path for generated templates.
/// - `schema_path`: Root JSON Schema path to reference from root templates.
///
/// # Returns
///
/// Returns all generated template targets in traversal order.
///
/// # Examples
///
/// ```
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, template_targets_for_paths_with_schema};
/// use schemars::JsonSchema;
///
/// #[derive(Config, JsonSchema)]
/// struct AppConfig {
/// #[config(default = [])]
/// include: Vec<std::path::PathBuf>,
/// #[config(default = "demo")]
/// mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
/// fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
/// layer.include.clone().unwrap_or_default()
/// }
/// }
///
/// let targets = template_targets_for_paths_with_schema::<AppConfig>(
/// "config.yaml",
/// "config.example.yaml",
/// "schemas/config.schema.json",
/// )?;
///
/// assert!(targets[0].content.starts_with("# yaml-language-server: $schema="));
/// # Ok::<(), rust_config_tree::ConfigError>(())
/// ```
/// Writes all generated config templates for a config tree.
///
/// Parent directories are created before each target is written.
///
/// # Type Parameters
///
/// - `S`: Config schema type used to discover includes and render templates.
///
/// # Arguments
///
/// - `config_path`: Root config path preferred as the template source when it
/// exists.
/// - `output_path`: Root output path for generated templates.
///
/// # Returns
///
/// Returns `Ok(())` after all template files have been written.
///
/// # Examples
///
/// ```
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, write_config_templates};
/// use schemars::JsonSchema;
///
/// #[derive(Config, JsonSchema)]
/// struct AppConfig {
/// #[config(default = [])]
/// include: Vec<std::path::PathBuf>,
/// #[config(default = "demo")]
/// mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
/// fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
/// layer.include.clone().unwrap_or_default()
/// }
/// }
///
/// let output = std::env::temp_dir().join("rust-config-tree-template-doctest.yaml");
/// write_config_templates::<AppConfig>("config.yaml", &output)?;
///
/// assert!(output.exists());
/// # let _ = std::fs::remove_file(output);
/// # Ok::<(), rust_config_tree::ConfigError>(())
/// ```
/// Writes all generated config templates with editor schema bindings.
///
/// TOML targets receive `#:schema <path>`, YAML targets receive
/// `# yaml-language-server: $schema=<path>`, and JSON/JSON5 targets receive a
/// top-level `$schema` property. The schema path is rendered relative to each
/// template file. Root targets bind `schema_path`; nested section targets bind
/// their generated section schema path.
///
/// # Type Parameters
///
/// - `S`: Config schema type used to discover includes and render templates.
///
/// # Arguments
///
/// - `config_path`: Root config path preferred as the template source when it
/// exists.
/// - `output_path`: Root output path for generated templates.
/// - `schema_path`: Root JSON Schema path to reference from root templates.
///
/// # Returns
///
/// Returns `Ok(())` after all template files have been written.
///
/// # Examples
///
/// ```
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, write_config_templates_with_schema};
/// use schemars::JsonSchema;
///
/// #[derive(Config, JsonSchema)]
/// struct AppConfig {
/// #[config(default = [])]
/// include: Vec<std::path::PathBuf>,
/// #[config(default = "demo")]
/// mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
/// fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
/// layer.include.clone().unwrap_or_default()
/// }
/// }
///
/// let output = std::env::temp_dir().join("rust-config-tree-template-schema-doctest.yaml");
/// write_config_templates_with_schema::<AppConfig>(
/// "config.yaml",
/// &output,
/// "schemas/config.schema.json",
/// )?;
///
/// let content = std::fs::read_to_string(&output)?;
/// assert!(content.starts_with("# yaml-language-server: $schema="));
/// # let _ = std::fs::remove_file(output);
/// # Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
/// ```