Skip to main content

pipa/lang/
adapter.rs

1// SPDX-FileCopyrightText: Copyright 2026 olav@occy.org
2// SPDX-License-Identifier: MPL-2.0
3
4//! Define template functions and built-in values.
5
6use crate::lang::arity::Arity;
7use crate::value::Value;
8use crate::value::keys::Keys;
9use crate::value::map::Map;
10use crate::value::operation;
11use crate::value::operation::Operation;
12use crate::value::span::Span;
13
14/// Define a custom adapter for your environment.
15pub trait Adapter: core::fmt::Debug {
16    fn template_source(&self) -> Option<&str> {
17        None
18    }
19
20    fn template_span(&self) -> Option<Span> {
21        None
22    }
23
24    fn value_get(&self, map: &Map, keys: &Keys) -> Option<Value> {
25        operation::get_map_value(map, keys)
26    }
27
28    #[doc(hidden)]
29    fn record_setter(&self, _: &Value) -> Result<Option<Operation>, String> {
30        Ok(None)
31    }
32
33    #[doc(hidden)]
34    fn record_getter(&self, _: &Value) -> Result<Option<Operation>, String> {
35        Ok(None)
36    }
37
38    #[doc(hidden)]
39    fn record_operation(&self, _: &Operation) -> Result<(), String> {
40        Ok(())
41    }
42
43    #[doc(hidden)]
44    fn erase_operation(&self, _: &Value) -> Result<Vec<Operation>, String> {
45        Ok(Vec::default())
46    }
47
48    fn func_find(&self, _: &[u8]) -> Option<Arity> {
49        None
50    }
51
52    fn func_call(&self, _: &[u8], _: Vec<Option<Value>>) -> Result<Option<Value>, String> {
53        Ok(None)
54    }
55}
56
57/// A default noop-y adapter for simple scenarios.
58#[derive(Debug, Default)]
59pub struct Default {
60    _private: (),
61}
62
63impl super::Adapter for Default {}