1use std::collections::HashMap;
2use std::fs;
3use std::path::{Path, PathBuf};
4
5use glob::glob;
6use serde::{Deserialize, Serialize};
7use thiserror::Error;
8
9#[derive(Debug, Error)]
10pub enum ConfigError {
11 #[error("I/O error reading configuration: {0}")]
12 Io(#[from] std::io::Error),
13
14 #[error("TOML syntax error: {0}")]
15 Toml(#[from] toml::de::Error),
16
17 #[error("Invalid glob pattern '{pattern}': {error}")]
18 GlobPattern {
19 pattern: String,
20 error: glob::PatternError,
21 },
22
23 #[error("Failed to read glob path: {0}")]
24 Glob(#[from] glob::GlobError),
25}
26
27#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
29pub struct WorkspaceManifest {
30 pub workspace: Option<WorkspaceSection>,
31 #[serde(default)]
32 pub generate: Vec<TargetConfig>,
33 pub codegen: Option<HashMap<String, CodegenTargetConfig>>,
34}
35
36#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
37pub struct WorkspaceSection {
38 pub name: Option<String>,
39 #[serde(default)]
40 pub schemas: Vec<String>,
41 pub include_dirs: Option<Vec<String>>,
42 pub output_base_dir: Option<String>,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47pub struct TargetConfig {
48 pub target: String,
49 pub output: String,
50 pub enabled: Option<bool>,
51 pub backend: Option<String>,
52 pub package: Option<String>,
53 pub namespace: Option<String>,
54 pub strict_facets: Option<bool>,
55 pub slots: Option<bool>,
56 pub kw_only: Option<bool>,
57 pub zero_copy: Option<bool>,
58 pub codecs: Option<bool>,
59 pub standard: Option<String>,
60 pub derive_traits: Option<Vec<String>>,
61 pub box_cycles: Option<bool>,
62 pub modules: Option<bool>,
63 pub mode: Option<String>,
64 pub serializer: Option<String>,
65 pub zod: Option<bool>,
66 pub source_gen: Option<bool>,
67 pub record_kind: Option<String>,
68 pub rkyv: Option<bool>,
69}
70
71#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
72pub struct CodegenTargetConfig {
73 pub enabled: Option<bool>,
74 pub output: Option<String>,
75 pub backend: Option<String>,
76 pub package: Option<String>,
77 pub namespace: Option<String>,
78 pub strict_facets: Option<bool>,
79 pub slots: Option<bool>,
80 pub kw_only: Option<bool>,
81 pub zero_copy: Option<bool>,
82 pub codecs: Option<bool>,
83 pub standard: Option<String>,
84 pub derive_traits: Option<Vec<String>>,
85 pub box_cycles: Option<bool>,
86 pub modules: Option<bool>,
87 pub mode: Option<String>,
88 pub serializer: Option<String>,
89 pub zod: Option<bool>,
90 pub source_gen: Option<bool>,
91 pub record_kind: Option<String>,
92 pub rkyv: Option<bool>,
93}
94
95impl std::str::FromStr for WorkspaceManifest {
96 type Err = ConfigError;
97
98 fn from_str(toml_str: &str) -> Result<Self, Self::Err> {
99 let manifest: WorkspaceManifest = toml::from_str(toml_str)?;
100 Ok(manifest)
101 }
102}
103
104impl WorkspaceManifest {
105 pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
106 let content = fs::read_to_string(path)?;
107 content.parse()
108 }
109
110 pub fn resolved_targets(&self) -> Vec<TargetConfig> {
113 let mut targets = Vec::new();
114
115 for gen in &self.generate {
117 if gen.enabled.unwrap_or(true) {
118 targets.push(gen.clone());
119 }
120 }
121
122 if let Some(ref codegen_map) = self.codegen {
124 for (lang, cfg) in codegen_map {
125 if cfg.enabled.unwrap_or(true) {
126 let output = cfg
127 .output
128 .clone()
129 .unwrap_or_else(|| format!("generated/{}", lang));
130
131 targets.push(TargetConfig {
132 target: lang.clone(),
133 output,
134 enabled: cfg.enabled,
135 backend: cfg.backend.clone(),
136 package: cfg.package.clone(),
137 namespace: cfg.namespace.clone(),
138 strict_facets: cfg.strict_facets,
139 slots: cfg.slots,
140 kw_only: cfg.kw_only,
141 zero_copy: cfg.zero_copy,
142 codecs: cfg.codecs,
143 standard: cfg.standard.clone(),
144 derive_traits: cfg.derive_traits.clone(),
145 box_cycles: cfg.box_cycles,
146 modules: cfg.modules,
147 mode: cfg.mode.clone(),
148 serializer: cfg.serializer.clone(),
149 zod: cfg.zod,
150 source_gen: cfg.source_gen,
151 record_kind: cfg.record_kind.clone(),
152 rkyv: cfg.rkyv,
153 });
154 }
155 }
156 }
157
158 targets
159 }
160
161 pub fn expand_schemas(&self, base_dir: &Path) -> Result<Vec<PathBuf>, ConfigError> {
163 let mut paths = Vec::new();
164
165 let Some(ref ws) = self.workspace else {
166 return Ok(paths);
167 };
168
169 for pattern in &ws.schemas {
170 let full_pattern = if Path::new(pattern).is_absolute() {
171 pattern.clone()
172 } else {
173 base_dir.join(pattern).to_string_lossy().to_string()
174 };
175
176 let entries = glob(&full_pattern).map_err(|e| ConfigError::GlobPattern {
177 pattern: full_pattern.clone(),
178 error: e,
179 })?;
180
181 for entry in entries {
182 let path = entry?;
183 if path.is_file() {
184 paths.push(path);
185 }
186 }
187 }
188
189 paths.sort();
190 paths.dedup();
191 Ok(paths)
192 }
193}