ggen-config 26.7.2

Configuration parser and validator for ggen.toml files
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Ontology configuration system
//!
//! Integrates ontology packs with ggen.toml configuration, enabling:
//! - Declarative ontology pack management
//! - Automatic composition and resolution
//! - Version constraints and lock files
//! - Multi-language code generation configuration

use crate::config_lib::error::Result;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;

/// Ontology configuration section in ggen.toml
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OntologyConfig {
    /// Installed ontology pack references
    pub packs: Vec<OntologyPackRef>,

    /// Composition strategy when multiple packs are used
    pub composition: CompositionStrategy,

    /// Code generation targets and options
    pub targets: BTreeMap<String, TargetConfig>,

    /// Feature flags for code generation
    pub features: HashMap<String, bool>,

    /// Lock file configuration
    pub lock: LockConfig,
}

/// Reference to an installed ontology pack
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OntologyPackRef {
    /// Pack name (e.g., "schema-org", "dublin-core")
    pub name: String,

    /// Version constraint (e.g., "^3.13.0", "~1.11.0", "latest")
    pub version: String,

    /// Namespace filter (extract only specified namespace)
    pub namespace: Option<String>,

    /// Classes to include (if None, include all)
    pub classes: Option<Vec<String>>,

    /// Properties to include (if None, include all)
    pub properties: Option<Vec<String>>,

    /// Optional source URL for custom/private packs
    pub source: Option<String>,
}

/// Strategy for composing multiple ontologies
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CompositionStrategy {
    /// Union: Include all classes and properties from all packs
    Union,

    /// Intersection: Only include classes/properties common to all packs
    Intersection,

    /// Priority: First pack takes precedence in conflicts
    Priority,

    /// Custom: Apply custom merge rules
    Custom {
        /// Rules for handling conflicts (pack_name: resolution)
        rules: HashMap<String, ConflictResolution>,
    },
}

/// How to resolve conflicts when composing ontologies
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ConflictResolution {
    /// Use the definition from the first pack
    UseFirst,

    /// Use the definition from the second pack
    UseSecond,

    /// Merge conflicting definitions
    Merge,

    /// Exclude the conflicting element
    Exclude,
}

/// Code generation target configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TargetConfig {
    /// Target language (typescript, rust, python, go)
    pub language: String,

    /// Output directory for generated code
    pub output_dir: PathBuf,

    /// Features to enable for this language
    pub features: Vec<String>,

    /// Custom template path (overrides pack templates)
    pub template_path: Option<PathBuf>,

    /// Post-generation hooks
    pub hooks: Option<GenerationHooks>,
}

/// Hooks to run during/after code generation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GenerationHooks {
    /// Command to run before generation
    pub pre_generate: Option<String>,

    /// Command to run after generation
    pub post_generate: Option<String>,

    /// Validate generated code
    pub validate: Option<String>,
}

/// Lock file configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LockConfig {
    /// Path to lock file
    pub file: PathBuf,

    /// Auto-create lock file on update
    pub auto_update: bool,

    /// Require lock file for reproducible builds
    pub enforce: bool,
}

impl Default for OntologyConfig {
    fn default() -> Self {
        Self {
            packs: Vec::new(),
            composition: CompositionStrategy::Union,
            targets: BTreeMap::new(),
            features: HashMap::new(),
            lock: LockConfig::default(),
        }
    }
}

impl Default for LockConfig {
    fn default() -> Self {
        Self {
            file: PathBuf::from("ggen.lock"),
            auto_update: true,
            enforce: false,
        }
    }
}

impl OntologyConfig {
    /// Create a new ontology configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a pack reference
    pub fn with_pack(mut self, pack: OntologyPackRef) -> Self {
        self.packs.push(pack);
        self
    }

    /// Set composition strategy
    pub fn with_composition(mut self, strategy: CompositionStrategy) -> Self {
        self.composition = strategy;
        self
    }

    /// Add a target configuration
    pub fn with_target(mut self, name: String, config: TargetConfig) -> Self {
        self.targets.insert(name, config);
        self
    }

    /// Add a feature flag
    pub fn with_feature(mut self, name: String, enabled: bool) -> Self {
        self.features.insert(name, enabled);
        self
    }

    /// Load from TOML file section
    pub fn from_toml_section(toml_content: &str) -> Result<Self> {
        toml::from_str(toml_content).map_err(|e| {
            crate::config_lib::ConfigError::Validation(format!(
                "Failed to parse ontology config: {}",
                e
            ))
        })
    }

    /// Save to TOML format
    pub fn to_toml(&self) -> Result<String> {
        toml::to_string_pretty(self).map_err(|e| {
            crate::config_lib::ConfigError::Validation(format!(
                "Failed to serialize ontology config: {}",
                e
            ))
        })
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.packs.is_empty() {
            return Err(crate::config_lib::ConfigError::Validation(
                "No ontology packs configured".to_string(),
            ));
        }

        // Validate pack references
        for pack in &self.packs {
            if pack.name.is_empty() {
                return Err(crate::config_lib::ConfigError::Validation(
                    "Pack name cannot be empty".to_string(),
                ));
            }

            if pack.version.is_empty() {
                return Err(crate::config_lib::ConfigError::Validation(format!(
                    "Version for pack '{}' cannot be empty",
                    pack.name
                )));
            }
        }

        // Validate targets
        for (name, target) in &self.targets {
            if target.language.is_empty() {
                return Err(crate::config_lib::ConfigError::Validation(format!(
                    "Language for target '{}' cannot be empty",
                    name
                )));
            }
        }

        Ok(())
    }

    /// Get all referenced pack names
    pub fn pack_names(&self) -> Vec<&str> {
        self.packs.iter().map(|p| p.name.as_str()).collect()
    }

    /// Get all target languages
    pub fn target_languages(&self) -> Vec<&str> {
        self.targets.values().map(|t| t.language.as_str()).collect()
    }

    /// Check if a feature is enabled
    pub fn is_feature_enabled(&self, name: &str) -> bool {
        self.features.get(name).copied().unwrap_or(false)
    }
}

// =========================================================================
// Validate Trait Implementations
// =========================================================================

impl star_toml::Validate for OntologyConfig {
    fn validate(&self, v: &mut star_toml::Validator) {
        if self.packs.is_empty() {
            v.error(star_toml::ErrorKind::Empty, "No ontology packs configured");
        }
        for (i, pack) in self.packs.iter().enumerate() {
            v.index(i, |v| pack.validate(v));
        }
        for (name, target) in &self.targets {
            v.field(name, |v| target.validate(v));
        }
        v.field("lock", |v| self.lock.validate(v));
    }
}

impl star_toml::Validate for OntologyPackRef {
    fn validate(&self, v: &mut star_toml::Validator) {
        v.check_non_empty("name", &self.name);
        v.check_non_empty("version", &self.version);
    }
}

impl star_toml::Validate for TargetConfig {
    fn validate(&self, v: &mut star_toml::Validator) {
        v.check_non_empty("language", &self.language);
        v.check_path("output_dir", &self.output_dir.to_string_lossy(), None);
        if let Some(template_path) = &self.template_path {
            v.check_path("template_path", &template_path.to_string_lossy(), None);
        }
        if let Some(hooks) = &self.hooks {
            v.field("hooks", |v| hooks.validate(v));
        }
    }
}

impl star_toml::Validate for GenerationHooks {
    fn validate(&self, _v: &mut star_toml::Validator) {}
}

impl star_toml::Validate for LockConfig {
    fn validate(&self, v: &mut star_toml::Validator) {
        v.check_path("file", &self.file.to_string_lossy(), None);
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_ontology_config_builder() {
        let config = OntologyConfig::new()
            .with_pack(OntologyPackRef {
                name: "schema-org".to_string(),
                version: "^3.13.0".to_string(),
                namespace: Some("https://schema.org/".to_string()),
                classes: None,
                properties: None,
                source: None,
            })
            .with_composition(CompositionStrategy::Union)
            .with_feature("zod".to_string(), true);

        assert_eq!(config.packs.len(), 1);
        assert_eq!(config.packs[0].name, "schema-org");
        assert!(config.is_feature_enabled("zod"));
        assert!(!config.is_feature_enabled("serde"));
    }

    #[test]
    fn test_ontology_config_validation() {
        let config = OntologyConfig::new();
        assert!(config.validate().is_err()); // No packs

        let valid_config = OntologyConfig::new().with_pack(OntologyPackRef {
            name: "schema-org".to_string(),
            version: "3.13.0".to_string(),
            namespace: None,
            classes: None,
            properties: None,
            source: None,
        });

        assert!(valid_config.validate().is_ok());
    }

    #[test]
    fn test_composition_strategies() {
        assert_eq!(CompositionStrategy::Union, CompositionStrategy::Union);
        assert_ne!(
            CompositionStrategy::Union,
            CompositionStrategy::Intersection
        );
    }

    #[test]
    fn test_ontology_config_star_toml_validate() {
        use star_toml::Validate;

        let mut config = OntologyConfig::new();
        // Invalid: empty packs
        let errs = config.check().unwrap_err();
        let error_msgs: Vec<String> = errs.errors().iter().map(|e| e.msg.clone()).collect();
        assert!(error_msgs
            .iter()
            .any(|m| m.contains("No ontology packs configured")));

        // Add invalid pack ref
        config = config.with_pack(OntologyPackRef {
            name: "".to_string(),
            version: "".to_string(),
            namespace: None,
            classes: None,
            properties: None,
            source: None,
        });

        let errs = config.check().unwrap_err();
        let error_locs: Vec<String> = errs.errors().iter().map(|e| e.loc.to_string()).collect();
        assert!(error_locs.contains(&"[0].name".to_string()));
        assert!(error_locs.contains(&"[0].version".to_string()));

        // Add invalid target config
        config.targets.insert(
            "test-target".to_string(),
            TargetConfig {
                language: "".to_string(),
                output_dir: std::path::PathBuf::from("."),
                features: vec![],
                template_path: None,
                hooks: None,
            },
        );

        let errs = config.check().unwrap_err();
        assert!(errs
            .errors()
            .iter()
            .any(|e| e.loc.to_string() == "test-target.language"));

        // Add invalid paths to targets and lock config to check path validation
        config.packs[0] = OntologyPackRef {
            name: "valid-pack".to_string(),
            version: "1.0.0".to_string(),
            namespace: None,
            classes: None,
            properties: None,
            source: None,
        };
        config.targets.insert(
            "test-target".to_string(),
            TargetConfig {
                language: "rust".to_string(),
                output_dir: std::path::PathBuf::from("path/../../traversal"),
                features: vec![],
                template_path: Some(std::path::PathBuf::from("path/with\0null")),
                hooks: None,
            },
        );
        config.lock = LockConfig {
            file: std::path::PathBuf::from("lock/../../traversal"),
            auto_update: true,
            enforce: false,
        };

        let errs = config.check().unwrap_err();
        let error_locs: Vec<String> = errs.errors().iter().map(|e| e.loc.to_string()).collect();
        assert!(error_locs.contains(&"test-target.output_dir".to_string()));
        assert!(error_locs.contains(&"test-target.template_path".to_string()));
        assert!(error_locs.contains(&"lock.file".to_string()));
    }
}