11use super :: Document ;
2- use anyhow:: { anyhow, bail} ;
3- use bytes:: BytesMut ;
4- use futures_util:: TryStreamExt ;
2+ use bytes:: Bytes ;
53use sea_orm:: prelude:: * ;
6- use trustify_common:: id:: Id ;
7- use trustify_entity:: { sbom, source_document} ;
8- use trustify_module_storage:: service:: { StorageBackend , StorageKey } ;
4+ use trustify_entity:: sbom;
5+ use trustify_module_storage:: service:: StorageBackend ;
96
107#[ allow( clippy:: large_enum_variant) ]
118pub enum Sbom {
129 CycloneDx ( serde_cyclonedx:: cyclonedx:: v_1_6:: CycloneDx ) ,
1310 Spdx ( spdx_rs:: models:: SPDX ) ,
11+ Other ( Bytes ) ,
12+ }
13+
14+ impl From < Bytes > for Sbom {
15+ fn from ( value : Bytes ) -> Self {
16+ serde_json:: from_slice ( & value)
17+ . map ( Sbom :: Spdx )
18+ . or_else ( |_| serde_json:: from_slice ( & value) . map ( Sbom :: CycloneDx ) )
19+ . unwrap_or_else ( |_err| Sbom :: Other ( value) )
20+ }
1421}
1522
1623impl Document for Sbom {
@@ -25,31 +32,6 @@ impl Document for Sbom {
2532 S : StorageBackend + Send + Sync ,
2633 C : ConnectionTrait ,
2734 {
28- let source = model. find_related ( source_document:: Entity ) . one ( tx) . await ?;
29-
30- let Some ( source) = source else {
31- bail ! ( "Missing source document ID for SBOM: {}" , model. sbom_id) ;
32- } ;
33-
34- let stream = storage
35- . retrieve (
36- StorageKey :: try_from ( Id :: Sha256 ( source. sha256 ) )
37- . map_err ( |err| anyhow ! ( "Invalid ID: {err}" ) ) ?,
38- )
39- . await
40- . map_err ( |err| anyhow ! ( "Failed to retrieve document: {err}" ) ) ?
41- . ok_or_else ( || anyhow ! ( "Missing source document for SBOM: {}" , model. sbom_id) ) ?;
42-
43- stream
44- . try_collect :: < BytesMut > ( )
45- . await
46- . map_err ( |err| anyhow ! ( "Failed to collect bytes: {err}" ) )
47- . map ( |bytes| bytes. freeze ( ) )
48- . and_then ( |bytes| {
49- serde_json:: from_slice ( & bytes)
50- . map ( Sbom :: Spdx )
51- . or_else ( |_| serde_json:: from_slice ( & bytes) . map ( Sbom :: CycloneDx ) )
52- . map_err ( |err| anyhow ! ( "Failed to parse document: {err}" ) )
53- } )
35+ super :: load ( model. source_document_id , storage, tx) . await
5436 }
5537}
0 commit comments