1use serde::{Deserialize, Serialize};
9
10use crate::checksum::Checksum;
11use crate::enums::{
12 ArtifactType, CleEventType, CollectionOwner, CollectionUpdateReasonType,
13 ComplianceDocumentType, IdentifierType, TeaErrorCode,
14};
15use crate::error::ModelError;
16use crate::ids::{
17 ArtifactId, CollectionId, ComponentId, ComponentReleaseId, DistributionId, ProductId,
18 ProductReleaseId, TeaTimestamp,
19};
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24#[non_exhaustive]
25pub struct Identifier {
26 pub id_type: IdentifierType,
28 pub id_value: String,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(rename_all = "camelCase")]
35#[non_exhaustive]
36pub struct Product {
37 pub uuid: ProductId,
39 pub name: String,
41 pub identifiers: Vec<Identifier>,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47#[non_exhaustive]
48pub struct ComponentRef {
49 pub uuid: ComponentId,
51 #[serde(default)]
53 pub release: Option<ComponentReleaseId>,
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
58#[serde(rename_all = "camelCase")]
59#[non_exhaustive]
60pub struct ProductRelease {
61 pub uuid: ProductReleaseId,
63 pub product: ProductId,
65 #[serde(default)]
67 pub product_name: Option<String>,
68 pub version: String,
70 pub created_date: TeaTimestamp,
72 #[serde(default)]
74 pub release_date: Option<TeaTimestamp>,
75 #[serde(default)]
77 pub pre_release: Option<bool>,
78 #[serde(default)]
80 pub identifiers: Option<Vec<Identifier>>,
81 pub components: Vec<ComponentRef>,
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
87#[serde(rename_all = "camelCase")]
88#[non_exhaustive]
89pub struct ProductReleaseWithCollection {
90 #[serde(flatten)]
92 pub release: ProductRelease,
93 pub latest_collection: Collection,
95}
96
97#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
99#[serde(rename_all = "camelCase")]
100#[non_exhaustive]
101pub struct Component {
102 pub uuid: ComponentId,
104 pub name: String,
106 pub identifiers: Vec<Identifier>,
108}
109
110#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
112#[serde(rename_all = "camelCase")]
113#[non_exhaustive]
114pub struct ReleaseDistribution {
115 pub distribution_id: DistributionId,
117 #[serde(default)]
119 pub description: Option<String>,
120 #[serde(default)]
122 pub identifiers: Option<Vec<Identifier>>,
123 #[serde(default)]
125 pub url: Option<String>,
126 #[serde(default)]
128 pub signature_url: Option<String>,
129 #[serde(default)]
131 pub checksums: Option<Vec<Checksum>>,
132}
133
134#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
136#[serde(rename_all = "camelCase")]
137#[non_exhaustive]
138pub struct ComponentRelease {
139 pub uuid: ComponentReleaseId,
141 pub component: ComponentId,
143 #[serde(default)]
145 pub component_name: Option<String>,
146 pub version: String,
148 pub created_date: TeaTimestamp,
150 #[serde(default)]
152 pub release_date: Option<TeaTimestamp>,
153 #[serde(default)]
155 pub pre_release: Option<bool>,
156 #[serde(default)]
158 pub identifiers: Option<Vec<Identifier>>,
159 #[serde(default)]
161 pub distributions: Option<Vec<ReleaseDistribution>>,
162}
163
164#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
166#[serde(rename_all = "camelCase")]
167#[non_exhaustive]
168pub struct ComponentReleaseWithCollection {
169 #[serde(flatten)]
171 pub release: ComponentRelease,
172 pub latest_collection: Collection,
174}
175
176#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
178#[non_exhaustive]
179pub struct CollectionUpdateReason {
180 #[serde(rename = "type")]
182 pub reason_type: CollectionUpdateReasonType,
183 #[serde(default)]
185 pub comment: Option<String>,
186}
187
188#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
190#[serde(rename_all = "camelCase")]
191#[non_exhaustive]
192pub struct ArtifactFormat {
193 pub media_type: String,
195 #[serde(default)]
197 pub description: Option<String>,
198 #[serde(default)]
200 pub url: Option<String>,
201 #[serde(default)]
203 pub signature_url: Option<String>,
204 #[serde(default)]
206 pub checksums: Option<Vec<Checksum>>,
207}
208
209impl ArtifactFormat {
210 pub fn validate(&self) -> Result<(), ModelError> {
216 if let Some(checksums) = &self.checksums {
217 if checksums.is_empty() {
218 return Err(ModelError::Invalid(
219 "checksums, when present, must contain at least one entry".into(),
220 ));
221 }
222 }
223 if self.url.is_some() && self.checksums.as_ref().map(Vec::is_empty).unwrap_or(true) {
224 return Err(ModelError::Invalid(
225 "an artifact format with url must publish at least one checksum".into(),
226 ));
227 }
228 if self.media_type.is_empty() {
229 return Err(ModelError::Invalid("mediaType must not be empty".into()));
230 }
231 Ok(())
232 }
233}
234
235#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
237#[serde(rename_all = "camelCase")]
238#[non_exhaustive]
239pub struct Artifact {
240 pub uuid: ArtifactId,
242 pub version: u64,
244 #[serde(default)]
246 pub name: Option<String>,
247 #[serde(rename = "type")]
249 pub artifact_type: ArtifactType,
250 pub created_date: TeaTimestamp,
252 #[serde(default)]
254 pub distribution_ids: Option<Vec<DistributionId>>,
255 pub formats: Vec<ArtifactFormat>,
257}
258
259impl Artifact {
260 pub fn validate(&self) -> Result<(), ModelError> {
266 if self.version < 1 {
267 return Err(ModelError::Invalid("artifact version starts at 1".into()));
268 }
269 if self.formats.is_empty() {
270 return Err(ModelError::Invalid(
271 "artifact formats must not be empty".into(),
272 ));
273 }
274 let mut seen = Vec::new();
275 for format in &self.formats {
276 format.validate()?;
277 if seen
278 .iter()
279 .any(|existing: &String| existing == &format.media_type)
280 {
281 return Err(ModelError::Invalid(format!(
282 "mediaType `{}` is repeated in one artifact revision",
283 format.media_type
284 )));
285 }
286 seen.push(format.media_type.clone());
287 }
288 Ok(())
289 }
290
291 #[must_use]
293 pub fn format(&self, media_type: &str) -> Option<&ArtifactFormat> {
294 self.formats
295 .iter()
296 .find(|format| format.media_type == media_type)
297 }
298}
299
300#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
302#[serde(rename_all = "camelCase")]
303#[non_exhaustive]
304pub struct Collection {
305 pub uuid: CollectionId,
307 pub version: u64,
309 pub created_date: TeaTimestamp,
311 pub belongs_to: CollectionOwner,
313 pub update_reason: CollectionUpdateReason,
315 pub artifacts: Vec<Artifact>,
317}
318
319impl Collection {
320 pub fn validate(&self) -> Result<(), ModelError> {
326 if self.version < 1 {
327 return Err(ModelError::Invalid("collection version starts at 1".into()));
328 }
329 for artifact in &self.artifacts {
330 artifact.validate()?;
331 }
332 Ok(())
333 }
334}
335
336#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
338#[serde(rename_all = "camelCase")]
339#[non_exhaustive]
340pub struct Page<T> {
341 pub has_next: bool,
343 #[serde(default)]
345 pub next_page_token: Option<String>,
346 pub results: Vec<T>,
348}
349
350impl<T> Page<T> {
351 #[must_use]
353 pub fn new(results: Vec<T>, has_next: bool, next_page_token: Option<String>) -> Self {
354 Self {
355 has_next,
356 next_page_token,
357 results,
358 }
359 }
360}
361
362#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
368#[non_exhaustive]
369pub struct TeaErrorBody {
370 pub error: TeaErrorCode,
372 #[serde(default)]
374 pub message: Option<String>,
375}
376
377impl TeaErrorBody {
378 pub fn parse(bytes: &[u8]) -> Result<Self, ModelError> {
384 Ok(serde_json::from_slice(bytes)?)
385 }
386}
387
388#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
390#[non_exhaustive]
391pub struct TokenErrorBody {
392 pub error: String,
394 #[serde(default)]
396 pub error_description: Option<String>,
397 #[serde(default)]
399 pub error_uri: Option<String>,
400}
401
402#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
404#[non_exhaustive]
405pub struct TokenResponse {
406 pub access_token: String,
408 pub token_type: String,
410 #[serde(default)]
412 pub expires_in: Option<u64>,
413 #[serde(default)]
415 pub scope: Option<String>,
416}
417
418impl TokenResponse {
419 pub fn bearer(self) -> Result<Self, ModelError> {
425 if !self.token_type.eq_ignore_ascii_case("bearer") {
426 return Err(ModelError::Invalid(format!(
427 "token_type must be Bearer, got `{}`",
428 self.token_type
429 )));
430 }
431 if self.access_token.is_empty() {
432 return Err(ModelError::Invalid("access_token is empty".into()));
433 }
434 Ok(self)
435 }
436}
437
438#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
440#[non_exhaustive]
441pub struct CleSupportDefinition {
442 pub id: String,
444 pub description: String,
446 #[serde(default)]
448 pub url: Option<String>,
449}
450
451#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
453#[non_exhaustive]
454pub struct CleDefinitions {
455 #[serde(default)]
457 pub support: Option<Vec<CleSupportDefinition>>,
458}
459
460#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
462#[serde(rename_all = "camelCase")]
463#[non_exhaustive]
464pub struct CleEvent {
465 pub id: i64,
467 #[serde(rename = "type")]
469 pub event_type: CleEventType,
470 pub effective: TeaTimestamp,
472 pub published: TeaTimestamp,
474 #[serde(default)]
476 pub version: Option<String>,
477 #[serde(default)]
479 pub versions: Option<Vec<CleVersionSpecifier>>,
480 #[serde(default)]
482 pub support_id: Option<String>,
483 #[serde(default)]
485 pub license: Option<String>,
486 #[serde(default)]
488 pub superseded_by_version: Option<String>,
489 #[serde(default)]
491 pub identifiers: Option<Vec<Identifier>>,
492 #[serde(default)]
494 pub event_id: Option<i64>,
495 #[serde(default)]
497 pub reason: Option<String>,
498 #[serde(default)]
500 pub description: Option<String>,
501 #[serde(default)]
503 pub references: Option<Vec<String>>,
504}
505
506#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
508#[non_exhaustive]
509pub struct CleVersionSpecifier {
510 #[serde(default)]
512 pub version: Option<String>,
513 #[serde(default)]
515 pub range: Option<String>,
516}
517
518#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
520#[non_exhaustive]
521pub struct Cle {
522 pub events: Vec<CleEvent>,
524 #[serde(default)]
526 pub definitions: Option<CleDefinitions>,
527}
528
529#[derive(Debug, Clone, PartialEq, Eq)]
531pub struct ComplianceDocument(pub ComplianceDocumentType);
532
533pub fn parse_json<T>(bytes: &[u8]) -> Result<T, ModelError>
539where
540 T: for<'de> Deserialize<'de>,
541{
542 Ok(serde_json::from_slice(bytes)?)
543}
544
545pub fn parse_artifact(bytes: &[u8]) -> Result<Artifact, ModelError> {
551 let artifact: Artifact = parse_json(bytes)?;
552 artifact.validate()?;
553 Ok(artifact)
554}
555
556pub fn parse_collection(bytes: &[u8]) -> Result<Collection, ModelError> {
562 let collection: Collection = parse_json(bytes)?;
563 collection.validate()?;
564 Ok(collection)
565}
566
567pub fn parse_product_release(bytes: &[u8]) -> Result<ProductReleaseWithCollection, ModelError> {
573 let value: ProductReleaseWithCollection = parse_json(bytes)?;
574 value.latest_collection.validate()?;
575 Ok(value)
576}
577
578pub fn parse_component_release(bytes: &[u8]) -> Result<ComponentReleaseWithCollection, ModelError> {
584 let value: ComponentReleaseWithCollection = parse_json(bytes)?;
585 value.latest_collection.validate()?;
586 Ok(value)
587}
588
589#[cfg(test)]
590mod tests {
591 use super::*;
592
593 #[test]
594 fn artifact_with_url_requires_checksum() {
595 let json = br#"{
596 "uuid": "2ff3ae7c-8eab-4e0b-9b8b-1a0c4c4c1c2d",
597 "version": 1,
598 "type": "BOM",
599 "createdDate": "2026-01-02T03:04:05Z",
600 "formats": [{"mediaType": "application/vnd.cyclonedx+json", "url": "https://cdn.example/sbom.json"}]
601 }"#;
602 assert!(parse_artifact(json).is_err());
603 }
604
605 #[test]
606 fn preserves_unknown_artifact_type() {
607 let json = br#"{
608 "uuid": "2ff3ae7c-8eab-4e0b-9b8b-1a0c4c4c1c2d",
609 "version": 1,
610 "type": "FUTURE_DOC",
611 "createdDate": "2026-01-02T03:04:05.5Z",
612 "formats": [{"mediaType": "application/json", "checksums": [{"algType": "SHA-256", "algValue": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}]
613 }"#;
614 let artifact = parse_artifact(json).unwrap();
615 assert_eq!(artifact.artifact_type.as_str(), "FUTURE_DOC");
616 }
617}