Skip to main content

zen_engine/workspace/
mod.rs

1pub(crate) mod db;
2pub(crate) mod editor;
3pub(crate) mod graph;
4pub(crate) mod search;
5pub(crate) mod types;
6
7use std::sync::Arc;
8
9use crate::model::DecisionContent;
10use crate::policy::evaluator::EvalArtifact;
11use crate::policy::raw::PolicyDocument;
12use db::Db;
13use zen_expression::nl::NlResult;
14use zen_expression::variable::VariableType;
15
16pub use graph::{
17    FunctionResolutionRequest, FunctionTypeResolver, GraphAnalysis, GraphNodeAnalysis,
18    GraphSignature, GraphTraceMap,
19};
20pub use types::{
21    BlockExecution, BlockRef, BlockTrace, Completion, ConditionTrace, ConditionalSchema, Cursor,
22    CursorTarget, DecisionTableExtras, DependencyNode, Diagnostic, DiagnosticCode,
23    DiagnosticLocation, Dictionary, DictionaryEntryInfo, DiscriminantVariant, DiscriminatedUnion,
24    EngineEdit, Entity, EntityField, EvaluateRequest, EvaluationError, EvaluationResult,
25    ExpressionKind, FieldOrigin, GuardedProperty, InputProperty, InputValidationError,
26    InspectResult, NlExpression, OutputProperty, PrepareRename, PropertyKind, ReferenceKind,
27    ReferenceSite, RenameTarget, SchemaFieldKind, SchemaGroup, ScopeRequest, SearchHit,
28    SearchHitKind, Severity, Span, Trace, WriteConflict, WriteTrace,
29};
30
31use types::Global;
32
33pub struct Workspace {
34    db: Db,
35}
36
37impl Workspace {
38    pub fn new() -> Self {
39        Self { db: Db::new() }
40    }
41
42    pub fn set_document(&mut self, path: impl Into<Arc<str>>, document: DecisionContent) {
43        self.db.set_document(path.into(), Arc::new(document));
44    }
45
46    pub fn set_document_arc(&mut self, path: impl Into<Arc<str>>, document: Arc<DecisionContent>) {
47        self.db.set_document(path.into(), document);
48    }
49
50    pub fn set_policy(&mut self, path: impl Into<Arc<str>>, document: PolicyDocument) {
51        self.db.set_policy(path.into(), Arc::new(document));
52    }
53
54    pub fn set_policy_arc(&mut self, path: impl Into<Arc<str>>, document: Arc<PolicyDocument>) {
55        self.db.set_policy(path.into(), document);
56    }
57
58    pub fn remove_path(&mut self, path: &str) -> bool {
59        self.db.remove_document(path)
60    }
61
62    pub fn paths(&self) -> Vec<Arc<str>> {
63        self.db.document_paths()
64    }
65
66    pub fn get_document(&self, path: &str) -> Option<Arc<DecisionContent>> {
67        self.db.raw_document(path)
68    }
69
70    pub fn get_policy(&self, path: &str) -> Option<Arc<PolicyDocument>> {
71        self.db.raw_policy(path)
72    }
73
74    pub fn is_graph(&self, path: &str) -> bool {
75        self.db.is_graph(path)
76    }
77
78    pub fn evaluate(&self, req: &EvaluateRequest) -> Result<EvaluationResult, EvaluationError> {
79        self.db.evaluate(req)
80    }
81
82    pub fn enhance_trace(
83        &self,
84        req: &EvaluateRequest,
85    ) -> Result<EvaluationResult, EvaluationError> {
86        self.db.enhance_trace(req)
87    }
88
89    pub fn enhance_graph_trace(
90        &self,
91        document: &Arc<str>,
92        trace: &GraphTraceMap,
93    ) -> Result<Trace, EvaluationError> {
94        self.db.enhance_graph_trace(document, trace)
95    }
96
97    pub(crate) fn eval_artifact(&self, policy: &str) -> Arc<EvalArtifact> {
98        self.db.eval_artifact(policy)
99    }
100
101    pub fn entities(&self, req: &ScopeRequest) -> Vec<Entity> {
102        if self.db.is_graph(&req.policy_path) {
103            return Vec::new();
104        }
105        self.db.entities(req)
106    }
107
108    pub fn globals(&self, req: &ScopeRequest) -> Vec<Global> {
109        if self.db.is_graph(&req.policy_path) {
110            return Vec::new();
111        }
112        self.db.globals(req)
113    }
114
115    pub fn dictionaries(&self, req: &ScopeRequest) -> Vec<Dictionary> {
116        self.db.dictionaries(req)
117    }
118
119    pub fn inputs(&self, req: &ScopeRequest) -> Vec<InputProperty> {
120        self.db.inputs(req)
121    }
122
123    pub fn outputs(&self, req: &ScopeRequest) -> Vec<OutputProperty> {
124        self.db.outputs(req)
125    }
126
127    pub fn conditional_schema(&self, req: &ScopeRequest) -> ConditionalSchema {
128        self.db.conditional_schema(req)
129    }
130
131    pub fn component_members(&self, policy: &str) -> Vec<Arc<str>> {
132        self.db.component_members(policy)
133    }
134
135    pub fn cross_component_write_conflicts(&self) -> Vec<WriteConflict> {
136        self.db.cross_component_write_conflicts()
137    }
138
139    pub fn diagnostics(&self, path: &str) -> Vec<Diagnostic> {
140        let path_arc: Arc<str> = Arc::from(path);
141        (*self.db.policy_diagnostics(&path_arc)).clone()
142    }
143
144    pub fn all_diagnostics(&self) -> Vec<Diagnostic> {
145        self.db.all_diagnostics()
146    }
147
148    pub fn set_function_resolver(
149        &mut self,
150        resolver: impl Fn(&str, &VariableType) -> Option<String> + 'static,
151    ) {
152        self.db.set_function_resolver(Some(Box::new(resolver)));
153    }
154
155    pub fn function_resolution_requests(&self) -> Vec<FunctionResolutionRequest> {
156        self.db.function_resolution_requests()
157    }
158
159    pub fn set_function_type(&self, source: &str, input: &VariableType, ts_type: Option<&str>) {
160        self.db.set_function_type(source, input, ts_type);
161    }
162
163    pub fn graph_analysis(&self, path: &str) -> Option<Arc<GraphAnalysis>> {
164        let path_arc: Arc<str> = Arc::from(path);
165        self.db.graph_analysis(&path_arc)
166    }
167
168    pub fn unchecked_nodes(&self, path: &str) -> Vec<Arc<str>> {
169        self.db.graph_unchecked_nodes(path)
170    }
171
172    pub fn inspect(&self, cursor: &Cursor) -> Option<InspectResult> {
173        self.db.inspect(cursor)
174    }
175
176    pub fn completions(&self, cursor: &Cursor) -> Vec<Completion> {
177        self.db.completions(cursor)
178    }
179
180    pub fn nl(&self, policy_path: &str) -> Vec<NlExpression> {
181        self.db.nl(policy_path)
182    }
183
184    pub fn nl_tokenize(&self, cursor: &Cursor, text: &str) -> Option<NlResult> {
185        self.db.nl_tokenize(cursor, text)
186    }
187
188    pub fn prepare_rename(&self, cursor: &Cursor) -> Option<PrepareRename> {
189        self.db.prepare_rename(cursor)
190    }
191
192    pub fn rename(&self, target: &RenameTarget, new_name: &str) -> Vec<EngineEdit> {
193        self.db.rename(target, new_name)
194    }
195
196    pub fn references(&self, target: &RenameTarget) -> Vec<ReferenceSite> {
197        self.db.references(target)
198    }
199
200    pub fn search(&self, query: &str, limit: Option<u32>) -> Vec<SearchHit> {
201        self.db.search(query, limit)
202    }
203
204    pub fn input_skeleton(&self, req: &ScopeRequest) -> serde_json::Value {
205        self.db.input_skeleton(req)
206    }
207
208    pub fn dependencies(&self, target: &str) -> DependencyNode {
209        self.db.dependencies(target)
210    }
211
212    pub fn dependencies_scoped(&self, target: &str, document: Option<&str>) -> DependencyNode {
213        if let Some(doc) = document {
214            if self.db.is_graph(doc) {
215                let doc_arc: Arc<str> = Arc::from(doc);
216                return self.db.graph_dependencies(&doc_arc, target);
217            }
218        }
219        self.db.dependencies(target)
220    }
221}
222
223impl Default for Workspace {
224    fn default() -> Self {
225        Self::new()
226    }
227}