1use std::collections::BTreeMap;
8use std::path::PathBuf;
9
10use indexmap::IndexMap;
11use serde::{Deserialize, Serialize};
12
13use crate::diagnostic::{SchemaDiagnostic, Severity, child_path};
14use crate::dialect::Dialect;
15use crate::filter::{FilterConfig, FilterError};
16use crate::names::SourceSubpath;
17
18#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum Source {
21 Git { url: String },
22 Path { path: PathBuf },
23}
24
25#[derive(Debug, Clone, PartialEq)]
29pub struct Entry {
30 pub source: Source,
31 pub version: Option<String>,
32 pub subpath: Option<SourceSubpath>,
33 pub dialect: Option<Dialect>,
35 pub plugin: Option<String>,
37 pub filter: FilterConfig,
38 pub unknown: BTreeMap<String, toml::Value>,
40}
41
42#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
46pub struct RawEntry {
47 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub url: Option<String>,
49 #[serde(default, skip_serializing_if = "Option::is_none", with = "path_serde")]
50 pub path: Option<PathBuf>,
51 #[serde(default, skip_serializing_if = "Option::is_none")]
52 pub subpath: Option<SourceSubpath>,
53 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub version: Option<String>,
55 #[serde(default, skip_serializing_if = "Option::is_none")]
56 pub dialect: Option<Dialect>,
57 #[serde(default, skip_serializing_if = "Option::is_none")]
58 pub plugin: Option<String>,
59 #[serde(flatten)]
60 pub filter: FilterConfig,
61 #[serde(flatten, default, skip_serializing_if = "BTreeMap::is_empty")]
62 pub unknown: BTreeMap<String, toml::Value>,
63}
64
65mod path_serde {
67 use std::path::PathBuf;
68
69 use serde::{Deserialize, Deserializer, Serializer};
70
71 pub fn serialize<S: Serializer>(
72 path: &Option<PathBuf>,
73 serializer: S,
74 ) -> Result<S::Ok, S::Error> {
75 match path {
76 Some(path) => serializer.serialize_some(&path.to_string_lossy().replace('\\', "/")),
77 None => serializer.serialize_none(),
78 }
79 }
80
81 pub fn deserialize<'de, D: Deserializer<'de>>(
82 deserializer: D,
83 ) -> Result<Option<PathBuf>, D::Error> {
84 Ok(Option::<String>::deserialize(deserializer)?.map(PathBuf::from))
85 }
86}
87
88#[derive(Debug, thiserror::Error, PartialEq, Eq)]
90pub enum EntryError {
91 #[error("source `{name}` has both `url` and `path` — pick one")]
92 BothSources { name: String },
93 #[error("source `{name}` has neither `url` nor `path` — one is required")]
94 NoSource { name: String },
95 #[error(transparent)]
96 PluginSelector(#[from] PluginSelectorError),
97 #[error(transparent)]
99 Filter(#[from] FilterError),
100}
101
102impl EntryError {
103 pub fn code(&self) -> &'static str {
105 match self {
106 Self::BothSources { .. } | Self::NoSource { .. } => "dependency.source",
107 Self::PluginSelector(_) => "dependency.plugin-selector",
108 Self::Filter(_) => "dependency.filter-conflict",
109 }
110 }
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub enum Strictness {
116 Consumer,
118 Package,
122}
123
124#[derive(Debug, Clone, PartialEq, Eq)]
126pub struct EntryWarning {
127 pub code: &'static str,
128 pub message: String,
129}
130
131impl Entry {
132 pub fn git(url: impl Into<String>) -> Self {
134 Self::from_source(Source::Git { url: url.into() })
135 }
136
137 pub fn at_path(path: impl Into<PathBuf>) -> Self {
139 Self::from_source(Source::Path { path: path.into() })
140 }
141
142 pub fn from_source(source: Source) -> Self {
144 Self {
145 source,
146 version: None,
147 subpath: None,
148 dialect: None,
149 plugin: None,
150 filter: FilterConfig::default(),
151 unknown: BTreeMap::new(),
152 }
153 }
154
155 pub fn url(&self) -> Option<&str> {
157 match &self.source {
158 Source::Git { url } => Some(url),
159 Source::Path { .. } => None,
160 }
161 }
162
163 pub fn path(&self) -> Option<&std::path::Path> {
165 match &self.source {
166 Source::Path { path } => Some(path),
167 Source::Git { .. } => None,
168 }
169 }
170
171 pub fn read(
177 name: &str,
178 mut raw: RawEntry,
179 strictness: Strictness,
180 ) -> Result<(Self, Vec<EntryWarning>), EntryError> {
181 let mut warnings = Vec::new();
182 if strictness == Strictness::Package && raw.plugin.is_some() && raw.dialect.take().is_some()
183 {
184 warnings.push(EntryWarning {
185 code: "dependency.dialect-ignored",
186 message: format!(
187 "source `{name}` has both `plugin` and `dialect`; the dialect is ignored"
188 ),
189 });
190 }
191 let source = match (raw.url, raw.path) {
192 (Some(url), None) => Source::Git {
193 url: crate::git_url::with_default_scheme(&url),
194 },
195 (None, Some(path)) => Source::Path { path },
196 (Some(_), Some(_)) => return Err(EntryError::BothSources { name: name.into() }),
197 (None, None) => return Err(EntryError::NoSource { name: name.into() }),
198 };
199 validate_plugin_selector(
200 name,
201 raw.plugin.as_deref(),
202 raw.subpath.as_ref(),
203 raw.dialect,
204 )?;
205 match (raw.filter.validate(name), strictness) {
206 (Err(conflict), Strictness::Consumer) => return Err(conflict.into()),
207 (Err(conflict), Strictness::Package) => warnings.push(EntryWarning {
208 code: "dependency.filter-conflict",
209 message: conflict.to_string(),
210 }),
211 (Ok(()), _) => {}
212 }
213 let entry = Self {
214 source,
215 version: raw.version,
216 subpath: raw.subpath,
217 dialect: raw.dialect,
218 plugin: raw.plugin,
219 filter: raw.filter,
220 unknown: raw.unknown,
221 };
222 Ok((entry, warnings))
223 }
224}
225
226impl From<&Entry> for RawEntry {
227 fn from(entry: &Entry) -> Self {
228 let (url, path) = match &entry.source {
229 Source::Git { url } => (Some(url.clone()), None),
230 Source::Path { path } => (None, Some(path.clone())),
231 };
232 Self {
233 url,
234 path,
235 subpath: entry.subpath.clone(),
236 version: entry.version.clone(),
237 dialect: entry.dialect,
238 plugin: entry.plugin.clone(),
239 filter: entry.filter.clone(),
240 unknown: entry.unknown.clone(),
241 }
242 }
243}
244
245impl From<Entry> for RawEntry {
246 fn from(entry: Entry) -> Self {
247 Self::from(&entry)
248 }
249}
250
251impl Serialize for Entry {
252 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
253 RawEntry::from(self).serialize(serializer)
254 }
255}
256
257#[derive(Debug, Clone, Default, PartialEq, Serialize)]
259pub struct Dependencies {
260 #[serde(skip_serializing_if = "IndexMap::is_empty")]
261 pub dependencies: IndexMap<String, Entry>,
262 #[serde(
263 rename = "local-dependencies",
264 skip_serializing_if = "IndexMap::is_empty"
265 )]
266 pub local_dependencies: IndexMap<String, Entry>,
267}
268
269impl Dependencies {
270 pub fn read(
277 document: &toml_edit::DocumentMut,
278 diagnostics: &mut Vec<SchemaDiagnostic>,
279 ) -> Self {
280 let tables = ["dependencies", "local-dependencies"].map(|key| match document.get(key) {
281 None => Ok(None),
282 Some(item) => item.as_table_like().map(Some).ok_or(key),
283 });
284 let [Ok(dependencies), Ok(local_dependencies)] = tables else {
285 let bad = tables.iter().find_map(|table| table.as_ref().err());
286 diagnostics.push(SchemaDiagnostic::new(
287 "dependency.invalid",
288 Severity::Error,
289 Vec::new(),
290 format!(
291 "`dependencies` and `local-dependencies` must be tables: `{}` is not",
292 bad.copied().unwrap_or_default()
293 ),
294 ));
295 return Self::default();
296 };
297 Self {
298 dependencies: read_table("dependencies", dependencies, Severity::Error, diagnostics),
299 local_dependencies: read_table(
300 "local-dependencies",
301 local_dependencies,
302 Severity::Warning,
303 diagnostics,
304 ),
305 }
306 }
307}
308
309fn read_table(
311 table: &str,
312 items: Option<&dyn toml_edit::TableLike>,
313 refusal: Severity,
314 diagnostics: &mut Vec<SchemaDiagnostic>,
315) -> IndexMap<String, Entry> {
316 let mut entries = IndexMap::new();
317 for (name, item) in items.into_iter().flat_map(|items| items.iter()) {
318 let path = child_path(table, name);
319 if let Err(message) = crate::ordered::reject_unholdable_key(name) {
322 diagnostics.push(SchemaDiagnostic::new(
323 "toml.non-json-value",
324 refusal,
325 path,
326 message,
327 ));
328 continue;
329 }
330 let key_path = format!("{table}.{name}");
331 let raw = match item.clone().into_value() {
332 Ok(value) => RawEntry::deserialize(
333 serde::de::IntoDeserializer::<toml_edit::de::Error>::into_deserializer(value),
334 )
335 .map_err(|error| format!("{key_path}: {}", error.to_string().trim_end())),
336 Err(_) => Err(format!("{key_path}: must be a table")),
337 };
338 let raw = match raw {
339 Ok(raw) => raw,
340 Err(message) => {
341 diagnostics.push(SchemaDiagnostic::new(
342 "dependency.invalid",
343 refusal,
344 path,
345 message,
346 ));
347 continue;
348 }
349 };
350 match Entry::read(name, raw, Strictness::Package) {
351 Ok((mut entry, warnings)) => {
352 let written = item.as_table_like();
355 let unknown = std::mem::take(&mut entry.unknown);
356 for (key, value) in unknown {
357 let holdable = crate::ordered::reject_unholdable_key(&key).and_then(|()| {
358 match written.and_then(|table| table.get(&key)) {
359 Some(item) => crate::manifest::item_json(item).and_then(|json| {
360 toml::Value::try_from(&json).map_err(|error| error.to_string())
361 }),
362 None => Ok(value),
363 }
364 });
365 match holdable {
366 Ok(value) => {
367 entry.unknown.insert(key, value);
368 }
369 Err(message) => {
370 let mut at = path.clone();
371 at.push(crate::diagnostic::PathSegment::Key(key));
372 diagnostics.push(SchemaDiagnostic::new(
373 "toml.non-json-value",
374 Severity::Warning,
375 at,
376 message,
377 ));
378 }
379 }
380 }
381 diagnostics.extend(warnings.into_iter().map(|warning| {
382 SchemaDiagnostic::new(
383 warning.code,
384 Severity::Warning,
385 path.clone(),
386 warning.message,
387 )
388 }));
389 entries.insert(name.to_owned(), entry);
390 }
391 Err(error) => diagnostics.push(SchemaDiagnostic::new(
392 error.code(),
393 refusal,
394 path,
395 error.to_string(),
396 )),
397 }
398 }
399 entries
400}
401
402#[derive(Debug, thiserror::Error, PartialEq, Eq)]
404pub enum PluginSelectorError {
405 #[error("source `{name}` has `plugin = \"{plugin}\"`; a plugin name is {rule}")]
406 BadName {
407 name: String,
408 plugin: String,
409 rule: &'static str,
410 },
411 #[error(
412 "source `{name}` has both `plugin` and `subpath` — a marketplace is read at the root of its source, and the marketplace entry says where the plugin is"
413 )]
414 WithSubpath { name: String },
415 #[error(
416 "source `{name}` has both `plugin` and `dialect` — a marketplace plugin is read as a Claude plugin"
417 )]
418 WithDialect { name: String },
419}
420
421pub fn validate_plugin_selector(
424 name: &str,
425 plugin: Option<&str>,
426 subpath: Option<&SourceSubpath>,
427 dialect: Option<Dialect>,
428) -> Result<(), PluginSelectorError> {
429 let Some(plugin) = plugin else {
430 return Ok(());
431 };
432 let name = name.to_string();
433 if !crate::claude::manifest::is_plugin_name(plugin) {
434 return Err(PluginSelectorError::BadName {
435 name,
436 plugin: plugin.to_string(),
437 rule: crate::claude::manifest::PLUGIN_NAME_RULE,
438 });
439 }
440 if subpath.is_some() {
441 return Err(PluginSelectorError::WithSubpath { name });
442 }
443 if dialect.is_some() {
444 return Err(PluginSelectorError::WithDialect { name });
445 }
446 Ok(())
447}