use crate::core::EntityTypeRef as _;
use crate::core::ODataId;
use crate::ResourceSchema;
use tagged_types::TaggedType;
#[cfg(feature = "oem")]
use crate::oem::OemIdentifier;
#[cfg(feature = "resource-status")]
use crate::ResourceStatusSchema;
#[cfg(feature = "resource-status")]
use std::convert::identity;
#[doc(inline)]
#[cfg(feature = "resource-status")]
pub use crate::schema::redfish::resource::Health;
#[doc(inline)]
#[cfg(feature = "resource-status")]
pub use crate::schema::redfish::resource::State;
#[doc(inline)]
#[cfg(feature = "computer-systems")]
pub use crate::schema::redfish::resource::PowerState;
pub type ResourceId = TaggedType<String, ResourceIdTag>;
pub type ResourceIdRef<'a> = TaggedType<&'a str, ResourceIdTag>;
#[doc(hidden)]
#[derive(tagged_types::Tag)]
#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
#[capability(inner_access, cloned)]
pub enum ResourceIdTag {}
pub type ResourceName = TaggedType<String, ResourceNameTag>;
pub type ResourceNameRef<'a> = TaggedType<&'a str, ResourceNameTag>;
#[doc(hidden)]
#[derive(tagged_types::Tag)]
#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
#[capability(inner_access, cloned)]
pub enum ResourceNameTag {}
pub type ResourceDescription = TaggedType<String, ResourceDescriptionTag>;
pub type ResourceDescriptionRef<'a> = TaggedType<&'a str, ResourceDescriptionTag>;
#[doc(hidden)]
#[derive(tagged_types::Tag)]
#[implement(Clone)]
#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
#[capability(inner_access, cloned)]
pub enum ResourceDescriptionTag {}
pub trait Resource {
fn resource_ref(&self) -> &ResourceSchema;
fn id(&self) -> ResourceIdRef<'_> {
ResourceIdRef::new(&self.resource_ref().id)
}
fn name(&self) -> ResourceNameRef<'_> {
ResourceNameRef::new(&self.resource_ref().name)
}
fn description(&self) -> Option<ResourceDescriptionRef<'_>> {
self.resource_ref()
.description
.as_ref()
.and_then(Option::as_deref)
.map(ResourceDescriptionRef::new)
}
#[cfg(feature = "oem")]
fn oem_id(&self) -> Option<OemIdentifier<&str>> {
oem_id_from_resource(self.resource_ref()).map(OemIdentifier::new)
}
fn odata_id(&self) -> &ODataId {
self.resource_ref().odata_id()
}
}
#[cfg(feature = "oem")]
pub(crate) fn oem_id_from_resource(r: &ResourceSchema) -> Option<&str> {
r.base
.oem
.as_ref()
.and_then(|v| v.additional_properties.as_object())
.and_then(|v| v.keys().next())
.map(String::as_str)
}
#[cfg(feature = "resource-status")]
#[derive(Clone, Debug)]
pub struct Status {
pub state: Option<State>,
pub health: Option<Health>,
pub health_rollup: Option<Health>,
}
#[cfg(feature = "resource-status")]
pub trait ResourceProvidesStatus {
fn resource_status_ref(&self) -> Option<&ResourceStatusSchema>;
fn status(&self) -> Option<Status> {
self.resource_status_ref().map(|status| Status {
state: status.state.and_then(identity),
health: status.health.and_then(identity),
health_rollup: status.health_rollup.and_then(identity),
})
}
}