cameleon_genapi/
string.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, IString},
8    ivalue::IValue,
9    node_base::{NodeAttributeBase, NodeBase, NodeElementBase},
10    store::{CacheStore, NodeStore, StringId, ValueStore},
11    Device, GenApiResult, ValueCtxt,
12};
13
14#[derive(Debug, Clone)]
15pub struct StringNode {
16    pub(crate) attr_base: NodeAttributeBase,
17    pub(crate) elem_base: NodeElementBase,
18
19    pub(crate) streamable: bool,
20    pub(crate) value: ImmOrPNode<StringId>,
21}
22
23impl StringNode {
24    #[must_use]
25    pub fn streamable(&self) -> bool {
26        self.streamable
27    }
28
29    #[must_use]
30    pub fn value_elem(&self) -> ImmOrPNode<StringId> {
31        self.value
32    }
33}
34
35impl INode for StringNode {
36    fn node_base(&self) -> NodeBase {
37        NodeBase::new(&self.attr_base, &self.elem_base)
38    }
39
40    fn streamable(&self) -> bool {
41        self.streamable
42    }
43}
44
45impl IString for StringNode {
46    #[tracing::instrument(skip(self, device, store, cx),
47                          level = "trace",
48                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
49    fn value<T: ValueStore, U: CacheStore>(
50        &self,
51        device: &mut impl Device,
52        store: &impl NodeStore,
53        cx: &mut ValueCtxt<T, U>,
54    ) -> GenApiResult<String> {
55        self.value.value(device, store, cx)
56    }
57
58    #[tracing::instrument(skip(self, device, store, cx),
59                          level = "trace",
60                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
61    fn set_value<T: ValueStore, U: CacheStore>(
62        &self,
63        value: String,
64        device: &mut impl Device,
65        store: &impl NodeStore,
66        cx: &mut ValueCtxt<T, U>,
67    ) -> GenApiResult<()> {
68        cx.invalidate_cache_by(self.node_base().id());
69        self.value.set_value(value, device, store, cx)
70    }
71
72    #[tracing::instrument(skip(self, device, store, cx),
73                          level = "trace",
74                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
75    fn max_length<T: ValueStore, U: CacheStore>(
76        &self,
77        device: &mut impl Device,
78        store: &impl NodeStore,
79        cx: &mut ValueCtxt<T, U>,
80    ) -> GenApiResult<i64> {
81        match self.value {
82            ImmOrPNode::Imm(_) => Ok(i64::MAX),
83            ImmOrPNode::PNode(nid) => nid
84                .expect_istring_kind(store)?
85                .max_length(device, store, cx),
86        }
87    }
88
89    #[tracing::instrument(skip(self, device, store, cx),
90                          level = "trace",
91                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
92    fn is_readable<T: ValueStore, U: CacheStore>(
93        &self,
94        device: &mut impl Device,
95        store: &impl NodeStore,
96        cx: &mut ValueCtxt<T, U>,
97    ) -> GenApiResult<bool> {
98        Ok(self.elem_base.is_readable(device, store, cx)?
99            && self.value.is_readable(device, store, cx)?)
100    }
101
102    #[tracing::instrument(skip(self, device, store, cx),
103                          level = "trace",
104                          fields(node = store.name_by_id(self.node_base().id()).unwrap()))]
105    fn is_writable<T: ValueStore, U: CacheStore>(
106        &self,
107        device: &mut impl Device,
108        store: &impl NodeStore,
109        cx: &mut ValueCtxt<T, U>,
110    ) -> GenApiResult<bool> {
111        Ok(self.elem_base.is_writable(device, store, cx)?
112            && self.value.is_writable(device, store, cx)?)
113    }
114}