use crate::GenApiError;
use super::{
interface::{INode, IRegister, IString},
node_base::{NodeAttributeBase, NodeBase},
register_base::RegisterBase,
store::{CacheStore, NodeStore, ValueStore},
Device, GenApiResult, ValueCtxt,
};
#[derive(Debug, Clone)]
pub struct StringRegNode {
pub(crate) attr_base: NodeAttributeBase,
pub(crate) register_base: RegisterBase,
}
impl StringRegNode {
#[must_use]
pub fn register_base(&self) -> &RegisterBase {
&self.register_base
}
}
impl INode for StringRegNode {
fn node_base(&self) -> NodeBase {
let elem_base = &self.register_base.elem_base;
NodeBase::new(&self.attr_base, elem_base)
}
fn streamable(&self) -> bool {
self.register_base().streamable()
}
}
impl IString for StringRegNode {
#[tracing::instrument(skip(self, device, store, cx),
level = "trace",
fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
fn value<T: ValueStore, U: CacheStore>(
&self,
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<String> {
let nid = self.node_base().id();
let reg = self.register_base();
reg.with_cache_or_read(nid, device, store, cx, |data| {
let str_end = data.iter().position(|b| *b == 0).unwrap_or(data.len());
Ok(String::from_utf8_lossy(&data[..str_end]).to_string())
})
}
#[tracing::instrument(skip(self, device, store, cx),
level = "trace",
fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
fn set_value<T: ValueStore, U: CacheStore>(
&self,
value: String,
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<()> {
let max_length = self.max_length(device, store, cx)? as usize;
if !value.is_ascii() {
return Err(GenApiError::invalid_data(
"the data must be an ascii string".into(),
));
}
if value.len() > max_length {
return Err(GenApiError::invalid_data(
"the data length exceeds the maximum length allowed by the node.".into(),
));
}
let nid = self.node_base().id();
cx.invalidate_cache_by(nid);
let reg = self.register_base();
let mut bytes = value.into_bytes();
bytes.resize(max_length, 0);
reg.write_and_cache(nid, &bytes, device, store, cx)
}
#[tracing::instrument(skip(self, device, store, cx),
level = "trace",
fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
fn max_length<T: ValueStore, U: CacheStore>(
&self,
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<i64> {
self.length(device, store, cx)
}
#[tracing::instrument(skip(self, device, store, cx),
level = "trace",
fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
fn is_readable<T: ValueStore, U: CacheStore>(
&self,
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<bool> {
self.register_base().is_readable(device, store, cx)
}
#[tracing::instrument(skip(self, device, store, cx),
level = "trace",
fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
fn is_writable<T: ValueStore, U: CacheStore>(
&self,
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<bool> {
self.register_base().is_writable(device, store, cx)
}
}
impl IRegister for StringRegNode {
fn read<T: ValueStore, U: CacheStore>(
&self,
buf: &mut [u8],
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<()> {
let address = self.address(device, store, cx)?;
let length = self.length(device, store, cx)?;
self.register_base().read_and_cache(
self.node_base().id(),
address,
length,
buf,
device,
store,
cx,
)
}
fn write<T: ValueStore, U: CacheStore>(
&self,
buf: &[u8],
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<()> {
self.register_base()
.write_and_cache(self.node_base().id(), buf, device, store, cx)
}
fn address<T: ValueStore, U: CacheStore>(
&self,
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<i64> {
self.register_base().address(device, store, cx)
}
fn length<T: ValueStore, U: CacheStore>(
&self,
device: &mut impl Device,
store: &impl NodeStore,
cx: &mut ValueCtxt<T, U>,
) -> GenApiResult<i64> {
self.register_base().length(device, store, cx)
}
}