From 5cea5186d767411caaa74b8b1678c7b4a225f9a6 Mon Sep 17 00:00:00 2001 From: Till Wegmueller Date: Wed, 14 Aug 2024 20:02:29 +0200 Subject: [PATCH] Add serde and image starter module Signed-off-by: Till Wegmueller --- Cargo.lock | 35 ++++++++++---------- Cargo.toml | 2 +- libips/Cargo.toml | 2 ++ libips/src/actions/mod.rs | 19 +++++------ libips/src/image/mod.rs | 58 ++++++++++++++++++++++++++++++++++ libips/src/image/properties.rs | 11 +++++++ libips/src/lib.rs | 1 + 7 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 libips/src/image/mod.rs create mode 100644 libips/src/image/properties.rs diff --git a/Cargo.lock b/Cargo.lock index 3c291a5..5a47d7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -608,6 +608,8 @@ dependencies = [ "pest", "pest_derive", "regex", + "serde", + "serde_json", "sha2 0.9.9", "sha3", "strum", @@ -747,7 +749,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.14", + "syn 2.0.74", ] [[package]] @@ -820,7 +822,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.14", + "syn 2.0.74", ] [[package]] @@ -911,18 +913,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.26" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -1127,31 +1129,32 @@ checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" [[package]] name = "serde" -version = "1.0.160" +version = "1.0.207" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" +checksum = "5665e14a49a4ea1b91029ba7d3bca9f299e1f7cfa194388ccc20f14743e784f2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.160" +version = "1.0.207" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" +checksum = "6aea2634c86b0e8ef2cfdc0c340baede54ec27b1e46febd7f80dffb2aa44a00e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.14", + "syn 2.0.74", ] [[package]] name = "serde_json" -version = "1.0.95" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" +checksum = "66ad62847a56b3dba58cc891acd13884b9c61138d330c0d7b6181713d4fce38d" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] @@ -1289,9 +1292,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.14" +version = "2.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcf316d5356ed6847742d036f8a39c3b8435cac10bd528a4bd461928a6ab34d5" +checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7" dependencies = [ "proc-macro2", "quote", @@ -1343,7 +1346,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.14", + "syn 2.0.74", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 5b629f1..3c18226 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,4 @@ members = [ "ports", ] - +resolver = "2" \ No newline at end of file diff --git a/libips/Cargo.toml b/libips/Cargo.toml index 1672183..f33b045 100644 --- a/libips/Cargo.toml +++ b/libips/Cargo.toml @@ -26,3 +26,5 @@ sha3 = "0.9.1" pest = "2.1.3" pest_derive = "2.1.0" strum = { version = "0.24.1", features = ["derive"] } +serde = { version = "1.0.207", features = ["derive"] } +serde_json = "1.0.124" diff --git a/libips/src/actions/mod.rs b/libips/src/actions/mod.rs index 9d1dfa0..def438b 100644 --- a/libips/src/actions/mod.rs +++ b/libips/src/actions/mod.rs @@ -15,6 +15,7 @@ use std::fs::read_to_string; use std::path::Path; use std::result::Result as StdResult; use std::str::FromStr; +use serde::{Deserialize, Serialize}; use thiserror::Error; type Result = StdResult; @@ -76,7 +77,7 @@ impl FacetedAction for Action { } } -#[derive(Debug, Default, PartialEq, Clone)] +#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)] pub struct Dir { pub path: 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 payload: Option, pub path: String, @@ -254,7 +255,7 @@ pub enum FileError { } //TODO implement multiple FMRI for require-any -#[derive(Debug, Default, PartialEq, Clone)] +#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)] pub struct Dependency { pub fmri: String, //TODO make FMRI 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 name: 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 key: String, pub values: Vec, @@ -356,7 +357,7 @@ impl From for Attr { } } -#[derive(Eq, PartialEq, Debug, Default, Clone)] +#[derive(Eq, PartialEq, Debug, Default, Clone, Deserialize, Serialize)] pub struct License { pub payload: String, pub properties: HashMap, @@ -384,7 +385,7 @@ impl From for License { } } -#[derive(Eq, PartialEq, Debug, Default, Clone)] +#[derive(Eq, PartialEq, Debug, Default, Clone, Deserialize, Serialize)] pub struct Link { pub path: String, pub target: String, @@ -421,13 +422,13 @@ impl From for Link { } } -#[derive(Hash, Eq, PartialEq, Debug, Default, Clone)] +#[derive(Hash, Eq, PartialEq, Debug, Default, Clone, Deserialize, Serialize)] pub struct Property { pub key: String, pub value: String, } -#[derive(Debug, Default, PartialEq, Clone)] +#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)] pub struct Manifest { pub attributes: Vec, pub directories: Vec, diff --git a/libips/src/image/mod.rs b/libips/src/image/mod.rs new file mode 100644 index 0000000..ebad6cb --- /dev/null +++ b/libips/src/image/mod.rs @@ -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 = std::result::Result; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Image { + path: PathBuf, + props: Vec, + version: i32, + variants: HashMap, + mediators: HashMap, +} + +impl Image { + pub fn new>(path: P) -> Image { + Image{ + path: path.into(), + version: 5, + variants: HashMap::new(), + mediators: HashMap::new(), + props: vec![], + } + } + + pub fn open>(path: P) -> Result { + 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>(path: P) -> Image { + if let Ok(img) = Image::open(path.as_ref()) { + img + } else { + Image::new(path.as_ref()) + } + } +} \ No newline at end of file diff --git a/libips/src/image/properties.rs b/libips/src/image/properties.rs new file mode 100644 index 0000000..a3310ff --- /dev/null +++ b/libips/src/image/properties.rs @@ -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), + Integer(i32), +} \ No newline at end of file diff --git a/libips/src/lib.rs b/libips/src/lib.rs index 8e9e9ae..4e4bc10 100644 --- a/libips/src/lib.rs +++ b/libips/src/lib.rs @@ -7,6 +7,7 @@ pub mod actions; pub mod digest; pub mod payload; +pub mod image; #[cfg(test)] mod tests {