Skip to main content

harmos_packaging/
lib.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// Reusable Cargo Build, Inert Inspection And Guest Componentization.
11
12/* === Modules === */
13
14mod build;
15pub mod bundle;
16#[cfg(feature = "guest")]
17mod component;
18pub mod config;
19mod dev;
20mod error;
21mod metadata;
22mod process;
23#[cfg(feature = "generate-proto")]
24pub mod proto;
25#[cfg(feature = "generate-python")]
26pub mod python;
27
28/* === Re-Exports === */
29
30pub use build::{Artifact, Build, PackageOutput, Schema, build, build_with_cancellation, package, package_with_cancellation};
31#[cfg(feature = "guest")]
32pub use component::componentize;
33pub use dev::{DevEvent, dev};
34pub use error::Error;
35pub use harmos_domain::inventory::{Declaration, DeclarationError, Facility};
36pub use process::BuildCancellation;
37
38/* === Imports === */
39
40// Standard Library Imports
41use std::path::Path;
42
43// Third Party Imports
44use sha2::{Digest, Sha256};
45
46// Local Imports
47
48/* === Constants === */
49
50/* === Macros === */
51
52/* === Type Aliases === */
53
54/* === Structs === */
55
56#[derive(Debug, serde::Serialize)]
57pub struct Inspection {
58    pub id: String,
59    pub version: String,
60    pub description: String,
61    pub config: String,
62    pub operations: Vec<String>,
63    pub sha256: String,
64    pub facility: &'static str,
65}
66
67/* === Enums === */
68
69/* === Traits === */
70
71/* === Implementations === */
72
73/* === Public Functions === */
74
75pub fn inspect(path: &Path) -> Result<Inspection, Error> {
76    let bytes = std::fs::read(path)?;
77    let guest = bytes.starts_with(b"\0asm");
78    let facility = if guest { Facility::Guest } else { Facility::Sidecar };
79    let declaration = Declaration::inspect(&bytes, facility)?;
80
81    let mut operations = Vec::with_capacity(declaration.operations.len());
82    for operation in &declaration.operations {
83        operations.push(operation.id.to_string());
84    }
85
86    Ok(Inspection {
87        id: declaration.id,
88        version: declaration.version.to_string(),
89        description: declaration.description,
90        config: declaration.config,
91        operations,
92        sha256: digest(&bytes),
93        facility: if guest { "guest" } else { "sidecar" },
94    })
95}
96
97pub fn digest(bytes: &[u8]) -> String {
98    const HEX: &[u8; 16] = b"0123456789abcdef";
99    let hash = Sha256::digest(bytes);
100    let mut encoded = String::with_capacity(hash.len() * 2);
101    for byte in hash {
102        let high = char::from(HEX[usize::from(byte >> 4)]);
103        let low = char::from(HEX[usize::from(byte & 0x0f)]);
104        encoded.push(high);
105        encoded.push(low);
106    }
107    encoded
108}
109
110/* === Private Functions === */
111
112/* === Tests === */