cameleon_genapi/
port.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use super::{
6    elem_type::ImmOrPNode,
7    interface::{INode, IPort},
8    node_base::{NodeAttributeBase, NodeBase, NodeElementBase},
9    store::{CacheStore, NodeStore, ValueStore},
10    Device, GenApiError, GenApiResult, ValueCtxt,
11};
12
13#[derive(Debug, Clone)]
14pub struct PortNode {
15    pub(crate) attr_base: NodeAttributeBase,
16    pub(crate) elem_base: NodeElementBase,
17
18    pub(crate) chunk_id: Option<ImmOrPNode<u64>>,
19    pub(crate) swap_endianness: bool,
20    pub(crate) cache_chunk_data: bool,
21}
22
23impl PortNode {
24    #[must_use]
25    pub fn chunk_id(&self) -> Option<&ImmOrPNode<u64>> {
26        self.chunk_id.as_ref()
27    }
28
29    #[must_use]
30    pub fn swap_endianness(&self) -> bool {
31        self.swap_endianness
32    }
33
34    #[must_use]
35    pub fn cache_chunk_data(&self) -> bool {
36        self.cache_chunk_data
37    }
38}
39
40impl INode for PortNode {
41    fn node_base(&self) -> NodeBase {
42        NodeBase::new(&self.attr_base, &self.elem_base)
43    }
44
45    fn streamable(&self) -> bool {
46        false
47    }
48}
49
50impl IPort for PortNode {
51    #[tracing::instrument(skip(self, device, store),
52                          level = "trace",
53                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
54    fn read<T: ValueStore, U: CacheStore>(
55        &self,
56        address: i64,
57        buf: &mut [u8],
58        device: &mut impl Device,
59        store: &impl NodeStore,
60        _: &mut ValueCtxt<T, U>,
61    ) -> GenApiResult<()> {
62        if self.chunk_id.is_some() {
63            Err(GenApiError::chunk_data_missing())
64        } else {
65            device.read_mem(address, buf).map_err(GenApiError::device)
66        }
67    }
68
69    #[tracing::instrument(skip(self, device, store, cx),
70                          level = "trace",
71                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
72    fn write<T: ValueStore, U: CacheStore>(
73        &self,
74        address: i64,
75        buf: &[u8],
76        device: &mut impl Device,
77        store: &impl NodeStore,
78        cx: &mut ValueCtxt<T, U>,
79    ) -> GenApiResult<()> {
80        cx.invalidate_cache_by(self.node_base().id());
81
82        if self.chunk_id.is_some() {
83            // TODO: Implement chunk parser.
84            todo!()
85        } else {
86            device.write_mem(address, buf).map_err(GenApiError::device)
87        }
88    }
89}