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
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::values::ValuesMap;
pub type LockedMap<T> = std::collections::BTreeMap<String, T>;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LockedApp {
pub spin_lock_version: FixedVersion<0>,
#[serde(default, skip_serializing_if = "ValuesMap::is_empty")]
pub metadata: ValuesMap,
#[serde(default, skip_serializing_if = "LockedMap::is_empty")]
pub variables: LockedMap<Variable>,
pub triggers: Vec<LockedTrigger>,
pub components: Vec<LockedComponent>,
}
impl LockedApp {
pub fn from_json(contents: &[u8]) -> serde_json::Result<Self> {
serde_json::from_slice(contents)
}
pub fn to_json(&self) -> serde_json::Result<Vec<u8>> {
serde_json::to_vec_pretty(&self)
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LockedComponent {
pub id: String,
#[serde(default, skip_serializing_if = "ValuesMap::is_empty")]
pub metadata: ValuesMap,
pub source: LockedComponentSource,
#[serde(default, skip_serializing_if = "LockedMap::is_empty")]
pub env: LockedMap<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub files: Vec<ContentPath>,
#[serde(default, skip_serializing_if = "LockedMap::is_empty")]
pub config: LockedMap<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LockedComponentSource {
pub content_type: String,
#[serde(flatten)]
pub content: ContentRef,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ContentPath {
#[serde(flatten)]
pub content: ContentRef,
pub path: PathBuf,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ContentRef {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub digest: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LockedTrigger {
pub id: String,
pub trigger_type: String,
pub trigger_config: Value,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Variable {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub secret: bool,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(into = "usize", try_from = "usize")]
pub struct FixedVersion<const V: usize>;
impl<const V: usize> From<FixedVersion<V>> for usize {
fn from(_: FixedVersion<V>) -> usize {
V
}
}
impl<const V: usize> From<FixedVersion<V>> for String {
fn from(_: FixedVersion<V>) -> String {
V.to_string()
}
}
impl<const V: usize> TryFrom<usize> for FixedVersion<V> {
type Error = String;
fn try_from(value: usize) -> Result<Self, Self::Error> {
if value != V {
return Err(format!("invalid version {} != {}", value, V));
}
Ok(Self)
}
}
impl<const V: usize> TryFrom<String> for FixedVersion<V> {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
let value: usize = value
.parse()
.map_err(|err| format!("invalid version: {}", err))?;
value.try_into()
}
}