Add serde and image starter module

Signed-off-by: Till Wegmueller <toasterson@gmail.com>
This commit is contained in:
Till Wegmueller 2024-08-14 20:02:29 +02:00
parent 457111fc57
commit 5cea5186d7
No known key found for this signature in database
7 changed files with 102 additions and 26 deletions

35
Cargo.lock generated
View file

@ -608,6 +608,8 @@ dependencies = [
"pest", "pest",
"pest_derive", "pest_derive",
"regex", "regex",
"serde",
"serde_json",
"sha2 0.9.9", "sha2 0.9.9",
"sha3", "sha3",
"strum", "strum",
@ -747,7 +749,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.14", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -820,7 +822,7 @@ dependencies = [
"pest_meta", "pest_meta",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.14", "syn 2.0.74",
] ]
[[package]] [[package]]
@ -911,18 +913,18 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.56" version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.26" version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -1127,31 +1129,32 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.160" version = "1.0.207"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" checksum = "5665e14a49a4ea1b91029ba7d3bca9f299e1f7cfa194388ccc20f14743e784f2"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.160" version = "1.0.207"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" checksum = "6aea2634c86b0e8ef2cfdc0c340baede54ec27b1e46febd7f80dffb2aa44a00e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.14", "syn 2.0.74",
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.95" version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" checksum = "66ad62847a56b3dba58cc891acd13884b9c61138d330c0d7b6181713d4fce38d"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr",
"ryu", "ryu",
"serde", "serde",
] ]
@ -1289,9 +1292,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.14" version = "2.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcf316d5356ed6847742d036f8a39c3b8435cac10bd528a4bd461928a6ab34d5" checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1343,7 +1346,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.14", "syn 2.0.74",
] ]
[[package]] [[package]]

View file

@ -10,4 +10,4 @@ members = [
"ports", "ports",
] ]
resolver = "2"

View file

@ -26,3 +26,5 @@ sha3 = "0.9.1"
pest = "2.1.3" pest = "2.1.3"
pest_derive = "2.1.0" pest_derive = "2.1.0"
strum = { version = "0.24.1", features = ["derive"] } strum = { version = "0.24.1", features = ["derive"] }
serde = { version = "1.0.207", features = ["derive"] }
serde_json = "1.0.124"

View file

@ -15,6 +15,7 @@ use std::fs::read_to_string;
use std::path::Path; use std::path::Path;
use std::result::Result as StdResult; use std::result::Result as StdResult;
use std::str::FromStr; use std::str::FromStr;
use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
type Result<T> = StdResult<T, ActionError>; type Result<T> = StdResult<T, ActionError>;
@ -76,7 +77,7 @@ impl FacetedAction for Action {
} }
} }
#[derive(Debug, Default, PartialEq, Clone)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
pub struct Dir { pub struct Dir {
pub path: String, pub path: String,
pub group: String, pub group: String,
@ -127,7 +128,7 @@ impl FacetedAction for Dir {
} }
} }
#[derive(Debug, Default, PartialEq, Clone)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
pub struct File { pub struct File {
pub payload: Option<Payload>, pub payload: Option<Payload>,
pub path: String, pub path: String,
@ -254,7 +255,7 @@ pub enum FileError {
} }
//TODO implement multiple FMRI for require-any //TODO implement multiple FMRI for require-any
#[derive(Debug, Default, PartialEq, Clone)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
pub struct Dependency { pub struct Dependency {
pub fmri: String, //TODO make FMRI pub fmri: String, //TODO make FMRI
pub dependency_type: String, //TODO make enum pub dependency_type: String, //TODO make enum
@ -304,7 +305,7 @@ impl FacetedAction for Dependency {
} }
} }
#[derive(Hash, Eq, PartialEq, Debug, Default, Clone)] #[derive(Hash, Eq, PartialEq, Debug, Default, Clone, Deserialize, Serialize)]
pub struct Facet { pub struct Facet {
pub name: String, pub name: String,
pub value: String, pub value: String,
@ -319,7 +320,7 @@ impl Facet {
} }
} }
#[derive(Debug, Default, PartialEq, Clone)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
pub struct Attr { pub struct Attr {
pub key: String, pub key: String,
pub values: Vec<String>, pub values: Vec<String>,
@ -356,7 +357,7 @@ impl From<Action> for Attr {
} }
} }
#[derive(Eq, PartialEq, Debug, Default, Clone)] #[derive(Eq, PartialEq, Debug, Default, Clone, Deserialize, Serialize)]
pub struct License { pub struct License {
pub payload: String, pub payload: String,
pub properties: HashMap<String, Property>, pub properties: HashMap<String, Property>,
@ -384,7 +385,7 @@ impl From<Action> for License {
} }
} }
#[derive(Eq, PartialEq, Debug, Default, Clone)] #[derive(Eq, PartialEq, Debug, Default, Clone, Deserialize, Serialize)]
pub struct Link { pub struct Link {
pub path: String, pub path: String,
pub target: String, pub target: String,
@ -421,13 +422,13 @@ impl From<Action> for Link {
} }
} }
#[derive(Hash, Eq, PartialEq, Debug, Default, Clone)] #[derive(Hash, Eq, PartialEq, Debug, Default, Clone, Deserialize, Serialize)]
pub struct Property { pub struct Property {
pub key: String, pub key: String,
pub value: String, pub value: String,
} }
#[derive(Debug, Default, PartialEq, Clone)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
pub struct Manifest { pub struct Manifest {
pub attributes: Vec<Attr>, pub attributes: Vec<Attr>,
pub directories: Vec<Dir>, pub directories: Vec<Dir>,

58
libips/src/image/mod.rs Normal file
View file

@ -0,0 +1,58 @@
mod properties;
use std::collections::HashMap;
use std::fs::File;
use properties::*;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ImageError {
//Implement derives for IO error and serde_json error
#[error(transparent)]
IO(#[from] std::io::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
}
pub type Result<T> = std::result::Result<T, ImageError>;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Image {
path: PathBuf,
props: Vec<ImageProperty>,
version: i32,
variants: HashMap<String, String>,
mediators: HashMap<String, String>,
}
impl Image {
pub fn new<P: Into<PathBuf>>(path: P) -> Image {
Image{
path: path.into(),
version: 5,
variants: HashMap::new(),
mediators: HashMap::new(),
props: vec![],
}
}
pub fn open<P: AsRef<Path>>(path: P) -> Result<Image> {
let path = path.as_ref();
//TODO: Parse the old INI format of pkg5
//TODO once root images are implemented, look for metadata under sub directory var/pkg
let props_path = path.join("pkg6.image.json");
let mut f = File::open(props_path)?;
Ok(serde_json::from_reader(&mut f)?)
}
pub fn open_default<P: AsRef<Path>>(path: P) -> Image {
if let Ok(img) = Image::open(path.as_ref()) {
img
} else {
Image::new(path.as_ref())
}
}
}

View file

@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Default, Deserialize, Serialize)]
pub enum ImageProperty {
String(String),
Boolean(bool),
#[default]
None,
Array(Vec<ImageProperty>),
Integer(i32),
}

View file

@ -7,6 +7,7 @@
pub mod actions; pub mod actions;
pub mod digest; pub mod digest;
pub mod payload; pub mod payload;
pub mod image;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {