iron_os_packages_api/
packages.rs

1use std::str::FromStr;
2use std::{fmt, mem};
3
4pub use crypto::signature::{PublicKey, Signature};
5pub use crypto::hash::Hash;
6
7use serde::{Serialize, Deserialize};
8use serde::de::{value, IntoDeserializer};
9
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub enum Channel {
13	Debug,
14	Alpha,
15	Beta,
16	Release
17}
18
19impl FromStr for Channel {
20	type Err = value::Error;
21
22	fn from_str(s: &str) -> Result<Self, Self::Err> {
23		Self::deserialize(s.into_deserializer())
24	}
25}
26
27impl fmt::Display for Channel {
28	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29		self.serialize(f)
30	}
31}
32
33impl Channel {
34	pub fn is_debug(&self) -> bool {
35		matches!(self, Self::Debug)
36	}
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
40#[serde(rename_all = "camelCase")]
41pub struct Source {
42	/// example packages.lvgd.ch:9281
43	pub addr: String,
44	/// if public == false an authentication token is sent?
45	pub public: bool,
46	/// the connection signature key
47	pub public_key: PublicKey,
48	/// the package signature key
49	pub sign_key: PublicKey
50
51	// todo add whitelist that only specific packages can be fetched
52	// from this source
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(rename_all = "camelCase")]
57pub struct PackagesCfg {
58	/// sources to fetch for updates
59	/// updates are checked in reverse order
60	/// until some source is found that contains that package
61	pub sources: Vec<Source>,
62	/// if this is true that last source will return realtime updates
63	///
64	/// Todo
65	pub fetch_realtime: bool,
66	/// the package that should be run normally
67	pub on_run: String,
68	/// this information get's overriden if the image is in Debug
69	pub channel: Channel
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
73pub enum TargetArch {
74	/// should only be used for example for web only
75	/// packages, or script only
76	Any,
77	Amd64,
78	Arm64
79}
80
81impl From<BoardArch> for TargetArch {
82	fn from(b: BoardArch) -> Self {
83		match b {
84			BoardArch::Amd64 => TargetArch::Amd64,
85			BoardArch::Arm64 => TargetArch::Arm64
86		}
87	}
88}
89
90impl FromStr for TargetArch {
91	type Err = value::Error;
92
93	fn from_str(s: &str) -> Result<Self, Self::Err> {
94		Self::deserialize(s.into_deserializer())
95	}
96}
97
98impl fmt::Display for TargetArch {
99	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100		self.serialize(f)
101	}
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
105pub enum BoardArch {
106	Amd64,
107	Arm64
108}
109
110impl FromStr for BoardArch {
111	type Err = value::Error;
112
113	fn from_str(s: &str) -> Result<Self, Self::Err> {
114		Self::deserialize(s.into_deserializer())
115	}
116}
117
118impl fmt::Display for BoardArch {
119	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120		self.serialize(f)
121	}
122}
123
124#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase")]
126pub struct Package {
127	pub name: String,
128	pub version_str: String,
129	/// blake2s hash of the full compressed file
130	pub version: Hash,
131	pub signature: Signature,
132	pub arch: TargetArch,
133	pub binary: Option<String>
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub enum PackageCfg {
138	// do i need to other package??
139	Left(Package),
140	Right(Package)
141}
142
143impl PackageCfg {
144	pub fn package(&self) -> &Package {
145		match self {
146			Self::Left(p) => p,
147			Self::Right(p) => p
148		}
149	}
150
151	/// Returns the current side of the package
152	pub fn current(&self) -> &'static str {
153		match self {
154			Self::Left(_) => "left",
155			Self::Right(_) => "right"
156		}
157	}
158
159	/// returns the other side of the package
160	pub fn other(&self) -> &'static str {
161		match self {
162			Self::Left(_) => "right",
163			Self::Right(_) => "left"
164		}
165	}
166
167	/// Switches the side of the current package and stores it there
168	pub fn switch(&mut self, new: Package) {
169		let new = match self {
170			Self::Left(_) => PackageCfg::Right(new),
171			Self::Right(_) => PackageCfg::Left(new)
172		};
173		let _ = mem::replace(self, new);
174	}
175}