use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result as FmtResult;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ODataId(String);
impl ODataId {
#[must_use]
pub fn service_root() -> Self {
Self("/redfish/v1".into())
}
}
impl From<String> for ODataId {
fn from(s: String) -> Self {
Self(s)
}
}
impl Display for ODataId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
self.0.fmt(f)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct ODataETag(String);
impl From<String> for ODataETag {
fn from(value: String) -> Self {
Self(value)
}
}
impl Display for ODataETag {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
self.0.fmt(f)
}
}
pub struct ODataType<'a> {
pub namespace: Vec<&'a str>,
pub type_name: &'a str,
}
impl ODataType<'_> {
#[must_use]
pub fn parse_from(v: &serde_json::Value) -> Option<ODataType<'_>> {
v.get("@odata.type")
.and_then(|v| v.as_str())
.and_then(|v| v.starts_with('#').then_some(&v[1..]))
.and_then(|v| {
let mut all = v.split('.').collect::<Vec<_>>();
all.pop().map(|type_name| ODataType {
namespace: all,
type_name,
})
})
}
}