semantic_operators/
state.rs1use crate::{Error, ErrorKind};
3use serde_json::Value;
4#[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)]
6#[serde(try_from = "Value", into = "Value")]
7pub struct State(Value);
8impl State {
9 pub fn text(text: impl Into<String>) -> Self {
11 Self(Value::String(text.into()))
12 }
13 pub fn as_value(&self) -> &Value {
15 &self.0
16 }
17 pub fn into_value(self) -> Value {
19 self.0
20 }
21}
22impl TryFrom<Value> for State {
23 type Error = Error;
24 fn try_from(value: Value) -> Result<Self, Error> {
29 match value {
30 Value::String(_) | Value::Object(_) | Value::Array(_) => Ok(Self(value)),
31 _ => Err(Error::new(
32 ErrorKind::InvalidState,
33 "state must be text, a JSON object, or an array",
34 )),
35 }
36 }
37}
38impl std::fmt::Debug for State {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 f.write_str("State(<private>)")
41 }
42}
43
44impl From<State> for Value {
45 fn from(state: State) -> Value {
46 state.0
47 }
48}