1use crate::{
6 libs::core::type_id::{
7 CoreLibBaseTypeId, CoreLibTypeId, CoreLibVariantTypeId,
8 },
9 prelude::*,
10 types::entities::entity_type_definition::EntityTypeDefinition,
11 values::core_values::instant::Instant,
12};
13pub mod serde_dif;
14use crate::{
15 types::r#type::Type,
16 values::{
17 core_values::{
18 boolean::Boolean,
19 callable::Callable,
20 decimal::{
21 Decimal,
22 typed_decimal::{DecimalTypeVariant, TypedDecimal},
23 },
24 endpoint::Endpoint,
25 integer::{
26 Integer,
27 typed_integer::{IntegerTypeVariant, TypedInteger},
28 },
29 list::List,
30 map::Map,
31 range::Range,
32 receiver::ValueContainerReceiver,
33 sender::ValueContainerSender,
34 text::Text,
35 },
36 value_container::ValueContainer,
37 },
38};
39use core::fmt::{Debug, Display, Formatter};
40
41pub mod borrowed_core_value;
42pub mod convert_core_value;
43mod convert_parts;
44mod datex_hash;
45mod equality;
46mod iter_parts;
47mod ops;
48mod to_instructions;
49mod try_clone;
50
51#[derive(Default, Clone, Debug)]
52pub enum CoreValue {
53 #[default]
54 Uninitialized,
55 Null,
56 Boolean(Boolean),
57 Integer(Integer),
58 TypedInteger(TypedInteger),
59 Decimal(Decimal),
60 TypedDecimal(TypedDecimal),
61 Text(Text),
62 Endpoint(Endpoint),
63 List(List),
64 Map(Map),
65 Type(Type),
66 EntityTypeDefinition(EntityTypeDefinition),
67 Callable(Callable),
68 Range(Range),
69 Instant(Instant),
70 Sender(ValueContainerSender),
71 Receiver(ValueContainerReceiver),
72 Box(Box<ValueContainer>),
74}
75
76impl<T> FromIterator<T> for CoreValue
77where
78 T: Into<ValueContainer>,
79{
80 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
81 CoreValue::List(List::new(iter.into_iter().map(Into::into).collect()))
82 }
83}
84
85impl From<&CoreValue> for CoreLibTypeId {
86 fn from(value: &CoreValue) -> Self {
87 match value {
88 CoreValue::Map(_) => CoreLibTypeId::Base(CoreLibBaseTypeId::Map),
89 CoreValue::List(_) => CoreLibTypeId::Base(CoreLibBaseTypeId::List),
90 CoreValue::Text(_) => CoreLibTypeId::Base(CoreLibBaseTypeId::Text),
91 CoreValue::Boolean(_) => {
92 CoreLibTypeId::Base(CoreLibBaseTypeId::Boolean)
93 }
94 CoreValue::TypedInteger(i) => CoreLibTypeId::Variant(
95 CoreLibVariantTypeId::Integer(i.variant()),
96 ),
97 CoreValue::TypedDecimal(d) => CoreLibTypeId::Variant(
98 CoreLibVariantTypeId::Decimal(d.variant()),
99 ),
100 CoreValue::Integer(_) => {
101 CoreLibTypeId::Base(CoreLibBaseTypeId::Integer)
102 }
103 CoreValue::Decimal(_) => {
104 CoreLibTypeId::Base(CoreLibBaseTypeId::Decimal)
105 }
106 CoreValue::Endpoint(_) => {
107 CoreLibTypeId::Base(CoreLibBaseTypeId::Endpoint)
108 }
109 CoreValue::Null => CoreLibTypeId::Base(CoreLibBaseTypeId::Null),
110 CoreValue::Type(_) => CoreLibTypeId::Base(CoreLibBaseTypeId::Type),
111 CoreValue::Callable(_) => {
112 CoreLibTypeId::Base(CoreLibBaseTypeId::Callable)
113 }
114 CoreValue::Range(_) => {
115 CoreLibTypeId::Base(CoreLibBaseTypeId::Range)
116 }
117 CoreValue::EntityTypeDefinition(_nominal_type) => {
118 CoreLibTypeId::Base(CoreLibBaseTypeId::Never) }
120 CoreValue::Instant(_) => {
121 CoreLibTypeId::Base(CoreLibBaseTypeId::Instant)
122 }
123 CoreValue::Uninitialized => {
124 CoreLibTypeId::Base(CoreLibBaseTypeId::Never)
125 }
126 CoreValue::Box(_) => CoreLibTypeId::Base(CoreLibBaseTypeId::Box),
127 CoreValue::Sender(_) => {
128 CoreLibTypeId::Base(CoreLibBaseTypeId::Sender)
129 }
130 CoreValue::Receiver(_) => {
131 CoreLibTypeId::Base(CoreLibBaseTypeId::Receiver)
132 }
133 }
134 }
135}
136
137impl CoreValue {
138 pub fn new<T>(value: T) -> CoreValue
139 where
140 CoreValue: From<T>,
141 {
142 value.into()
143 }
144
145 pub fn is_collection_value(&self) -> bool {
148 core::matches!(self, CoreValue::List(_) | CoreValue::Map(_))
149 }
150
151 pub fn core_lib_type_id(&self) -> CoreLibTypeId {
156 CoreLibTypeId::from(self)
157 }
158
159 pub fn cast_to_text(&self) -> Text {
162 match self {
163 CoreValue::Text(text) => text.clone(),
164 _ => Text::String(self.to_string()),
165 }
166 }
167
168 pub fn cast_to_bool(&self) -> Option<Boolean> {
169 match self {
170 CoreValue::Text(text) => Some(Boolean(!text.as_str().is_empty())),
171 CoreValue::Boolean(bool) => Some(bool.clone()),
172 CoreValue::TypedInteger(int) => Some(Boolean(int.as_i128()? != 0)),
173 CoreValue::Null => Some(Boolean(false)),
174 _ => None,
175 }
176 }
177
178 pub fn cast_to_decimal(&self) -> Option<Decimal> {
179 match self {
180 CoreValue::Text(text) => {
181 text.as_string().parse::<f64>().ok().map(Decimal::from)
182 }
183 CoreValue::TypedInteger(int) => {
184 Some(Decimal::from(int.as_i128()? as f64))
185 }
186 CoreValue::TypedDecimal(decimal) => {
187 Some(Decimal::from(decimal.clone()))
188 }
189 CoreValue::Integer(int) => {
190 Some(Decimal::from(int.as_i128()? as f64))
191 }
192 CoreValue::Decimal(decimal) => Some(decimal.clone()),
193 _ => None,
194 }
195 }
196
197 pub fn cast_to_typed_decimal(
198 &self,
199 variant: DecimalTypeVariant,
200 ) -> Option<TypedDecimal> {
201 match self {
202 CoreValue::Text(text) => TypedDecimal::try_from_string_and_variant(
203 text.as_str(),
204 variant,
205 )
206 .ok(),
207 CoreValue::TypedInteger(int) => Some(
208 TypedDecimal::try_from_string_and_variant(
209 &int.to_string(),
210 variant,
211 )
212 .ok()?,
213 ),
214 CoreValue::TypedDecimal(decimal) => Some(
215 TypedDecimal::try_from_string_and_variant(
216 &decimal.to_string(),
217 variant,
218 )
219 .ok()?,
220 ),
221 CoreValue::Integer(int) => Some(
222 TypedDecimal::try_from_string_and_variant(
223 &int.to_string(),
224 variant,
225 )
226 .ok()?,
227 ),
228 CoreValue::Decimal(decimal) => Some(
229 TypedDecimal::try_from_string_and_variant(
230 &decimal.to_string(),
231 variant,
232 )
233 .ok()?,
234 ),
235 _ => None,
236 }
237 }
238
239 pub fn _cast_to_integer_internal(&self) -> Option<TypedInteger> {
241 match self {
242 CoreValue::Text(text) => {
243 Integer::try_from_string(&text.as_string())
244 .map(|x| Some(x.to_smallest_fitting()))
245 .unwrap_or(None)
246 }
247 CoreValue::TypedInteger(int) => {
248 Some(int.to_smallest_fitting().clone())
249 }
250 CoreValue::Integer(int) => {
251 Some(TypedInteger::IBig(int.clone()).to_smallest_fitting())
252 }
253 CoreValue::Decimal(decimal) => Some(
254 TypedInteger::from(decimal.into_f64() as i128)
255 .to_smallest_fitting(),
256 ),
257 CoreValue::TypedDecimal(decimal) => Some(
258 TypedInteger::from(decimal.as_f64() as i64)
259 .to_smallest_fitting(),
260 ),
261 _ => None,
262 }
263 }
264
265 pub fn cast_to_integer(&self) -> Option<Integer> {
267 match self {
268 CoreValue::Text(text) => {
269 Integer::try_from_string(&text.as_string()).ok()
270 }
271 CoreValue::TypedInteger(int) => Some(int.as_integer()),
272 CoreValue::Integer(int) => Some(int.clone()),
273 CoreValue::Decimal(decimal) => {
274 Some(Integer::from(decimal.into_f64() as i128))
277 }
278 CoreValue::TypedDecimal(decimal) => {
279 decimal.as_integer().map(Integer::from)
280 }
281 _ => None,
282 }
283 }
284
285 pub fn cast_to_typed_integer(
286 &self,
287 variant: IntegerTypeVariant,
288 ) -> Option<TypedInteger> {
289 match self {
290 CoreValue::Text(text) => TypedInteger::try_from_string_and_variant(
291 text.as_str(),
292 variant,
293 )
294 .ok(),
295 CoreValue::TypedInteger(int) => {
296 TypedInteger::try_from_string_and_variant(
297 &int.to_string(),
298 variant,
299 )
300 .ok()
301 }
302 CoreValue::Integer(int) => {
303 TypedInteger::try_from_string_and_variant(
304 int.to_string().as_str(),
305 variant,
306 )
307 .ok()
308 }
309 CoreValue::Decimal(decimal) => {
310 Some(TypedInteger::from(decimal.into_f64() as i128))
311 }
312 CoreValue::TypedDecimal(decimal) => {
313 decimal.as_integer().map(TypedInteger::from)
314 }
315 _ => None,
316 }
317 }
318
319 pub fn cast_to_endpoint(&self) -> Option<Endpoint> {
320 match self {
321 CoreValue::Text(text) => Endpoint::try_from(text.as_str()).ok(),
322 CoreValue::Endpoint(endpoint) => Some(endpoint.clone()),
323 _ => None,
324 }
325 }
326}
327
328impl Display for CoreValue {
329 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
330 match self {
331 CoreValue::Instant(instant) => write!(f, "{instant}"),
332 CoreValue::Type(ty) => write!(f, "{ty}"),
333 CoreValue::Boolean(bool) => write!(f, "{bool}"),
334 CoreValue::TypedInteger(int) => write!(f, "{int}"),
335 CoreValue::TypedDecimal(decimal) => write!(f, "{decimal}"),
336 CoreValue::Text(text) => write!(f, "{text}"),
337 CoreValue::Null => write!(f, "null"),
338 CoreValue::Endpoint(endpoint) => write!(f, "{endpoint}"),
339 CoreValue::Map(map) => write!(f, "{map}"),
340 CoreValue::Range(range) => {
341 write!(f, "{}..{}", range.start, range.end)
342 }
343 CoreValue::Integer(integer) => write!(f, "{integer}"),
344 CoreValue::Decimal(decimal) => write!(f, "{decimal}"),
345 CoreValue::List(list) => write!(f, "{list}"),
346 CoreValue::Callable(_callable) => write!(f, "[[ callable ]]"),
347 CoreValue::EntityTypeDefinition(container) => {
348 write!(f, "{container}")
349 }
350 CoreValue::Uninitialized => write!(f, "[[ uninitialized ]]"),
351 CoreValue::Box(inner) => write!(f, "({})", inner),
352 CoreValue::Sender(_sender) => write!(f, "[[ sender ]]"),
353 CoreValue::Receiver(_receiver) => write!(f, "[[ receiver ]]"),
354 }
355 }
356}
357
358impl From<Text> for CoreValue {
359 fn from(value: Text) -> Self {
360 CoreValue::Text(value)
361 }
362}
363
364impl From<Endpoint> for CoreValue {
365 fn from(value: Endpoint) -> Self {
366 CoreValue::Endpoint(value)
367 }
368}
369
370impl From<Range> for CoreValue {
371 fn from(value: Range) -> Self {
372 CoreValue::Range(value)
373 }
374}
375
376impl From<Callable> for CoreValue {
377 fn from(value: Callable) -> Self {
378 CoreValue::Callable(value)
379 }
380}
381
382impl From<Type> for CoreValue {
383 fn from(value: Type) -> Self {
384 CoreValue::Type(value)
385 }
386}
387
388impl From<EntityTypeDefinition> for CoreValue {
389 fn from(value: EntityTypeDefinition) -> Self {
390 CoreValue::EntityTypeDefinition(value)
391 }
392}
393
394impl From<Map> for CoreValue {
395 fn from(value: Map) -> Self {
396 CoreValue::Map(value)
397 }
398}
399
400impl From<List> for CoreValue {
401 fn from(value: List) -> Self {
402 CoreValue::List(value)
403 }
404}
405
406impl From<Decimal> for CoreValue {
407 fn from(value: Decimal) -> Self {
408 CoreValue::Decimal(value)
409 }
410}
411
412impl From<TypedDecimal> for CoreValue {
413 fn from(value: TypedDecimal) -> Self {
414 CoreValue::TypedDecimal(value)
415 }
416}
417
418impl From<Integer> for CoreValue {
419 fn from(value: Integer) -> Self {
420 CoreValue::Integer(value)
421 }
422}
423
424impl From<TypedInteger> for CoreValue {
425 fn from(value: TypedInteger) -> Self {
426 CoreValue::TypedInteger(value)
427 }
428}
429
430impl From<Boolean> for CoreValue {
431 fn from(value: Boolean) -> Self {
432 CoreValue::Boolean(value)
433 }
434}
435
436impl From<Instant> for CoreValue {
437 fn from(value: Instant) -> Self {
438 CoreValue::Instant(value)
439 }
440}
441
442#[cfg(test)]
443mod tests {
447 use log::{debug, info};
448
449 use super::*;
450 use crate::traits::{
451 convert_value::ConvertValue, get_core_lib_type_id::GetCoreLibTypeId,
452 };
453
454 #[test]
455 fn type_construct() {
456 let a = 42i32.to_value();
457 assert_eq!(a.core_lib_type_id().to_string(), "integer/i32");
458 }
459
460 #[test]
461 fn addition() {
462 let a = 42i32.to_value();
463 let b = 11i32.to_value();
464
465 let a_plus_b = (a.clone() + b.clone()).unwrap();
466 assert_eq!(a_plus_b.clone(), 53i32.to_value());
467 info!("{} + {} = {}", a.clone(), b.clone(), a_plus_b.clone());
468 }
469
470 #[test]
471 fn endpoint() {
472 let endpoint: Endpoint = "@test"
473 .to_string()
474 .to_value()
475 .try_into_core_value()
476 .unwrap()
477 .cast_to_endpoint()
478 .unwrap();
479 debug!("Endpoint: {endpoint}");
480 assert_eq!(endpoint.to_string(), "@test");
481 }
482
483 #[test]
484 pub fn range_from_core() {
485 assert_eq!(
486 CoreValue::Range(Range::new(
487 CoreValue::Integer(Integer::from(11)).into(),
488 CoreValue::Integer(Integer::from(13)).into()
489 ))
490 .to_string(),
491 "11..13"
492 );
493 }
494}