use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::str;
use crate::Property;
use crate::error::ModelError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceTreeProperty {
name: String,
value: Vec<u8>,
}
impl<'a> Property<'a> for &'a DeviceTreeProperty {
fn name(&self) -> &'a str {
&self.name
}
fn value(&self) -> &'a [u8] {
&self.value
}
}
impl DeviceTreeProperty {
pub fn new(name: impl Into<String>, value: impl Into<Vec<u8>>) -> Result<Self, ModelError> {
let name = name.into();
if !crate::validate::is_valid_property_name(&name) {
return Err(ModelError::InvalidPropertyName(name));
}
Ok(Self {
name,
value: value.into(),
})
}
#[must_use]
pub fn new_unchecked(name: impl Into<String>, value: impl Into<Vec<u8>>) -> Self {
Self {
name: name.into(),
value: value.into(),
}
}
pub fn set_value(&mut self, value: impl Into<Vec<u8>>) {
self.value = value.into();
}
}
impl<'a, T: Property<'a>> From<T> for DeviceTreeProperty {
fn from(prop: T) -> Self {
let name = prop.name().to_string();
let value = prop.value().to_vec();
DeviceTreeProperty { name, value }
}
}