From 2ab55beb988eb2555ebe15adebe536d7d292b109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20St=C3=BChn?= Date: Tue, 16 Nov 2021 12:10:50 +0100 Subject: [PATCH] add ObjectDetails-struct and use it in list_objects-function --- rust-bindings/rust/src/lib.rs | 2 + rust-bindings/rust/src/object_details.rs | 48 ++++++++++++++++++++++++ rust-bindings/rust/src/repo.rs | 14 +++---- 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 rust-bindings/rust/src/object_details.rs diff --git a/rust-bindings/rust/src/lib.rs b/rust-bindings/rust/src/lib.rs index 5bce2adb..ae2debb5 100644 --- a/rust-bindings/rust/src/lib.rs +++ b/rust-bindings/rust/src/lib.rs @@ -48,6 +48,8 @@ mod kernel_args; pub use crate::kernel_args::*; mod object_name; pub use crate::object_name::*; +mod object_details; +pub use crate::object_details::*; mod repo; pub use crate::repo::*; #[cfg(any(feature = "v2016_8", feature = "dox"))] diff --git a/rust-bindings/rust/src/object_details.rs b/rust-bindings/rust/src/object_details.rs new file mode 100644 index 00000000..12a580cb --- /dev/null +++ b/rust-bindings/rust/src/object_details.rs @@ -0,0 +1,48 @@ +use glib; +use std::fmt::Display; +use std::fmt::Formatter; +use std::fmt::Error; + +/// Details of an object in an OSTree repo. It contains information about if +/// the object is "loose", and contains a list of pack file checksums in which +/// this object appears. +#[derive(Debug)] +pub struct ObjectDetails { + loose: bool, + object_appearances: Vec, +} + +impl ObjectDetails { + /// Create a new `ObjectDetails` from a serialized representation. + pub fn new_from_variant(variant: glib::Variant) -> Option { + let deserialize = variant.get::<(bool, Vec)>()?; + Some(ObjectDetails { + loose: deserialize.0, + object_appearances: deserialize.1, + }) + } + + /// is object available "loose" + pub fn is_loose(&self) -> bool { + self.loose + } + + /// Provide list of pack file checksums in which the object appears + pub fn appearances(&self) -> &Vec { + &self.object_appearances + } + + /// Format this `ObjectDetails` as a string. + fn to_string(&self) -> String { + format!("Object is {} loose and appears in {} checksums", + if self.loose {"available"} else {"not available"}, + self.object_appearances.len() ) + } +} + +impl Display for ObjectDetails{ + fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { + write!(f, "{}", self.to_string()) + } +} + diff --git a/rust-bindings/rust/src/repo.rs b/rust-bindings/rust/src/repo.rs index f44a6c2d..edec8dc5 100644 --- a/rust-bindings/rust/src/repo.rs +++ b/rust-bindings/rust/src/repo.rs @@ -1,6 +1,6 @@ #[cfg(any(feature = "v2016_4", feature = "dox"))] use crate::RepoListRefsExtFlags; -use crate::{Checksum, ObjectName, ObjectType, Repo, RepoTransactionStats}; +use crate::{Checksum, ObjectName, ObjectDetails, ObjectType, Repo, RepoTransactionStats}; use ffi; use ffi::OstreeRepoListObjectsFlags; use glib::ffi as glib_sys; @@ -31,9 +31,9 @@ unsafe extern "C" fn read_variant_object_map( ) { let key: glib::Variant = from_glib_none(key as *const glib_sys::GVariant); let value: glib::Variant = from_glib_none(value as *const glib_sys::GVariant); - if let Some(insert) = value.get::<(bool, Vec)>() { - let set: &mut HashMap)> = &mut *(hash_set as *mut HashMap)>); - set.insert(ObjectName::new_from_variant(key), insert); + let set: &mut HashMap = &mut *(hash_set as *mut HashMap); + if let Some(details) = ObjectDetails::new_from_variant(value) { + set.insert(ObjectName::new_from_variant(key), details); } } @@ -48,12 +48,12 @@ unsafe fn from_glib_container_variant_set(ptr: *mut glib_sys::GHashTable) -> Has set } -unsafe fn from_glib_container_variant_map(ptr: *mut glib_sys::GHashTable) -> HashMap)> { +unsafe fn from_glib_container_variant_map(ptr: *mut glib_sys::GHashTable) -> HashMap { let mut set = HashMap::new(); glib_sys::g_hash_table_foreach( ptr, Some(read_variant_object_map), - &mut set as *mut HashMap)> as *mut _, + &mut set as *mut HashMap as *mut _, ); glib_sys::g_hash_table_unref(ptr); set @@ -177,7 +177,7 @@ impl Repo { &self, flags: OstreeRepoListObjectsFlags, cancellable: Option<&P>, - ) -> Result)>, Error> { + ) -> Result, Error> { unsafe { let mut error = ptr::null_mut(); let mut hashtable = ptr::null_mut();