1use crate::ResourceSchema;
19use tagged_types::TaggedType;
20
21#[cfg(feature = "oem")]
22use crate::oem::OemIdentifier;
23#[cfg(feature = "resource-status")]
24use crate::ResourceStatusSchema;
25#[cfg(feature = "resource-status")]
26use std::convert::identity;
27
28#[doc(inline)]
29#[cfg(feature = "resource-status")]
30pub use crate::schema::redfish::resource::Health;
31
32#[doc(inline)]
33#[cfg(feature = "resource-status")]
34pub use crate::schema::redfish::resource::State;
35
36#[doc(inline)]
37#[cfg(feature = "computer-systems")]
38pub use crate::schema::redfish::resource::PowerState;
39
40pub type ResourceId = TaggedType<String, ResourceIdTag>;
42pub type ResourceIdRef<'a> = TaggedType<&'a String, ResourceIdTag>;
44#[doc(hidden)]
45#[derive(tagged_types::Tag)]
46#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
47#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
48#[capability(inner_access, cloned)]
49pub enum ResourceIdTag {}
50
51pub type ResourceName = TaggedType<String, ResourceNameTag>;
53pub type ResourceNameRef<'a> = TaggedType<&'a String, ResourceNameTag>;
55#[doc(hidden)]
56#[derive(tagged_types::Tag)]
57#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
58#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
59#[capability(inner_access, cloned)]
60pub enum ResourceNameTag {}
61
62pub type ResourceDescription = TaggedType<String, ResourceDescriptionTag>;
64pub type ResourceDescriptionRef<'a> = TaggedType<&'a String, ResourceDescriptionTag>;
66#[doc(hidden)]
67#[derive(tagged_types::Tag)]
68#[implement(Clone)]
69#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
70#[capability(inner_access, cloned)]
71pub enum ResourceDescriptionTag {}
72
73pub trait Resource {
75 fn resource_ref(&self) -> &ResourceSchema;
77
78 fn id(&self) -> ResourceIdRef<'_> {
80 ResourceIdRef::new(&self.resource_ref().id)
81 }
82
83 fn name(&self) -> ResourceNameRef<'_> {
85 ResourceNameRef::new(&self.resource_ref().name)
86 }
87
88 fn description(&self) -> Option<ResourceDescriptionRef<'_>> {
90 self.resource_ref()
91 .description
92 .as_ref()
93 .and_then(|v| v.as_ref())
94 .map(ResourceDescriptionRef::new)
95 }
96
97 #[cfg(feature = "oem")]
99 fn oem_id(&self) -> Option<OemIdentifier<&String>> {
100 self.resource_ref()
101 .base
102 .oem
103 .as_ref()
104 .and_then(|v| v.additional_properties.as_object())
105 .and_then(|v| v.keys().next())
106 .map(OemIdentifier::new)
107 }
108}
109
110#[cfg(feature = "resource-status")]
112#[derive(Clone, Debug)]
113pub struct Status {
114 pub state: Option<State>,
116 pub health: Option<Health>,
118 pub health_rollup: Option<Health>,
120}
121
122#[cfg(feature = "resource-status")]
124pub trait ResourceProvidesStatus {
125 fn resource_status_ref(&self) -> Option<&ResourceStatusSchema>;
128
129 fn status(&self) -> Option<Status> {
131 self.resource_status_ref().map(|status| Status {
132 state: status.state.and_then(identity),
133 health: status.health.and_then(identity),
134 health_rollup: status.health_rollup.and_then(identity),
135 })
136 }
137}