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
|
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use anyhow::{Context, Result, bail};
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct ImageSpec {
pub target: Target,
pub kernel: Kernel,
pub boot: BootSpec,
pub packages: PackageSpec,
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Target {
pub name: String,
pub architecture: Architecture,
pub format: OutputFormat,
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Kernel {
pub flavour: String,
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct BootSpec {
pub initrd_features: Vec<String>,
pub initrd_modules: Vec<String>,
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct PackageSpec {
pub base: Vec<String>,
pub selectors: Vec<PackageSelector>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageSelector(String);
impl PackageSelector {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl<'de> Deserialize<'de> for PackageSelector {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
if value.trim().is_empty() {
return Err(serde::de::Error::custom("package selector cannot be empty"));
}
Ok(Self(value))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Architecture {
X86_64,
}
impl Architecture {
pub fn as_str(&self) -> &'static str {
match self {
Self::X86_64 => "x86_64",
}
}
}
impl<'de> Deserialize<'de> for Architecture {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
match String::deserialize(deserializer)?.as_str() {
"x86_64" => Ok(Self::X86_64),
value => Err(serde::de::Error::custom(format!(
"unsupported architecture: {value}"
))),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OutputFormat {
Tar,
}
impl<'de> Deserialize<'de> for OutputFormat {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
match String::deserialize(deserializer)?.as_str() {
"tar" => Ok(Self::Tar),
value => Err(serde::de::Error::custom(format!(
"unsupported output format: {value}"
))),
}
}
}
impl ImageSpec {
pub fn load(path: &Path) -> Result<Self> {
let contents = fs::read_to_string(path)
.with_context(|| format!("read image specification {}", path.display()))?;
let spec: Self = toml::from_str(&contents)
.with_context(|| format!("parse image specification {}", path.display()))?;
spec.validate()?;
Ok(spec)
}
pub fn validate(&self) -> Result<()> {
if self.target.name.trim().is_empty() {
bail!("target name cannot be empty");
}
let mut selectors = HashSet::new();
for selector in &self.packages.selectors {
if !selectors.insert(selector.as_str()) {
bail!("duplicate package selector: {}", selector.as_str());
}
}
for module in &self.boot.initrd_modules {
if module.is_empty()
|| module.contains('/')
|| !module.chars().all(|character| {
character.is_ascii_alphanumeric() || matches!(character, '.' | '_' | '-')
})
{
bail!("invalid initrd module: {module}");
}
}
Ok(())
}
}
|