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
|
//! The spec: populate's arguments as JSON, as docs/design.md describes them.
// Delivering sources isn't implemented yet, so most of the spec is unused.
#![allow(dead_code)]
use serde::Deserialize;
use serde_json::{Map, Value};
use std::collections::BTreeMap;
/// The version of the spec this populate reads.
pub const VERSION: u64 = 1;
/// The source types populate knows.
pub const SOURCE_TYPES: &[&str] = &[
"derivation",
"file",
"flake",
"flake-input",
"git",
"pass",
"passage",
"pipe",
"symlink",
];
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Spec {
pub version: u64,
#[serde(default)]
pub backup: bool,
#[serde(default)]
pub force: bool,
#[serde(default)]
pub prune: bool,
pub source: BTreeMap<String, Source>,
pub target: Target,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Target {
#[serde(default)]
pub user: Option<String>,
pub host: String,
#[serde(default)]
pub port: Option<String>,
pub path: String,
#[serde(default)]
pub sudo: bool,
#[serde(default)]
pub extra_options: Vec<String>,
}
/// A source: its type, and the settings of that type, under the type's name.
#[derive(Debug)]
pub struct Source {
pub kind: String,
pub settings: Map<String, Value>,
}
impl<'de> Deserialize<'de> for Source {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::Error;
let mut object = Map::deserialize(deserializer)?;
let kind = match object.remove("type") {
Some(Value::String(kind)) => kind,
Some(_) => return Err(D::Error::custom("type must be a string")),
None => return Err(D::Error::missing_field("type")),
};
if !SOURCE_TYPES.contains(&kind.as_str()) {
return Err(D::Error::custom(format!("unknown source type {kind:?}")));
}
let settings = match object.remove(&kind) {
Some(Value::Object(settings)) => settings,
Some(_) => return Err(D::Error::custom(format!("{kind} must be an object"))),
None => return Err(D::Error::missing_field("settings named after the type")),
};
if let Some(other) = object.keys().next() {
return Err(D::Error::custom(format!(
"unknown field {other:?} of a {kind} source"
)));
}
Ok(Source { kind, settings })
}
}
/// An input drawn from another source: the path of input names of the flake
/// in the source drawing it, and the name of the source it's drawn from.
#[derive(Debug, PartialEq)]
pub struct Follows {
pub path: Vec<String>,
pub source: String,
}
impl Source {
/// The inputs this source draws from other sources, like krops' lib's
/// flakeFollows.
pub fn follows(&self) -> Vec<Follows> {
fn walk(prefix: &[String], inputs: &Map<String, Value>, out: &mut Vec<Follows>) {
for (name, input) in inputs {
let mut path = prefix.to_vec();
path.push(name.clone());
if let Some(Value::String(source)) = input.get("follows") {
out.push(Follows {
path: path.clone(),
source: source.clone(),
});
}
if let Some(Value::Object(inputs)) = input.get("inputs") {
walk(&path, inputs, out);
}
}
}
let mut out = Vec::new();
if let Some(Value::Object(inputs)) = self.settings.get("inputs") {
walk(&[], inputs, &mut out);
}
out
}
}
|