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 pub custom_header: Option<String>,
44}
45
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
48pub struct TargetConfig {
49 pub target: String,
50 pub output: String,
51 pub enabled: Option<bool>,
52 pub backend: Option<String>,
53 pub package: Option<String>,
54 pub namespace: Option<String>,
55 pub strict_facets: Option<bool>,
56 pub slots: Option<bool>,
57 pub kw_only: Option<bool>,
58 pub zero_copy: Option<bool>,
59 pub codecs: Option<bool>,
60 pub standard: Option<String>,
61 pub derive_traits: Option<Vec<String>>,
62 pub box_cycles: Option<bool>,
63 pub modules: Option<bool>,
64 pub mode: Option<String>,
65 pub serializer: Option<String>,
66 pub zod: Option<bool>,
67 pub source_gen: Option<bool>,
68 pub record_kind: Option<String>,
69 pub rkyv: Option<bool>,
70 pub custom_header: Option<String>,
71}
72
73#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
74pub struct CodegenTargetConfig {
75 pub enabled: Option<bool>,
76 pub output: Option<String>,
77 pub backend: Option<String>,
78 pub package: Option<String>,
79 pub namespace: Option<String>,
80 pub strict_facets: Option<bool>,
81 pub slots: Option<bool>,
82 pub kw_only: Option<bool>,
83 pub zero_copy: Option<bool>,
84 pub codecs: Option<bool>,
85 pub standard: Option<String>,
86 pub derive_traits: Option<Vec<String>>,
87 pub box_cycles: Option<bool>,
88 pub modules: Option<bool>,
89 pub mode: Option<String>,
90 pub serializer: Option<String>,
91 pub zod: Option<bool>,
92 pub source_gen: Option<bool>,
93 pub record_kind: Option<String>,
94 pub rkyv: Option<bool>,
95 pub custom_header: Option<String>,
96}
97
98impl std::str::FromStr for WorkspaceManifest {
99 type Err = ConfigError;
100
101 fn from_str(toml_str: &str) -> Result<Self, Self::Err> {
102 let manifest: WorkspaceManifest = toml::from_str(toml_str)?;
103 Ok(manifest)
104 }
105}
106
107impl WorkspaceManifest {
108 pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
109 let content = fs::read_to_string(path)?;
110 content.parse()
111 }
112
113 pub fn resolved_targets(&self) -> Vec<TargetConfig> {
116 let mut targets = Vec::new();
117
118 let ws_header = self
119 .workspace
120 .as_ref()
121 .and_then(|w| w.custom_header.clone());
122
123 for gen in &self.generate {
125 if gen.enabled.unwrap_or(true) {
126 let mut target = gen.clone();
127 if target.custom_header.is_none() {
128 target.custom_header = ws_header.clone();
129 }
130 targets.push(target);
131 }
132 }
133
134 if let Some(ref codegen_map) = self.codegen {
136 for (lang, cfg) in codegen_map {
137 if cfg.enabled.unwrap_or(true) {
138 let output = cfg
139 .output
140 .clone()
141 .unwrap_or_else(|| format!("generated/{}", lang));
142
143 targets.push(TargetConfig {
144 target: lang.clone(),
145 output,
146 enabled: cfg.enabled,
147 backend: cfg.backend.clone(),
148 package: cfg.package.clone(),
149 namespace: cfg.namespace.clone(),
150 strict_facets: cfg.strict_facets,
151 slots: cfg.slots,
152 kw_only: cfg.kw_only,
153 zero_copy: cfg.zero_copy,
154 codecs: cfg.codecs,
155 standard: cfg.standard.clone(),
156 derive_traits: cfg.derive_traits.clone(),
157 box_cycles: cfg.box_cycles,
158 modules: cfg.modules,
159 mode: cfg.mode.clone(),
160 serializer: cfg.serializer.clone(),
161 zod: cfg.zod,
162 source_gen: cfg.source_gen,
163 record_kind: cfg.record_kind.clone(),
164 rkyv: cfg.rkyv,
165 custom_header: cfg.custom_header.clone().or_else(|| ws_header.clone()),
166 });
167 }
168 }
169 }
170
171 targets
172 }
173
174 pub fn expand_schemas(&self, base_dir: &Path) -> Result<Vec<PathBuf>, ConfigError> {
176 let mut paths = Vec::new();
177
178 let Some(ref ws) = self.workspace else {
179 return Ok(paths);
180 };
181
182 for pattern in &ws.schemas {
183 let full_pattern = if Path::new(pattern).is_absolute() {
184 pattern.clone()
185 } else {
186 base_dir.join(pattern).to_string_lossy().to_string()
187 };
188
189 let entries = glob(&full_pattern).map_err(|e| ConfigError::GlobPattern {
190 pattern: full_pattern.clone(),
191 error: e,
192 })?;
193
194 for entry in entries {
195 let path = entry?;
196 if path.is_file() {
197 paths.push(path);
198 }
199 }
200 }
201
202 paths.sort();
203 paths.dedup();
204 Ok(paths)
205 }
206}