-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is the first step in having HTTP based API for storage. As it is really big API we decided to split it into series of patches for easier review.
- Loading branch information
Showing
10 changed files
with
569 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,48 @@ | ||
use anyhow::Context; | ||
use std::collections::HashMap; | ||
use zbus::zvariant; | ||
use zbus::zvariant::{self, OwnedValue, Value}; | ||
|
||
use crate::error::ServiceError; | ||
|
||
/// Nested hash to send to D-Bus. | ||
pub type NestedHash<'a> = HashMap<&'a str, HashMap<&'a str, zvariant::Value<'a>>>; | ||
/// Nested hash as it comes from D-Bus. | ||
pub type OwnedNestedHash = HashMap<String, HashMap<String, zvariant::OwnedValue>>; | ||
|
||
/// Helper to get property of given type from ManagedObjects map or any generic D-Bus Hash with variant as value | ||
pub fn get_property<'a, T>( | ||
properties: &'a HashMap<String, OwnedValue>, | ||
name: &str, | ||
) -> Result<T, zbus::zvariant::Error> | ||
where | ||
T: TryFrom<Value<'a>>, | ||
<T as TryFrom<Value<'a>>>::Error: Into<zbus::zvariant::Error>, | ||
{ | ||
let value: Value = properties | ||
.get(name) | ||
.ok_or(zbus::zvariant::Error::Message(format!( | ||
"Failed to find property '{}'", | ||
name | ||
)))? | ||
.into(); | ||
|
||
T::try_from(value).map_err(|e| e.into()) | ||
} | ||
|
||
/// It is similar helper like get_property with difference that name does not need to be in HashMap. | ||
/// In such case `None` is returned, so type has to be enclosed in `Option`. | ||
pub fn get_optional_property<'a, T>( | ||
properties: &'a HashMap<String, OwnedValue>, | ||
name: &str, | ||
) -> Result<Option<T>, zbus::zvariant::Error> | ||
where | ||
T: TryFrom<Value<'a>>, | ||
<T as TryFrom<Value<'a>>>::Error: Into<zbus::zvariant::Error>, | ||
{ | ||
if let Some(value) = properties.get(name) { | ||
let value: Value = value.into(); | ||
T::try_from(value).map(|v| Some(v)).map_err(|e| e.into()) | ||
} else { | ||
Ok(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.