Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit cd4432e

Browse files
committed
Change DialectFunction to take &mut DialectInterpreter
- Update DialectFunction trait to use &mut DialectInterpreter - Update function signature in BTreeMap to use &mut - Update evaluate() and execute() methods to take &mut self - Update all test function implementations - Make interpreter variables mutable in tests This allows functions to modify interpreter state (caching, connections, etc.) which will be needed for IDE functions that maintain state or connections to external services.
1 parent c43c465 commit cd4432e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

server/src/dialect.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde_json::Value;
66
pub mod ambiguity;
77

88
pub struct DialectInterpreter<U> {
9-
functions: BTreeMap<String, fn(&DialectInterpreter<U>, Value) -> anyhow::Result<Value>>,
9+
functions: BTreeMap<String, fn(&mut DialectInterpreter<U>, Value) -> anyhow::Result<Value>>,
1010
userdata: U,
1111
}
1212

@@ -29,7 +29,7 @@ impl<U> DialectInterpreter<U> {
2929
self.functions.insert(type_name_lower, Self::execute::<F>);
3030
}
3131

32-
pub fn evaluate(&self, value: Value) -> anyhow::Result<Value> {
32+
pub fn evaluate(&mut self, value: Value) -> anyhow::Result<Value> {
3333
match value {
3434
Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => Ok(value),
3535
Value::Array(values) => Ok(Value::Array(
@@ -82,7 +82,7 @@ impl<U> DialectInterpreter<U> {
8282
}
8383
}
8484

85-
fn execute<F>(&self, value: Value) -> anyhow::Result<Value>
85+
fn execute<F>(&mut self, value: Value) -> anyhow::Result<Value>
8686
where
8787
F: DialectFunction<U>,
8888
{
@@ -102,7 +102,7 @@ impl<U> DialectInterpreter<U> {
102102
pub trait DialectFunction<U>: DeserializeOwned {
103103
type Output: Serialize;
104104

105-
fn execute(self, interpreter: &DialectInterpreter<U>) -> anyhow::Result<Self::Output>;
105+
fn execute(self, interpreter: &mut DialectInterpreter<U>) -> anyhow::Result<Self::Output>;
106106
}
107107

108108
#[cfg(test)]
@@ -119,7 +119,7 @@ mod tests {
119119
impl DialectFunction<()> for Uppercase {
120120
type Output = String;
121121

122-
fn execute(self, _interpreter: &DialectInterpreter<()>) -> anyhow::Result<Self::Output> {
122+
fn execute(self, _interpreter: &mut DialectInterpreter<()>) -> anyhow::Result<Self::Output> {
123123
Ok(self.text.to_uppercase())
124124
}
125125
}
@@ -134,7 +134,7 @@ mod tests {
134134
impl DialectFunction<()> for Concat {
135135
type Output = String;
136136

137-
fn execute(self, _interpreter: &DialectInterpreter<()>) -> anyhow::Result<Self::Output> {
137+
fn execute(self, _interpreter: &mut DialectInterpreter<()>) -> anyhow::Result<Self::Output> {
138138
Ok(format!("{}{}", self.left, self.right))
139139
}
140140
}
@@ -149,7 +149,7 @@ mod tests {
149149
impl DialectFunction<()> for Add {
150150
type Output = i32;
151151

152-
fn execute(self, _interpreter: &DialectInterpreter<()>) -> anyhow::Result<Self::Output> {
152+
fn execute(self, _interpreter: &mut DialectInterpreter<()>) -> anyhow::Result<Self::Output> {
153153
Ok(self.a + self.b)
154154
}
155155
}
@@ -210,7 +210,7 @@ mod tests {
210210

211211
#[test]
212212
fn test_literal_values() {
213-
let interpreter = DialectInterpreter::new(());
213+
let mut interpreter = DialectInterpreter::new(());
214214

215215
// Test that literal values pass through unchanged
216216
assert_eq!(
@@ -248,7 +248,7 @@ mod tests {
248248

249249
#[test]
250250
fn test_unknown_function_error() {
251-
let interpreter = DialectInterpreter::new(());
251+
let mut interpreter = DialectInterpreter::new(());
252252

253253
let input = serde_json::json!({"unknown": {"arg": "value"}});
254254
let result = interpreter.evaluate(input);
@@ -262,7 +262,7 @@ mod tests {
262262

263263
#[test]
264264
fn test_invalid_function_format() {
265-
let interpreter = DialectInterpreter::new(());
265+
let mut interpreter = DialectInterpreter::new(());
266266

267267
// Multiple keys in object
268268
let input = serde_json::json!({"func1": {}, "func2": {}});
@@ -284,7 +284,7 @@ where
284284
{
285285
type Output = V;
286286

287-
fn execute(self, _interpreter: &DialectInterpreter<U>) -> anyhow::Result<Self::Output> {
287+
fn execute(self, _interpreter: &mut DialectInterpreter<U>) -> anyhow::Result<Self::Output> {
288288
Ok(self)
289289
}
290290
}

0 commit comments

Comments
 (0)