2021-10-01 05:11:49 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-09-02 01:29:43 +00:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2022-03-07 20:08:56 +00:00
|
|
|
use crate::SyntaxShape;
|
|
|
|
|
2022-06-10 15:59:35 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
|
2021-09-02 01:29:43 +00:00
|
|
|
pub enum Type {
|
|
|
|
Int,
|
|
|
|
Float,
|
2021-09-04 21:52:57 +00:00
|
|
|
Range,
|
2021-09-02 01:29:43 +00:00
|
|
|
Bool,
|
|
|
|
String,
|
|
|
|
Block,
|
2021-09-06 22:02:24 +00:00
|
|
|
CellPath,
|
2021-09-02 01:29:43 +00:00
|
|
|
Duration,
|
2021-10-05 02:27:39 +00:00
|
|
|
Date,
|
2021-09-02 01:29:43 +00:00
|
|
|
Filesize,
|
|
|
|
List(Box<Type>),
|
|
|
|
Number,
|
|
|
|
Nothing,
|
2021-11-19 04:30:27 +00:00
|
|
|
Record(Vec<(String, Type)>),
|
2022-04-07 04:34:09 +00:00
|
|
|
Table(Vec<(String, Type)>),
|
2022-02-24 11:57:31 +00:00
|
|
|
ListStream,
|
2022-04-07 04:34:09 +00:00
|
|
|
Any,
|
2021-09-05 23:16:27 +00:00
|
|
|
Error,
|
2021-09-23 16:42:03 +00:00
|
|
|
Binary,
|
2022-06-10 15:59:35 +00:00
|
|
|
Custom(String),
|
2021-12-27 19:13:52 +00:00
|
|
|
Signature,
|
2021-09-02 01:29:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 20:08:56 +00:00
|
|
|
impl Type {
|
|
|
|
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,
|
|
|
|
Type::Block => SyntaxShape::Block(None), // FIXME needs more accuracy
|
|
|
|
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,
|
|
|
|
Type::Nothing => SyntaxShape::Any,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"),
|
|
|
|
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"),
|
2021-11-19 04:30:27 +00:00
|
|
|
Type::Record(fields) => write!(
|
|
|
|
f,
|
|
|
|
"record<{}>",
|
|
|
|
fields
|
|
|
|
.iter()
|
2022-01-13 19:40:25 +00:00
|
|
|
.map(|(x, y)| format!("{}: {}", x, y))
|
2021-11-19 04:30:27 +00:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", "),
|
|
|
|
),
|
2022-04-07 04:34:09 +00:00
|
|
|
Type::Table(columns) => write!(
|
|
|
|
f,
|
|
|
|
"table<{}>",
|
|
|
|
columns
|
|
|
|
.iter()
|
|
|
|
.map(|(x, y)| format!("{}: {}", x, y))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
),
|
2021-09-02 01:29:43 +00:00
|
|
|
Type::List(l) => write!(f, "list<{}>", l),
|
|
|
|
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"),
|
2022-06-12 19:18:00 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|