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
32impl Identifier {
33 #[must_use]
40 pub fn purl(&self) -> Option<Result<crate::purl::Purl, crate::purl::PurlError>> {
41 (self.id_type == IdentifierType::Purl).then(|| crate::purl::Purl::parse(&self.id_value))
42 }
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48#[non_exhaustive]
49pub struct Product {
50 pub uuid: ProductId,
52 pub name: String,
54 pub identifiers: Vec<Identifier>,
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60#[non_exhaustive]
61pub struct ComponentRef {
62 pub uuid: ComponentId,
64 #[serde(default)]
66 pub release: Option<ComponentReleaseId>,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
71#[serde(rename_all = "camelCase")]
72#[non_exhaustive]
73pub struct ProductRelease {
74 pub uuid: ProductReleaseId,
76 pub product: ProductId,
78 #[serde(default)]
80 pub product_name: Option<String>,
81 pub version: String,
83 pub created_date: TeaTimestamp,
85 #[serde(default)]
87 pub release_date: Option<TeaTimestamp>,
88 #[serde(default)]
90 pub pre_release: Option<bool>,
91 #[serde(default)]
93 pub identifiers: Option<Vec<Identifier>>,
94 pub components: Vec<ComponentRef>,
96}
97
98#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase")]
101#[non_exhaustive]
102pub struct ProductReleaseWithCollection {
103 #[serde(flatten)]
105 pub release: ProductRelease,
106 pub latest_collection: Collection,
108}
109
110#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
112#[serde(rename_all = "camelCase")]
113#[non_exhaustive]
114pub struct Component {
115 pub uuid: ComponentId,
117 pub name: String,
119 pub identifiers: Vec<Identifier>,
121}
122
123#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase")]
126#[non_exhaustive]
127pub struct ReleaseDistribution {
128 pub distribution_id: DistributionId,
130 #[serde(default)]
132 pub description: Option<String>,
133 #[serde(default)]
135 pub identifiers: Option<Vec<Identifier>>,
136 #[serde(default)]
138 pub url: Option<String>,
139 #[serde(default)]
141 pub signature_url: Option<String>,
142 #[serde(default)]
144 pub checksums: Option<Vec<Checksum>>,
145}
146
147#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149#[serde(rename_all = "camelCase")]
150#[non_exhaustive]
151pub struct ComponentRelease {
152 pub uuid: ComponentReleaseId,
154 pub component: ComponentId,
156 #[serde(default)]
158 pub component_name: Option<String>,
159 pub version: String,
161 pub created_date: TeaTimestamp,
163 #[serde(default)]
165 pub release_date: Option<TeaTimestamp>,
166 #[serde(default)]
168 pub pre_release: Option<bool>,
169 #[serde(default)]
171 pub identifiers: Option<Vec<Identifier>>,
172 #[serde(default)]
174 pub distributions: Option<Vec<ReleaseDistribution>>,
175}
176
177#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
179#[serde(rename_all = "camelCase")]
180#[non_exhaustive]
181pub struct ComponentReleaseWithCollection {
182 #[serde(flatten)]
184 pub release: ComponentRelease,
185 pub latest_collection: Collection,
187}
188
189#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
191#[non_exhaustive]
192pub struct CollectionUpdateReason {
193 #[serde(rename = "type")]
195 pub reason_type: CollectionUpdateReasonType,
196 #[serde(default)]
198 pub comment: Option<String>,
199}
200
201#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
203#[serde(rename_all = "camelCase")]
204#[non_exhaustive]
205pub struct ArtifactFormat {
206 pub media_type: String,
208 #[serde(default)]
210 pub description: Option<String>,
211 #[serde(default)]
213 pub url: Option<String>,
214 #[serde(default)]
216 pub signature_url: Option<String>,
217 #[serde(default)]
219 pub checksums: Option<Vec<Checksum>>,
220}
221
222impl ArtifactFormat {
223 pub fn validate(&self) -> Result<(), ModelError> {
229 if let Some(checksums) = &self.checksums {
230 if checksums.is_empty() {
231 return Err(ModelError::Invalid(
232 "checksums, when present, must contain at least one entry".into(),
233 ));
234 }
235 }
236 if self.url.is_some() && self.checksums.as_ref().map(Vec::is_empty).unwrap_or(true) {
237 return Err(ModelError::Invalid(
238 "an artifact format with url must publish at least one checksum".into(),
239 ));
240 }
241 if self.media_type.is_empty() {
242 return Err(ModelError::Invalid("mediaType must not be empty".into()));
243 }
244 Ok(())
245 }
246}
247
248#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
250#[serde(rename_all = "camelCase")]
251#[non_exhaustive]
252pub struct Artifact {
253 pub uuid: ArtifactId,
255 pub version: u64,
257 #[serde(default)]
259 pub name: Option<String>,
260 #[serde(rename = "type")]
262 pub artifact_type: ArtifactType,
263 pub created_date: TeaTimestamp,
265 #[serde(default)]
267 pub distribution_ids: Option<Vec<DistributionId>>,
268 pub formats: Vec<ArtifactFormat>,
270}
271
272impl Artifact {
273 pub fn validate(&self) -> Result<(), ModelError> {
279 if self.version < 1 {
280 return Err(ModelError::Invalid("artifact version starts at 1".into()));
281 }
282 if self.formats.is_empty() {
283 return Err(ModelError::Invalid(
284 "artifact formats must not be empty".into(),
285 ));
286 }
287 let mut seen = Vec::new();
288 for format in &self.formats {
289 format.validate()?;
290 if seen
291 .iter()
292 .any(|existing: &String| existing == &format.media_type)
293 {
294 return Err(ModelError::Invalid(format!(
295 "mediaType `{}` is repeated in one artifact revision",
296 format.media_type
297 )));
298 }
299 seen.push(format.media_type.clone());
300 }
301 Ok(())
302 }
303
304 #[must_use]
306 pub fn format(&self, media_type: &str) -> Option<&ArtifactFormat> {
307 self.formats
308 .iter()
309 .find(|format| format.media_type == media_type)
310 }
311}
312
313#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
315#[serde(rename_all = "camelCase")]
316#[non_exhaustive]
317pub struct Collection {
318 pub uuid: CollectionId,
320 pub version: u64,
322 pub created_date: TeaTimestamp,
324 pub belongs_to: CollectionOwner,
326 pub update_reason: CollectionUpdateReason,
328 pub artifacts: Vec<Artifact>,
330}
331
332impl Collection {
333 pub fn validate(&self) -> Result<(), ModelError> {
339 if self.version < 1 {
340 return Err(ModelError::Invalid("collection version starts at 1".into()));
341 }
342 for artifact in &self.artifacts {
343 artifact.validate()?;
344 }
345 Ok(())
346 }
347}
348
349#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
351#[serde(rename_all = "camelCase")]
352#[non_exhaustive]
353pub struct Page<T> {
354 pub has_next: bool,
356 #[serde(default)]
358 pub next_page_token: Option<String>,
359 pub results: Vec<T>,
361}
362
363impl<T> Page<T> {
364 #[must_use]
366 pub fn new(results: Vec<T>, has_next: bool, next_page_token: Option<String>) -> Self {
367 Self {
368 has_next,
369 next_page_token,
370 results,
371 }
372 }
373}
374
375#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
381#[non_exhaustive]
382pub struct TeaErrorBody {
383 pub error: TeaErrorCode,
385 #[serde(default)]
387 pub message: Option<String>,
388}
389
390impl TeaErrorBody {
391 pub fn parse(bytes: &[u8]) -> Result<Self, ModelError> {
397 Ok(serde_json::from_slice(bytes)?)
398 }
399}
400
401#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
403#[non_exhaustive]
404pub struct TokenErrorBody {
405 pub error: String,
407 #[serde(default)]
409 pub error_description: Option<String>,
410 #[serde(default)]
412 pub error_uri: Option<String>,
413}
414
415#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
417#[non_exhaustive]
418pub struct TokenResponse {
419 pub access_token: String,
421 pub token_type: String,
423 #[serde(default)]
425 pub expires_in: Option<u64>,
426 #[serde(default)]
428 pub scope: Option<String>,
429}
430
431impl TokenResponse {
432 pub fn bearer(self) -> Result<Self, ModelError> {
438 if !self.token_type.eq_ignore_ascii_case("bearer") {
439 return Err(ModelError::Invalid(format!(
440 "token_type must be Bearer, got `{}`",
441 self.token_type
442 )));
443 }
444 if self.access_token.is_empty() {
445 return Err(ModelError::Invalid("access_token is empty".into()));
446 }
447 Ok(self)
448 }
449}
450
451#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
453#[non_exhaustive]
454pub struct CleSupportDefinition {
455 pub id: String,
457 pub description: String,
459 #[serde(default)]
461 pub url: Option<String>,
462}
463
464#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
466#[non_exhaustive]
467pub struct CleDefinitions {
468 #[serde(default)]
470 pub support: Option<Vec<CleSupportDefinition>>,
471}
472
473#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
475#[serde(rename_all = "camelCase")]
476#[non_exhaustive]
477pub struct CleEvent {
478 pub id: i64,
480 #[serde(rename = "type")]
482 pub event_type: CleEventType,
483 pub effective: TeaTimestamp,
485 pub published: TeaTimestamp,
487 #[serde(default)]
489 pub version: Option<String>,
490 #[serde(default)]
492 pub versions: Option<Vec<CleVersionSpecifier>>,
493 #[serde(default)]
495 pub support_id: Option<String>,
496 #[serde(default)]
498 pub license: Option<String>,
499 #[serde(default)]
501 pub superseded_by_version: Option<String>,
502 #[serde(default)]
504 pub identifiers: Option<Vec<Identifier>>,
505 #[serde(default)]
507 pub event_id: Option<i64>,
508 #[serde(default)]
510 pub reason: Option<String>,
511 #[serde(default)]
513 pub description: Option<String>,
514 #[serde(default)]
516 pub references: Option<Vec<String>>,
517}
518
519#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
521#[non_exhaustive]
522pub struct CleVersionSpecifier {
523 #[serde(default)]
525 pub version: Option<String>,
526 #[serde(default)]
528 pub range: Option<String>,
529}
530
531#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
533#[non_exhaustive]
534pub struct Cle {
535 pub events: Vec<CleEvent>,
537 #[serde(default)]
539 pub definitions: Option<CleDefinitions>,
540}
541
542#[derive(Debug, Clone, PartialEq, Eq)]
544pub struct ComplianceDocument(pub ComplianceDocumentType);
545
546pub fn parse_json<T>(bytes: &[u8]) -> Result<T, ModelError>
552where
553 T: for<'de> Deserialize<'de>,
554{
555 Ok(serde_json::from_slice(bytes)?)
556}
557
558pub fn parse_artifact(bytes: &[u8]) -> Result<Artifact, ModelError> {
564 let artifact: Artifact = parse_json(bytes)?;
565 artifact.validate()?;
566 Ok(artifact)
567}
568
569pub fn parse_collection(bytes: &[u8]) -> Result<Collection, ModelError> {
575 let collection: Collection = parse_json(bytes)?;
576 collection.validate()?;
577 Ok(collection)
578}
579
580pub fn parse_product_release(bytes: &[u8]) -> Result<ProductReleaseWithCollection, ModelError> {
586 let value: ProductReleaseWithCollection = parse_json(bytes)?;
587 value.latest_collection.validate()?;
588 Ok(value)
589}
590
591pub fn parse_component_release(bytes: &[u8]) -> Result<ComponentReleaseWithCollection, ModelError> {
597 let value: ComponentReleaseWithCollection = parse_json(bytes)?;
598 value.latest_collection.validate()?;
599 Ok(value)
600}
601
602#[cfg(test)]
603mod tests {
604 use super::*;
605
606 #[test]
607 fn artifact_with_url_requires_checksum() {
608 let json = br#"{
609 "uuid": "2ff3ae7c-8eab-4e0b-9b8b-1a0c4c4c1c2d",
610 "version": 1,
611 "type": "BOM",
612 "createdDate": "2026-01-02T03:04:05Z",
613 "formats": [{"mediaType": "application/vnd.cyclonedx+json", "url": "https://cdn.example/sbom.json"}]
614 }"#;
615 assert!(parse_artifact(json).is_err());
616 }
617
618 #[test]
619 fn preserves_unknown_artifact_type() {
620 let json = br#"{
621 "uuid": "2ff3ae7c-8eab-4e0b-9b8b-1a0c4c4c1c2d",
622 "version": 1,
623 "type": "FUTURE_DOC",
624 "createdDate": "2026-01-02T03:04:05.5Z",
625 "formats": [{"mediaType": "application/json", "checksums": [{"algType": "SHA-256", "algValue": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}]}]
626 }"#;
627 let artifact = parse_artifact(json).unwrap();
628 assert_eq!(artifact.artifact_type.as_str(), "FUTURE_DOC");
629 }
630}