From 562bd4d745a9ec468bc88435098033cf4739b9b7 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Sun, 21 Nov 2021 15:17:05 -0500 Subject: [PATCH] Value: add an `into_static` method This allows a parsed document to be stored as a reference-using instance, but if values need pulled out for later storage, they can be made independent. --- src/common.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/common.rs b/src/common.rs index aae4d39..c5189fa 100644 --- a/src/common.rs +++ b/src/common.rs @@ -58,6 +58,26 @@ pub enum Value<'a, T: Text<'a>> { Object(BTreeMap>), } +impl<'a, T: Text<'a>> Value<'a, T> { + pub fn into_static(&self) -> Value<'static, String> { + match self { + Self::Variable(v) => Value::Variable(v.as_ref().into()), + Self::Int(i) => Value::Int(i.clone()), + Self::Float(f) => Value::Float(*f), + Self::String(s) => Value::String(s.clone()), + Self::Boolean(b) => Value::Boolean(*b), + Self::Null => Value::Null, + Self::Enum(v) => Value::Enum(v.as_ref().into()), + Self::List(l) => { + Value::List(l.iter().map(|e| e.into_static()).collect()) + }, + Self::Object(o) => { + Value::Object(o.iter().map(|(k, v)| (k.as_ref().into(), v.into_static())).collect()) + }, + } + } +} + #[derive(Debug, Clone, PartialEq)] pub enum Type<'a, T: Text<'a>> { NamedType(T::Value),