1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::collections::HashMap;
pub const WASM_LAYER_MEDIA_TYPE: &str = "application/vnd.wasm.content.layer.v1+wasm";
pub const WASM_CONFIG_MEDIA_TYPE: &str = "application/vnd.wasm.config.v1+json";
pub const IMAGE_MANIFEST_MEDIA_TYPE: &str = "application/vnd.docker.distribution.manifest.v2+json";
pub const IMAGE_CONFIG_MEDIA_TYPE: &str = "application/vnd.oci.image.config.v1+json";
pub const IMAGE_DOCKER_CONFIG_MEDIA_TYPE: &str = "application/vnd.docker.container.image.v1+json";
pub const IMAGE_LAYER_MEDIA_TYPE: &str = "application/vnd.oci.image.layer.v1.tar";
pub const IMAGE_LAYER_GZIP_MEDIA_TYPE: &str = "application/vnd.oci.image.layer.v1.tar+gzip";
pub const IMAGE_LAYER_NONDISTRIBUTABLE_MEDIA_TYPE: &str =
"application/vnd.oci.image.layer.nondistributable.v1.tar";
pub const IMAGE_LAYER_NONDISTRIBUTABLE_GZIP_MEDIA_TYPE: &str =
"application/vnd.oci.image.layer.nondistributable.v1.tar+gzip";
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OciManifest {
pub schema_version: u8,
pub media_type: Option<String>,
pub config: OciDescriptor,
pub layers: Vec<OciDescriptor>,
pub annotations: Option<HashMap<String, String>>,
}
impl Default for OciManifest {
fn default() -> Self {
OciManifest {
schema_version: 2,
media_type: None,
config: OciDescriptor::default(),
layers: vec![],
annotations: None,
}
}
}
#[derive(Clone, Debug, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Versioned {
pub schema_version: i32,
pub media_type: Option<String>,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OciDescriptor {
pub media_type: String,
pub digest: String,
pub size: i64,
pub urls: Option<Vec<String>>,
pub annotations: Option<HashMap<String, String>>,
}
impl Default for OciDescriptor {
fn default() -> Self {
OciDescriptor {
media_type: IMAGE_CONFIG_MEDIA_TYPE.to_owned(),
digest: "".to_owned(),
size: 0,
urls: None,
annotations: None,
}
}
}
#[cfg(test)]
mod test {
use super::*;
const TEST_MANIFEST: &str = r#"{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 2,
"digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a"
},
"layers": [
{
"mediaType": "application/vnd.wasm.content.layer.v1+wasm",
"size": 1615998,
"digest": "sha256:f9c91f4c280ab92aff9eb03b279c4774a80b84428741ab20855d32004b2b983f",
"annotations": {
"org.opencontainers.image.title": "module.wasm"
}
}
]
}
"#;
#[test]
fn test_manifest() {
let manifest: OciManifest = serde_json::from_str(TEST_MANIFEST).expect("parsed manifest");
assert_eq!(2, manifest.schema_version);
assert_eq!(
Some(IMAGE_MANIFEST_MEDIA_TYPE.to_owned()),
manifest.media_type
);
let config = manifest.config;
assert_eq!(IMAGE_DOCKER_CONFIG_MEDIA_TYPE.to_owned(), config.media_type);
assert_eq!(2, config.size);
assert_eq!(
"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a".to_owned(),
config.digest
);
assert_eq!(1, manifest.layers.len());
let wasm_layer = &manifest.layers[0];
assert_eq!(1_615_998, wasm_layer.size);
assert_eq!(WASM_LAYER_MEDIA_TYPE.to_owned(), wasm_layer.media_type);
assert_eq!(
1,
wasm_layer
.annotations
.as_ref()
.expect("annotations map")
.len()
);
}
}