loose_liquid_core/model/value/
cow.rs1use std::fmt;
2
3use crate::model::KStringCow;
4
5use super::DisplayCow;
6use super::State;
7use super::Value;
8use super::{ValueView, ValueViewCmp};
9use crate::model::array::{Array, ArrayView};
10use crate::model::object::{Object, ObjectView};
11use crate::model::scalar::{Scalar, ScalarCow};
12
13#[derive(Clone, Debug)]
15pub enum ValueCow<'s> {
16 Owned(Value),
18 Borrowed(&'s dyn ValueView),
20}
21
22impl<'s> ValueCow<'s> {
23 pub fn into_owned(self) -> Value {
27 match self {
28 ValueCow::Owned(x) => x,
29 ValueCow::Borrowed(x) => x.to_value(),
30 }
31 }
32
33 pub fn as_view(&self) -> &dyn ValueView {
35 match self {
36 ValueCow::Owned(o) => o.as_view(),
37 ValueCow::Borrowed(b) => *b,
38 }
39 }
40}
41
42impl<'s> ValueView for ValueCow<'s> {
43 fn as_debug(&self) -> &dyn fmt::Debug {
44 self
45 }
46
47 fn render(&self) -> DisplayCow<'_> {
48 self.as_view().render()
49 }
50 fn source(&self) -> DisplayCow<'_> {
51 self.as_view().source()
52 }
53 fn type_name(&self) -> &'static str {
54 self.as_view().type_name()
55 }
56 fn query_state(&self, state: State) -> bool {
57 self.as_view().query_state(state)
58 }
59
60 fn to_kstr(&self) -> KStringCow<'_> {
61 self.as_view().to_kstr()
62 }
63 fn to_value(&self) -> Value {
64 self.as_view().to_value()
65 }
66
67 fn as_scalar(&self) -> Option<ScalarCow<'_>> {
68 self.as_view().as_scalar()
69 }
70
71 fn as_array(&self) -> Option<&dyn ArrayView> {
72 self.as_view().as_array()
73 }
74
75 fn as_object(&self) -> Option<&dyn ObjectView> {
76 self.as_view().as_object()
77 }
78
79 fn as_state(&self) -> Option<State> {
80 self.as_view().as_state()
81 }
82
83 fn is_nil(&self) -> bool {
84 self.as_view().is_nil()
85 }
86}
87
88impl From<Value> for ValueCow<'static> {
89 fn from(other: Value) -> Self {
90 ValueCow::Owned(other)
91 }
92}
93
94impl<'s> From<&'s Value> for ValueCow<'s> {
95 fn from(other: &'s Value) -> Self {
96 ValueCow::Borrowed(other.as_view())
97 }
98}
99
100impl From<Scalar> for ValueCow<'static> {
101 fn from(other: Scalar) -> Self {
102 ValueCow::Owned(Value::Scalar(other))
103 }
104}
105
106impl From<Array> for ValueCow<'static> {
107 fn from(other: Array) -> Self {
108 ValueCow::Owned(Value::Array(other))
109 }
110}
111
112impl From<Object> for ValueCow<'static> {
113 fn from(other: Object) -> Self {
114 ValueCow::Owned(Value::Object(other))
115 }
116}
117
118impl From<State> for ValueCow<'static> {
119 fn from(other: State) -> Self {
120 ValueCow::Owned(Value::State(other))
121 }
122}
123
124impl<'v> Default for ValueCow<'v> {
125 fn default() -> Self {
126 ValueCow::Owned(Value::default())
127 }
128}
129
130impl<'v> PartialEq<ValueCow<'v>> for ValueCow<'v> {
131 fn eq(&self, other: &Self) -> bool {
132 super::value_eq(self.as_view(), other.as_view())
133 }
134}
135
136impl<'v> PartialEq<ValueViewCmp<'v>> for ValueCow<'v> {
137 fn eq(&self, other: &ValueViewCmp<'v>) -> bool {
138 ValueViewCmp::new(self.as_view()) == *other
139 }
140}
141
142impl<'v> PartialEq<Value> for ValueCow<'v> {
143 fn eq(&self, other: &Value) -> bool {
144 super::value_eq(self.as_view(), other.as_view())
145 }
146}
147
148impl<'v> PartialEq<i64> for ValueCow<'v> {
149 fn eq(&self, other: &i64) -> bool {
150 super::value_eq(self.as_view(), other)
151 }
152}
153
154impl<'v> PartialEq<f64> for ValueCow<'v> {
155 fn eq(&self, other: &f64) -> bool {
156 super::value_eq(self.as_view(), other)
157 }
158}
159
160impl<'v> PartialEq<bool> for ValueCow<'v> {
161 fn eq(&self, other: &bool) -> bool {
162 super::value_eq(self.as_view(), other)
163 }
164}
165
166impl<'v> PartialEq<crate::model::scalar::DateTime> for ValueCow<'v> {
167 fn eq(&self, other: &crate::model::scalar::DateTime) -> bool {
168 super::value_eq(self.as_view(), other)
169 }
170}
171
172impl<'v> PartialEq<crate::model::scalar::Date> for ValueCow<'v> {
173 fn eq(&self, other: &crate::model::scalar::Date) -> bool {
174 super::value_eq(self.as_view(), other)
175 }
176}
177
178impl<'v> PartialEq<str> for ValueCow<'v> {
179 fn eq(&self, other: &str) -> bool {
180 let other = KStringCow::from_ref(other);
181 super::value_eq(self.as_view(), &other)
182 }
183}
184
185impl<'v> PartialEq<&'v str> for ValueCow<'v> {
186 fn eq(&self, other: &&str) -> bool {
187 super::value_eq(self.as_view(), other)
188 }
189}
190
191impl<'v> PartialEq<String> for ValueCow<'v> {
192 fn eq(&self, other: &String) -> bool {
193 super::value_eq(self.as_view(), other)
194 }
195}
196
197impl<'v> PartialEq<crate::model::KString> for ValueCow<'v> {
198 fn eq(&self, other: &crate::model::KString) -> bool {
199 super::value_eq(self.as_view(), &other.as_ref())
200 }
201}
202
203impl<'v> PartialEq<crate::model::KStringRef<'v>> for ValueCow<'v> {
204 fn eq(&self, other: &crate::model::KStringRef<'v>) -> bool {
205 super::value_eq(self.as_view(), other)
206 }
207}
208
209impl<'v> PartialEq<crate::model::KStringCow<'v>> for ValueCow<'v> {
210 fn eq(&self, other: &crate::model::KStringCow<'v>) -> bool {
211 super::value_eq(self.as_view(), other)
212 }
213}