spacetimedb_sats/algebraic_type.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
pub mod fmt;
pub mod map_notation;
use crate::algebraic_value::de::{ValueDeserializeError, ValueDeserializer};
use crate::algebraic_value::ser::value_serialize;
use crate::de::Deserialize;
use crate::meta_type::MetaType;
use crate::product_type::{ADDRESS_TAG, IDENTITY_TAG};
use crate::sum_type::{OPTION_NONE_TAG, OPTION_SOME_TAG};
use crate::{i256, u256};
use crate::{
AlgebraicTypeRef, AlgebraicValue, ArrayType, MapType, ProductType, SpacetimeType, SumType, SumTypeVariant,
};
use derive_more::From;
use enum_as_inner::EnumAsInner;
/// The SpacetimeDB Algebraic Type System (SATS) is a structural type system in
/// which a nominal type system can be constructed.
///
/// The type system unifies the concepts sum types, product types, scalar value types,
/// and convenience types strings, arrays, and maps,
/// into a single type system.
#[derive(EnumAsInner, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, SpacetimeType, From)]
#[sats(crate = crate)]
pub enum AlgebraicType {
/// A type where the definition is given by the typing context (`Typespace`).
/// In other words, this is defined by a pointer to another `AlgebraicType`.
///
/// This should not be conflated with reference and pointer types in languages like Rust,
/// In other words, this is not `&T` or `*const T`.
Ref(AlgebraicTypeRef),
/// A structural sum type.
///
/// Unlike most languages, sums in SATs are *[structural]* and not nominal.
/// When checking whether two nominal types are the same,
/// their names and/or declaration sites (e.g., module / namespace) are considered.
/// Meanwhile, a structural type system would only check the structure of the type itself,
/// e.g., the names of its variants and their inner data types in the case of a sum.
///
/// This is also known as a discriminated union (implementation) or disjoint union.
/// Another name is [coproduct (category theory)](https://ncatlab.org/nlab/show/coproduct).
///
/// These structures are known as sum types because the number of possible values a sum
/// ```ignore
/// { N_0(T_0), N_1(T_1), ..., N_n(T_n) }
/// ```
/// is:
/// ```ignore
/// Σ (i ∈ 0..n). values(T_i)
/// ```
/// so for example, `values({ A(U64), B(Bool) }) = values(U64) + values(Bool)`.
///
/// See also: https://ncatlab.org/nlab/show/sum+type.
///
/// [structural]: https://en.wikipedia.org/wiki/Structural_type_system
Sum(SumType),
/// A structural product type.
///
/// This is also known as `struct` and `tuple` in many languages,
/// but note that unlike most languages, sums in SATs are *[structural]* and not nominal.
/// When checking whether two nominal types are the same,
/// their names and/or declaration sites (e.g., module / namespace) are considered.
/// Meanwhile, a structural type system would only check the structure of the type itself,
/// e.g., the names of its fields and their types in the case of a record.
/// The name "product" comes from category theory.
///
/// See also: https://ncatlab.org/nlab/show/product+type.
///
/// These structures are known as product types because the number of possible values in product
/// ```ignore
/// { N_0: T_0, N_1: T_1, ..., N_n: T_n }
/// ```
/// is:
/// ```ignore
/// Π (i ∈ 0..n). values(T_i)
/// ```
/// so for example, `values({ A: U64, B: Bool }) = values(U64) * values(Bool)`.
///
/// [structural]: https://en.wikipedia.org/wiki/Structural_type_system
Product(ProductType),
/// The type of array values where elements are of a base type `elem_ty`.
/// Values [`AlgebraicValue::Array(array)`](crate::AlgebraicValue::Array) will have this type.
Array(ArrayType),
/// The type of map values consisting of a key type `key_ty` and value `ty`.
/// Values [`AlgebraicValue::Map(map)`](crate::AlgebraicValue::Map) will have this type.
/// The order of entries in a map value is observable.
Map(Box<MapType>),
/// The UTF-8 encoded `String` type.
/// Values [`AlgebraicValue::String(s)`](crate::AlgebraicValue::String) will have this type.
///
/// This type exists for convenience and because it is easy to just use Rust's `String` (UTF-8)
/// as opposed to rolling your own equivalent byte-array based UTF-8 encoding.
String,
/// The bool type. Values [`AlgebraicValue::Bool(b)`](crate::AlgebraicValue::Bool) will have this type.
Bool,
/// The `I8` type. Values [`AlgebraicValue::I8(v)`](crate::AlgebraicValue::I8) will have this type.
I8,
/// The `U8` type. Values [`AlgebraicValue::U8(v)`](crate::AlgebraicValue::U8) will have this type.
U8,
/// The `I16` type. Values [`AlgebraicValue::I16(v)`](crate::AlgebraicValue::I16) will have this type.
I16,
/// The `U16` type. Values [`AlgebraicValue::U16(v)`](crate::AlgebraicValue::U16) will have this type.
U16,
/// The `I32` type. Values [`AlgebraicValue::I32(v)`](crate::AlgebraicValue::I32) will have this type.
I32,
/// The `U32` type. Values [`AlgebraicValue::U32(v)`](crate::AlgebraicValue::U32) will have this type.
U32,
/// The `I64` type. Values [`AlgebraicValue::I64(v)`](crate::AlgebraicValue::I64) will have this type.
I64,
/// The `U64` type. Values [`AlgebraicValue::U64(v)`](crate::AlgebraicValue::U64) will have this type.
U64,
/// The `I128` type. Values [`AlgebraicValue::I128(v)`](crate::AlgebraicValue::I128) will have this type.
I128,
/// The `U128` type. Values [`AlgebraicValue::U128(v)`](crate::AlgebraicValue::U128) will have this type.
U128,
/// The `I256` type. Values [`AlgebraicValue::I256(v)`](crate::AlgebraicValue::I256) will have this type.
I256,
/// The `U256` type. Values [`AlgebraicValue::U256(v)`](crate::AlgebraicValue::U256) will have this type.
U256,
/// The `F32` type. Values [`AlgebraicValue::F32(v)`](crate::AlgebraicValue::F32) will have this type.
F32,
/// The `F64` type. Values [`AlgebraicValue::F64(v)`](crate::AlgebraicValue::F64) will have this type.
F64,
}
impl MetaType for AlgebraicType {
/// This is a static function that constructs the type of `AlgebraicType`
/// and returns it as an `AlgebraicType`.
///
/// This could alternatively be implemented
/// as a regular AlgebraicValue or as a static variable.
fn meta_type() -> Self {
AlgebraicType::sum([
("ref", AlgebraicTypeRef::meta_type()),
("sum", SumType::meta_type()),
("product", ProductType::meta_type()),
("array", ArrayType::meta_type()),
("map", MapType::meta_type()),
("string", AlgebraicType::unit()),
("bool", AlgebraicType::unit()),
("i8", AlgebraicType::unit()),
("u8", AlgebraicType::unit()),
("i16", AlgebraicType::unit()),
("u16", AlgebraicType::unit()),
("i32", AlgebraicType::unit()),
("u32", AlgebraicType::unit()),
("i64", AlgebraicType::unit()),
("u64", AlgebraicType::unit()),
("i128", AlgebraicType::unit()),
("u128", AlgebraicType::unit()),
("i256", AlgebraicType::unit()),
("u256", AlgebraicType::unit()),
("f32", AlgebraicType::unit()),
("f64", AlgebraicType::unit()),
])
}
}
/// Provided to enable `mem::take`.
impl Default for AlgebraicType {
fn default() -> Self {
Self::ZERO_REF
}
}
impl AlgebraicType {
/// The first type in the typespace.
pub const ZERO_REF: Self = Self::Ref(AlgebraicTypeRef(0));
/// Returns whether this type is the conventional address type.
pub fn is_address(&self) -> bool {
matches!(self, Self::Product(p) if p.is_address())
}
/// Returns whether this type is the conventional identity type.
pub fn is_identity(&self) -> bool {
matches!(self, Self::Product(p) if p.is_identity())
}
/// Returns whether this type is the conventional `ScheduleAt` type.
pub fn is_schedule_at(&self) -> bool {
matches!(self, Self::Sum(p) if p.is_schedule_at())
}
/// Returns whether this type is a unit type.
pub fn is_unit(&self) -> bool {
matches!(self, Self::Product(p) if p.is_unit())
}
/// Returns whether this type is a never type.
pub fn is_never(&self) -> bool {
matches!(self, Self::Sum(p) if p.is_empty())
}
/// If this type is the standard option type, returns the type of the `some` variant.
/// Otherwise, returns `None`.
pub fn as_option(&self) -> Option<&AlgebraicType> {
self.as_sum()?.as_option()
}
/// Returns whether this type is scalar or a string type.
pub fn is_scalar_or_string(&self) -> bool {
self.is_scalar() || self.is_string()
}
/// Returns whether this type is one which holds a scalar value.
///
/// A scalar value is one not made up of other values, i.e., not composite.
/// These are all integer and float values,
/// i.e., integer and float types are scalar.
/// References to other types, i.e., [`AlgebraicType::Ref`]s are not scalar.
pub fn is_scalar(&self) -> bool {
self.is_bool() || self.is_integer() || self.is_float()
}
/// Returns whether the type is a signed integer type.
pub fn is_signed(&self) -> bool {
matches!(
self,
Self::I8 | Self::I16 | Self::I32 | Self::I64 | Self::I128 | Self::I256
)
}
/// Returns whether the type is an unsigned integer type.
pub fn is_unsigned(&self) -> bool {
matches!(
self,
Self::U8 | Self::U16 | Self::U32 | Self::U64 | Self::U128 | Self::U256
)
}
/// Returns whether this type is one of the integer types, e.g., `U64` and `I32`.
pub fn is_integer(&self) -> bool {
self.is_signed() || self.is_unsigned()
}
/// Returns whether the type is a float type.
pub fn is_float(&self) -> bool {
matches!(self, Self::F32 | Self::F64)
}
/// The canonical 0-element unit type.
pub fn unit() -> Self {
let fs: [AlgebraicType; 0] = [];
Self::product(fs)
}
/// The canonical 0-variant "never" / "absurd" / "void" type.
pub fn never() -> Self {
let vs: [SumTypeVariant; 0] = [];
Self::sum(vs)
}
/// A type representing an array of `U8`s.
pub fn bytes() -> Self {
Self::array(Self::U8)
}
/// Returns whether this type is `AlgebraicType::bytes()`.
pub fn is_bytes(&self) -> bool {
self.as_array().is_some_and(|ty| ty.elem_ty.is_u8())
}
/// Whether this type, or the types it references, contain any `AlgebraicTypeRef`s.
pub fn contains_refs(&self) -> bool {
match self {
AlgebraicType::Ref(_) => true,
AlgebraicType::Product(ProductType { elements }) => {
elements.iter().any(|elem| elem.algebraic_type.contains_refs())
}
AlgebraicType::Sum(SumType { variants }) => {
variants.iter().any(|variant| variant.algebraic_type.contains_refs())
}
AlgebraicType::Array(array) => array.elem_ty.contains_refs(),
AlgebraicType::Map(map) => map.key_ty.contains_refs() || map.ty.contains_refs(),
_ => false,
}
}
/// Returns a sum type with the given `sum`.
pub fn sum<S: Into<SumType>>(sum: S) -> Self {
AlgebraicType::Sum(sum.into())
}
/// Returns a product type with the given `prod`.
pub fn product<P: Into<ProductType>>(prod: P) -> Self {
AlgebraicType::Product(prod.into())
}
/// Returns a structural option type where `some_type` is the type for the `some` variant.
pub fn option(some_type: Self) -> Self {
Self::sum([(OPTION_SOME_TAG, some_type), (OPTION_NONE_TAG, AlgebraicType::unit())])
}
/// Returns an unsized array type where the element type is `ty`.
pub fn array(ty: Self) -> Self {
ArrayType { elem_ty: Box::new(ty) }.into()
}
/// Returns a map type from the type `key` to the type `value`.
pub fn map(key: Self, value: Self) -> Self {
MapType::new(key, value).into()
}
/// Construct a copy of the `Identity` type.
pub fn identity() -> Self {
AlgebraicType::product([(IDENTITY_TAG, AlgebraicType::bytes())])
}
/// Construct a copy of the `Address` type.
pub fn address() -> Self {
AlgebraicType::product([(ADDRESS_TAG, AlgebraicType::bytes())])
}
/// Returns a sum type of unit variants with names taken from `var_names`.
pub fn simple_enum<'a>(var_names: impl Iterator<Item = &'a str>) -> Self {
Self::sum(var_names.into_iter().map(SumTypeVariant::unit).collect::<Box<[_]>>())
}
pub fn as_value(&self) -> AlgebraicValue {
value_serialize(self)
}
pub fn from_value(value: &AlgebraicValue) -> Result<Self, ValueDeserializeError> {
Self::deserialize(ValueDeserializer::from_ref(value))
}
#[inline]
/// Given an AlgebraicType, returns the min value for that type.
pub fn min_value(&self) -> Option<AlgebraicValue> {
match *self {
Self::I8 => Some(i8::MIN.into()),
Self::U8 => Some(u8::MIN.into()),
Self::I16 => Some(i16::MIN.into()),
Self::U16 => Some(u16::MIN.into()),
Self::I32 => Some(i32::MIN.into()),
Self::U32 => Some(u32::MIN.into()),
Self::I64 => Some(i64::MIN.into()),
Self::U64 => Some(u64::MIN.into()),
Self::I128 => Some(i128::MIN.into()),
Self::U128 => Some(u128::MIN.into()),
Self::I256 => Some(i256::MIN.into()),
Self::U256 => Some(u256::MIN.into()),
Self::F32 => Some(f32::MIN.into()),
Self::F64 => Some(f64::MIN.into()),
_ => None,
}
}
#[inline]
/// Given an AlgebraicType, returns the max value for that type.
pub fn max_value(&self) -> Option<AlgebraicValue> {
match *self {
Self::I8 => Some(i8::MAX.into()),
Self::U8 => Some(u8::MAX.into()),
Self::I16 => Some(i16::MAX.into()),
Self::U16 => Some(u16::MAX.into()),
Self::I32 => Some(i32::MAX.into()),
Self::U32 => Some(u32::MAX.into()),
Self::I64 => Some(i64::MAX.into()),
Self::U64 => Some(u64::MAX.into()),
Self::I128 => Some(i128::MAX.into()),
Self::U128 => Some(u128::MAX.into()),
Self::I256 => Some(i256::MAX.into()),
Self::U256 => Some(u256::MAX.into()),
Self::F32 => Some(f32::MAX.into()),
Self::F64 => Some(f64::MAX.into()),
_ => None,
}
}
/// Check if the type is one of a small number of special, known types
/// with specific layouts.
/// See also [`ProductType::is_special`] and [`SumType::is_special`].
pub fn is_special(&self) -> bool {
match self {
AlgebraicType::Product(product) => product.is_special(),
AlgebraicType::Sum(sum) => sum.is_special(),
_ => false,
}
}
/// Validates that the type can be used to generate a type definition
/// in a `SpacetimeDB` client module.
///
/// Such a type must be a non-special sum or product type.
/// All of the elements of the type must be [`valid_for_client_type_use`](AlgebraicType::valid_for_client_type_use).
///
/// This method does not actually follow `Ref`s to check the types they point to,
/// it only checks the structure of this type.
pub fn is_valid_for_client_type_definition(&self) -> bool {
// Special types should not be used to generate type definitions.
if self.is_special() {
return false;
}
match self {
AlgebraicType::Sum(sum) => sum
.variants
.iter()
.all(|variant| variant.algebraic_type.is_valid_for_client_type_use()),
AlgebraicType::Product(product) => product
.elements
.iter()
.all(|elem| elem.algebraic_type.is_valid_for_client_type_use()),
_ => false,
}
}
/// Validates that the type can be used to generate a *use* of a type in a `SpacetimeDB` client module.
/// (As opposed to a *definition* of a type.)
///
/// This means that the type is either:
/// - a reference
/// - a special, known type
/// - a non-compound type like `U8`, `I32`, `F64`, etc.
/// - or a map, array, or option built from types that are [`valid_for_client_type_use`](AlgebraicType::valid_for_client_type_use).
///
/// This method does not actually follow `Ref`s to check the types they point to,
/// it only checks the structure of the type.
pub fn is_valid_for_client_type_use(&self) -> bool {
match self {
AlgebraicType::Sum(sum) => {
if let Some(wrapped) = sum.as_option() {
wrapped.is_valid_for_client_type_use()
} else {
sum.is_special() || sum.is_empty()
}
}
AlgebraicType::Product(product) => product.is_special() || product.is_unit(),
AlgebraicType::Array(array) => array.elem_ty.is_valid_for_client_type_use(),
AlgebraicType::Map(map) => {
map.key_ty.is_valid_for_client_type_use() && map.ty.is_valid_for_client_type_use()
}
AlgebraicType::Ref(_) => true,
_ => true,
}
}
}
#[cfg(test)]
mod tests {
use super::AlgebraicType;
use crate::meta_type::MetaType;
use crate::satn::Satn;
use crate::{
algebraic_type::fmt::fmt_algebraic_type, algebraic_type::map_notation::fmt_algebraic_type as fmt_map,
algebraic_type_ref::AlgebraicTypeRef, typespace::Typespace,
};
use crate::{ValueWithType, WithTypespace};
#[test]
fn never() {
assert_eq!("(|)", fmt_algebraic_type(&AlgebraicType::never()).to_string());
}
#[test]
fn never_map() {
assert_eq!("{ ty_: Sum }", fmt_map(&AlgebraicType::never()).to_string());
}
#[test]
fn unit() {
assert_eq!("()", fmt_algebraic_type(&AlgebraicType::unit()).to_string());
}
#[test]
fn unit_map() {
assert_eq!("{ ty_: Product }", fmt_map(&AlgebraicType::unit()).to_string());
}
#[test]
fn primitive() {
assert_eq!("U8", fmt_algebraic_type(&AlgebraicType::U8).to_string());
}
#[test]
fn primitive_map() {
assert_eq!("{ ty_: U8 }", fmt_map(&AlgebraicType::U8).to_string());
}
#[test]
fn option() {
let option = AlgebraicType::option(AlgebraicType::never());
assert_eq!("(some: (|) | none: ())", fmt_algebraic_type(&option).to_string());
}
#[test]
fn option_map() {
let option = AlgebraicType::option(AlgebraicType::never());
assert_eq!(
"{ ty_: Sum, some: { ty_: Sum }, none: { ty_: Product } }",
fmt_map(&option).to_string()
);
}
#[test]
fn algebraic_type() {
let algebraic_type = AlgebraicType::meta_type();
assert_eq!(
"(\
ref: U32 \
| sum: (variants: Array<(\
name: (some: String | none: ()), \
algebraic_type: &0\
)>) \
| product: (elements: Array<(\
name: (some: String | none: ()), \
algebraic_type: &0\
)>) \
| array: &0 \
| map: (key_ty: &0, ty: &0) \
| string: () \
| bool: () \
| i8: () | u8: () \
| i16: () | u16: () \
| i32: () | u32: () \
| i64: () | u64: () \
| i128: () | u128: () \
| i256: () | u256: () \
| f32: () | f64: ()\
)",
fmt_algebraic_type(&algebraic_type).to_string()
);
}
#[test]
fn algebraic_type_map() {
let algebraic_type = AlgebraicType::meta_type();
assert_eq!(
"{ \
ty_: Sum, \
ref: { ty_: U32 }, \
sum: { \
ty_: Product, \
variants: { \
ty_: Array, \
0: { \
ty_: Product, \
name: { ty_: Sum, some: { ty_: String }, none: { ty_: Product } }, \
algebraic_type: { ty_: Ref, 0: 0 } \
} \
} \
}, \
product: { \
ty_: Product, \
elements: { \
ty_: Array, \
0: { \
ty_: Product, \
name: { ty_: Sum, some: { ty_: String }, none: { ty_: Product } }, \
algebraic_type: { ty_: Ref, 0: 0 } \
} \
} \
}, \
array: { ty_: Ref, 0: 0 }, \
map: { ty_: Product, key_ty: { ty_: Ref, 0: 0 }, ty: { ty_: Ref, 0: 0 } }, \
string: { ty_: Product }, \
bool: { ty_: Product }, \
i8: { ty_: Product }, u8: { ty_: Product }, \
i16: { ty_: Product }, u16: { ty_: Product }, \
i32: { ty_: Product }, u32: { ty_: Product }, \
i64: { ty_: Product }, u64: { ty_: Product }, \
i128: { ty_: Product }, u128: { ty_: Product }, \
i256: { ty_: Product }, u256: { ty_: Product }, \
f32: { ty_: Product }, f64: { ty_: Product } \
}",
fmt_map(&algebraic_type).to_string()
);
}
#[test]
fn nested_products_and_sums() {
let builtin = AlgebraicType::U8;
let product = AlgebraicType::product([("thing", AlgebraicType::U8)]);
let sum = AlgebraicType::sum([builtin.clone(), builtin.clone(), product]);
let next = AlgebraicType::product([
(Some("test"), builtin.clone()),
(None, sum),
(None, builtin),
(Some("never"), AlgebraicType::never()),
]);
assert_eq!(
"(test: U8, 1: (U8 | U8 | (thing: U8)), 2: U8, never: (|))",
fmt_algebraic_type(&next).to_string()
);
}
fn in_space<'a, T: crate::Value>(ts: &'a Typespace, ty: &'a T::Type, val: &'a T) -> ValueWithType<'a, T> {
WithTypespace::new(ts, ty).with_value(val)
}
#[test]
fn option_as_value() {
let option = AlgebraicType::option(AlgebraicType::never());
let algebraic_type = AlgebraicType::meta_type();
let typespace = Typespace::new(vec![algebraic_type]);
let at_ref = AlgebraicType::Ref(AlgebraicTypeRef(0));
assert_eq!(
r#"(sum = (variants = [(name = (some = "some"), algebraic_type = (sum = (variants = []))), (name = (some = "none"), algebraic_type = (product = (elements = [])))]))"#,
in_space(&typespace, &at_ref, &option.as_value()).to_satn()
);
}
#[test]
fn algebraic_type_as_value() {
let algebraic_type = AlgebraicType::meta_type();
let typespace = Typespace::new(vec![algebraic_type.clone()]);
let at_ref = AlgebraicType::Ref(AlgebraicTypeRef(0));
let ref0 = "algebraic_type = (ref = 0)";
let unit = "algebraic_type = (product = (elements = []))";
let aggr_elems_ty = format!(
"algebraic_type = (array = (product = (elements = [\
(\
name = (some = \"name\"), \
algebraic_type = (sum = (variants = [\
(name = (some = \"some\"), algebraic_type = (string = ())), \
(name = (some = \"none\"), {unit})\
]))\
), \
(name = (some = \"algebraic_type\"), {ref0})\
])))"
);
assert_eq!(
format!(
"(\
sum = (\
variants = [\
(name = (some = \"ref\"), algebraic_type = (u32 = ())), \
(\
name = (some = \"sum\"), \
algebraic_type = (product = (elements = [\
(name = (some = \"variants\"), {aggr_elems_ty})\
]))\
), \
(\
name = (some = \"product\"), \
algebraic_type = (product = (elements = [\
(name = (some = \"elements\"), {aggr_elems_ty})\
]))\
), \
(name = (some = \"array\"), {ref0}), \
(\
name = (some = \"map\"), \
algebraic_type = (product = (elements = [\
(name = (some = \"key_ty\"), {ref0}), \
(name = (some = \"ty\"), {ref0})\
]))\
), \
(name = (some = \"string\"), {unit}), \
(name = (some = \"bool\"), {unit}), \
(name = (some = \"i8\"), {unit}), \
(name = (some = \"u8\"), {unit}), \
(name = (some = \"i16\"), {unit}), \
(name = (some = \"u16\"), {unit}), \
(name = (some = \"i32\"), {unit}), \
(name = (some = \"u32\"), {unit}), \
(name = (some = \"i64\"), {unit}), \
(name = (some = \"u64\"), {unit}), \
(name = (some = \"i128\"), {unit}), \
(name = (some = \"u128\"), {unit}), \
(name = (some = \"i256\"), {unit}), \
(name = (some = \"u256\"), {unit}), \
(name = (some = \"f32\"), {unit}), \
(name = (some = \"f64\"), {unit})\
]\
)\
)"
),
in_space(&typespace, &at_ref, &algebraic_type.as_value()).to_satn()
);
}
#[test]
fn option_from_value() {
let option = AlgebraicType::option(AlgebraicType::never());
AlgebraicType::from_value(&option.as_value()).expect("No errors.");
}
#[test]
fn builtin_from_value() {
let u8 = AlgebraicType::U8;
AlgebraicType::from_value(&u8.as_value()).expect("No errors.");
}
#[test]
fn algebraic_type_from_value() {
let algebraic_type = AlgebraicType::meta_type();
AlgebraicType::from_value(&algebraic_type.as_value()).expect("No errors.");
}
}