Skip to content

Commit

Permalink
Implement in local trait
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Jun 28, 2024
1 parent 5f33d07 commit 9836cd1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/oci-tar-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
clap = { version = "4.5.7", features = ["derive"] }
indexmap = "2.2.6"
oci-wasm = { path = "../../../rust-oci-wasm/" }
oci-wasm = "0.0.4"
tokio = { version = "1.38.0", features = [ "full" ] }

[lib]
Expand Down
33 changes: 29 additions & 4 deletions crates/oci-tar-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ use oci_spec::image::{
use oci_wasm::{WasmConfig, WASM_ARCHITECTURE};
use serde::Serialize;
use sha256::{digest, try_digest};
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Builder<C: OciConfig> {
configs: Vec<(C, String, MediaType)>,
layers: Vec<(PathBuf, String)>,
}

pub trait OciConfig: ToString {
pub trait OciConfig {
fn os(&self) -> String;
fn architecture(&self) -> String;
fn layers(&self) -> Vec<String>;
fn to_string(&self) -> String;
}

impl OciConfig for ImageConfiguration {
Expand All @@ -37,6 +38,10 @@ impl OciConfig for ImageConfiguration {
fn layers(&self) -> Vec<String> {
self.rootfs().diff_ids().to_vec()
}

fn to_string(&self) -> String {
self.to_string_pretty().unwrap()
}
}

impl OciConfig for WasmConfig {
Expand All @@ -51,6 +56,28 @@ impl OciConfig for WasmConfig {
fn layers(&self) -> Vec<String> {
self.layer_digests.clone()
}

fn to_string(&self) -> String {
serde_json::to_string_pretty(self).unwrap()
}
}

impl Default for Builder<WasmConfig> {
fn default() -> Self {
Self {
configs: Vec::new(),
layers: Vec::new(),
}
}
}

impl Default for Builder<ImageConfiguration> {
fn default() -> Self {
Self {
configs: Vec::new(),
layers: Vec::new(),
}
}
}

#[derive(Serialize, Debug)]
Expand Down Expand Up @@ -79,8 +106,6 @@ struct DockerManifest {

pub const WASM_LAYER_MEDIA_TYPE: &str =
"application/vnd.bytecodealliance.wasm.component.layer.v0+wasm";
pub const WASM_ARTIFACT_LAYER: &str = "application/wasm";
pub const WASM_ARTIFACT_TYPE: &str = "application/vnd.wasm.config.v0+json";

impl<C: OciConfig> Builder<C> {
pub fn add_config(&mut self, config: C, name: String, media_type: MediaType) -> &mut Self {
Expand Down
1 change: 1 addition & 0 deletions crates/wasi-demo-app/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn main() {
builder.add_config(
img,
"ghcr.io/containerd/runwasi/wasi-demo-app:latest".to_string(),
spec::MediaType::ImageConfig,
);

let f = File::create(&p).unwrap();
Expand Down

0 comments on commit 9836cd1

Please sign in to comment.