2021-10-01 05:11:49 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-11-09 21:55:05 +00:00
|
|
|
use strum_macros::EnumIter;
|
2021-10-01 05:11:49 +00:00
|
|
|
|
2021-09-02 01:29:43 +00:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2022-03-07 20:08:56 +00:00
|
|
|
use crate::SyntaxShape;
|
|
|
|
|
2022-11-09 21:55:05 +00:00
|
|
|
#[derive(Clone, Debug, Default, EnumIter, PartialEq, Eq, Serialize, Deserialize, Hash)]
|
2021-09-02 01:29:43 +00:00
|
|
|
pub enum Type {
|
2022-12-13 16:46:22 +00:00
|
|
|
Any,
|
|
|
|
Binary,
|
2021-09-02 01:29:43 +00:00
|
|
|
Block,
|
2022-12-13 16:46:22 +00:00
|
|
|
Bool,
|
2021-09-06 22:02:24 +00:00
|
|
|
CellPath,
|
2022-12-13 16:46:22 +00:00
|
|
|
Closure,
|
|
|
|
Custom(String),
|
2021-10-05 02:27:39 +00:00
|
|
|
Date,
|
2022-12-13 16:46:22 +00:00
|
|
|
Duration,
|
|
|
|
Error,
|
2021-09-02 01:29:43 +00:00
|
|
|
Filesize,
|
2022-12-13 16:46:22 +00:00
|
|
|
Float,
|
|
|
|
Int,
|
2021-09-02 01:29:43 +00:00
|
|
|
List(Box<Type>),
|
2022-12-13 16:46:22 +00:00
|
|
|
ListStream,
|
2022-11-09 21:55:05 +00:00
|
|
|
#[default]
|
2021-09-02 01:29:43 +00:00
|
|
|
Nothing,
|
2022-12-13 16:46:22 +00:00
|
|
|
Number,
|
|
|
|
Range,
|
2021-11-19 04:30:27 +00:00
|
|
|
Record(Vec<(String, Type)>),
|
2021-12-27 19:13:52 +00:00
|
|
|
Signature,
|
2022-12-13 16:46:22 +00:00
|
|
|
String,
|
|
|
|
Table(Vec<(String, Type)>),
|
2021-09-02 01:29:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 20:08:56 +00:00
|
|
|
impl Type {
|
2022-11-09 21:55:05 +00:00
|
|
|
pub fn is_subtype(&self, other: &Type) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(t, u) if t == u => true,
|
|
|
|
(Type::Float, Type::Number) => true,
|
|
|
|
(Type::Int, Type::Number) => true,
|
|
|
|
(_, Type::Any) => true,
|
|
|
|
(Type::List(t), Type::List(u)) if t.is_subtype(u) => true, // List is covariant
|
|
|
|
|
|
|
|
// TODO: Currently Record types specify their field types. If we are
|
|
|
|
// going to continue to do that, then it might make sense to define
|
|
|
|
// a "structural subtyping" whereby r1 is a subtype of r2 is the
|
|
|
|
// fields of r1 are a "subset" of the fields of r2 (names are a
|
|
|
|
// subset and agree on types). However, if we do that, then we need
|
|
|
|
// a way to specify the supertype of all Records. For now, we define
|
|
|
|
// any Record to be a subtype of any other Record. This allows
|
|
|
|
// Record(vec![]) to be used as an ad-hoc supertype of all Records
|
|
|
|
// in command signatures. This comment applies to Tables also, with
|
|
|
|
// "columns" in place of "fields".
|
|
|
|
(Type::Record(_), Type::Record(_)) => true,
|
|
|
|
(Type::Table(_), Type::Table(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_numeric(&self) -> bool {
|
|
|
|
matches!(self, Type::Int | Type::Float | Type::Number)
|
|
|
|
}
|
|
|
|
|
2023-02-22 12:53:11 +00:00
|
|
|
pub fn is_list(&self) -> bool {
|
|
|
|
matches!(self, Type::List(_))
|
|
|
|
}
|
|
|
|
|
2022-11-09 21:55:05 +00:00
|
|
|
/// Does this type represent a data structure containing values that can be addressed using 'cell paths'?
|
|
|
|
pub fn accepts_cell_paths(&self) -> bool {
|
|
|
|
matches!(self, Type::List(_) | Type::Record(_) | Type::Table(_))
|
|
|
|
}
|
|
|
|
|
2022-03-07 20:08:56 +00:00
|
|
|
pub fn to_shape(&self) -> SyntaxShape {
|
|
|
|
match self {
|
|
|
|
Type::Int => SyntaxShape::Int,
|
|
|
|
Type::Float => SyntaxShape::Number,
|
|
|
|
Type::Range => SyntaxShape::Range,
|
|
|
|
Type::Bool => SyntaxShape::Boolean,
|
|
|
|
Type::String => SyntaxShape::String,
|
2022-11-10 08:21:49 +00:00
|
|
|
Type::Block => SyntaxShape::Block, // FIXME needs more accuracy
|
|
|
|
Type::Closure => SyntaxShape::Closure(None), // FIXME needs more accuracy
|
2022-03-07 20:08:56 +00:00
|
|
|
Type::CellPath => SyntaxShape::CellPath,
|
|
|
|
Type::Duration => SyntaxShape::Duration,
|
|
|
|
Type::Date => SyntaxShape::DateTime,
|
|
|
|
Type::Filesize => SyntaxShape::Filesize,
|
|
|
|
Type::List(x) => SyntaxShape::List(Box::new(x.to_shape())),
|
|
|
|
Type::Number => SyntaxShape::Number,
|
2022-11-09 21:55:05 +00:00
|
|
|
Type::Nothing => SyntaxShape::Nothing,
|
2022-03-07 20:08:56 +00:00
|
|
|
Type::Record(_) => SyntaxShape::Record,
|
2022-04-07 04:34:09 +00:00
|
|
|
Type::Table(_) => SyntaxShape::Table,
|
2022-03-07 20:08:56 +00:00
|
|
|
Type::ListStream => SyntaxShape::List(Box::new(SyntaxShape::Any)),
|
2022-04-07 04:34:09 +00:00
|
|
|
Type::Any => SyntaxShape::Any,
|
2022-03-07 20:08:56 +00:00
|
|
|
Type::Error => SyntaxShape::Any,
|
|
|
|
Type::Binary => SyntaxShape::Binary,
|
2022-06-10 15:59:35 +00:00
|
|
|
Type::Custom(_) => SyntaxShape::Any,
|
2022-03-07 20:08:56 +00:00
|
|
|
Type::Signature => SyntaxShape::Signature,
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 16:18:33 +00:00
|
|
|
|
|
|
|
/// Get a string representation, without inner type specification of lists,
|
|
|
|
/// tables and records (get `list` instead of `list<any>`
|
|
|
|
pub fn get_non_specified_string(&self) -> String {
|
|
|
|
match self {
|
|
|
|
Type::Block => String::from("block"),
|
|
|
|
Type::Closure => String::from("closure"),
|
|
|
|
Type::Bool => String::from("bool"),
|
|
|
|
Type::CellPath => String::from("cell path"),
|
|
|
|
Type::Date => String::from("date"),
|
|
|
|
Type::Duration => String::from("duration"),
|
|
|
|
Type::Filesize => String::from("filesize"),
|
|
|
|
Type::Float => String::from("float"),
|
|
|
|
Type::Int => String::from("int"),
|
|
|
|
Type::Range => String::from("range"),
|
|
|
|
Type::Record(_) => String::from("record"),
|
|
|
|
Type::Table(_) => String::from("table"),
|
|
|
|
Type::List(_) => String::from("list"),
|
|
|
|
Type::Nothing => String::from("nothing"),
|
|
|
|
Type::Number => String::from("number"),
|
|
|
|
Type::String => String::from("string"),
|
|
|
|
Type::ListStream => String::from("list stream"),
|
|
|
|
Type::Any => String::from("any"),
|
|
|
|
Type::Error => String::from("error"),
|
|
|
|
Type::Binary => String::from("binary"),
|
|
|
|
Type::Custom(_) => String::from("custom"),
|
|
|
|
Type::Signature => String::from("signature"),
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 20:08:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 01:29:43 +00:00
|
|
|
impl Display for Type {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Type::Block => write!(f, "block"),
|
2022-11-10 08:21:49 +00:00
|
|
|
Type::Closure => write!(f, "closure"),
|
2021-09-02 01:29:43 +00:00
|
|
|
Type::Bool => write!(f, "bool"),
|
2021-09-06 22:02:24 +00:00
|
|
|
Type::CellPath => write!(f, "cell path"),
|
2021-10-05 02:27:39 +00:00
|
|
|
Type::Date => write!(f, "date"),
|
2021-09-02 01:29:43 +00:00
|
|
|
Type::Duration => write!(f, "duration"),
|
|
|
|
Type::Filesize => write!(f, "filesize"),
|
|
|
|
Type::Float => write!(f, "float"),
|
|
|
|
Type::Int => write!(f, "int"),
|
2021-09-04 21:52:57 +00:00
|
|
|
Type::Range => write!(f, "range"),
|
2022-11-20 13:22:42 +00:00
|
|
|
Type::Record(fields) => {
|
|
|
|
if fields.is_empty() {
|
|
|
|
write!(f, "record")
|
|
|
|
} else {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"record<{}>",
|
|
|
|
fields
|
|
|
|
.iter()
|
2023-01-30 01:37:54 +00:00
|
|
|
.map(|(x, y)| format!("{x}: {y}"))
|
2022-11-20 13:22:42 +00:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", "),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Type::Table(columns) => {
|
|
|
|
if columns.is_empty() {
|
|
|
|
write!(f, "table")
|
|
|
|
} else {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"table<{}>",
|
|
|
|
columns
|
|
|
|
.iter()
|
2023-01-30 01:37:54 +00:00
|
|
|
.map(|(x, y)| format!("{x}: {y}"))
|
2022-11-20 13:22:42 +00:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-01-30 01:37:54 +00:00
|
|
|
Type::List(l) => write!(f, "list<{l}>"),
|
2021-09-02 01:29:43 +00:00
|
|
|
Type::Nothing => write!(f, "nothing"),
|
|
|
|
Type::Number => write!(f, "number"),
|
|
|
|
Type::String => write!(f, "string"),
|
2022-02-24 11:57:31 +00:00
|
|
|
Type::ListStream => write!(f, "list stream"),
|
2022-04-07 04:34:09 +00:00
|
|
|
Type::Any => write!(f, "any"),
|
2021-09-05 23:16:27 +00:00
|
|
|
Type::Error => write!(f, "error"),
|
2021-09-23 16:42:03 +00:00
|
|
|
Type::Binary => write!(f, "binary"),
|
2023-01-30 01:37:54 +00:00
|
|
|
Type::Custom(custom) => write!(f, "{custom}"),
|
2021-12-27 19:13:52 +00:00
|
|
|
Type::Signature => write!(f, "signature"),
|
2021-09-02 01:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 21:55:05 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Type;
|
|
|
|
use strum::IntoEnumIterator;
|
|
|
|
|
|
|
|
mod subtype_relation {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reflexivity() {
|
|
|
|
for ty in Type::iter() {
|
|
|
|
assert!(ty.is_subtype(&ty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_any_is_top_type() {
|
|
|
|
for ty in Type::iter() {
|
|
|
|
assert!(ty.is_subtype(&Type::Any));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_number_supertype() {
|
|
|
|
assert!(Type::Int.is_subtype(&Type::Number));
|
|
|
|
assert!(Type::Float.is_subtype(&Type::Number));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_list_covariance() {
|
|
|
|
for ty1 in Type::iter() {
|
|
|
|
for ty2 in Type::iter() {
|
|
|
|
let list_ty1 = Type::List(Box::new(ty1.clone()));
|
|
|
|
let list_ty2 = Type::List(Box::new(ty2.clone()));
|
|
|
|
assert_eq!(list_ty1.is_subtype(&list_ty2), ty1.is_subtype(&ty2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|