Skip to main content

yang_base/definition/
view.rs

1//! 启动期预编译的 Table View 运行时产物。
2
3use super::{ActionHandle, ModuleName, ViewName};
4use std::sync::Arc;
5
6/// 启动期已解析的树 View 拓扑。
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct CompiledTreeView {
9    id_field: yang_db::FieldRef,
10    parent_field: yang_db::FieldRef,
11    label_field: yang_db::FieldRef,
12    max_nodes: usize,
13}
14
15impl CompiledTreeView {
16    pub(crate) fn new(
17        id_field: yang_db::FieldRef,
18        parent_field: yang_db::FieldRef,
19        label_field: yang_db::FieldRef,
20        max_nodes: usize,
21    ) -> Self {
22        Self {
23            id_field,
24            parent_field,
25            label_field,
26            max_nodes,
27        }
28    }
29
30    /// 返回节点唯一标识字段。
31    pub fn id_field(&self) -> &yang_db::FieldRef {
32        &self.id_field
33    }
34
35    /// 返回节点唯一标识的本地字段名。
36    pub fn id_field_name(&self) -> &str {
37        local_field_name(&self.id_field)
38    }
39
40    /// 返回父节点标识字段。
41    pub fn parent_field(&self) -> &yang_db::FieldRef {
42        &self.parent_field
43    }
44
45    /// 返回父节点标识的本地字段名。
46    pub fn parent_field_name(&self) -> &str {
47        local_field_name(&self.parent_field)
48    }
49
50    /// 返回节点用户可见标签字段。
51    pub fn label_field(&self) -> &yang_db::FieldRef {
52        &self.label_field
53    }
54
55    /// 返回节点标签的本地字段名。
56    pub fn label_field_name(&self) -> &str {
57        local_field_name(&self.label_field)
58    }
59
60    /// 返回启动期解析后的单次树查询节点上限。
61    pub fn max_nodes(&self) -> usize {
62        self.max_nodes
63    }
64}
65
66fn local_field_name(field: &yang_db::FieldRef) -> &str {
67    field
68        .as_str()
69        .rsplit_once('.')
70        .map_or(field.as_str(), |(_, name)| name)
71}
72
73/// 已解析字段引用和按钮 Action slot 的只读 View。
74#[derive(Debug, Clone, PartialEq, Eq)]
75pub struct CompiledTableView {
76    module: ModuleName,
77    name: ViewName,
78    table: yang_db::TableRef,
79    fields: Arc<[yang_db::FieldRef]>,
80    data_action: Option<ActionHandle>,
81    actions: Arc<[ActionHandle]>,
82    tree: Option<CompiledTreeView>,
83}
84
85impl CompiledTableView {
86    pub(crate) fn new(
87        module: ModuleName,
88        name: ViewName,
89        table: yang_db::TableRef,
90        fields: Vec<yang_db::FieldRef>,
91        data_action: Option<ActionHandle>,
92        actions: Vec<ActionHandle>,
93        tree: Option<CompiledTreeView>,
94    ) -> Self {
95        Self {
96            module,
97            name,
98            table,
99            fields: fields.into(),
100            data_action,
101            actions: actions.into(),
102            tree,
103        }
104    }
105
106    /// 返回所属 Module。
107    pub fn module(&self) -> &ModuleName {
108        &self.module
109    }
110
111    /// 返回 View 局部名称。
112    pub fn name(&self) -> &ViewName {
113        &self.name
114    }
115
116    /// 返回预校验表引用。
117    pub fn table(&self) -> &yang_db::TableRef {
118        &self.table
119    }
120
121    /// 返回预校验字段引用。
122    pub fn fields(&self) -> &[yang_db::FieldRef] {
123        &self.fields
124    }
125
126    /// 返回可选的表格数据查询 Action slot。
127    pub fn data_action(&self) -> Option<&ActionHandle> {
128        self.data_action.as_ref()
129    }
130
131    /// 返回预解析按钮 Action slot。
132    pub fn actions(&self) -> &[ActionHandle] {
133        &self.actions
134    }
135
136    /// 返回可选的显式树拓扑。
137    pub fn tree(&self) -> Option<&CompiledTreeView> {
138        self.tree.as_ref()
139    }
140}