nushell/crates/nu-protocol/src/ty.rs

103 lines
3.3 KiB
Rust
Raw Normal View History

2021-10-01 07:11:49 +02:00
use serde::{Deserialize, Serialize};
2021-09-02 03:29:43 +02:00
use std::fmt::Display;
2022-03-07 21:08:56 +01:00
use crate::SyntaxShape;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
2021-09-02 03:29:43 +02:00
pub enum Type {
Int,
Float,
Range,
2021-09-02 03:29:43 +02:00
Bool,
String,
Block,
2021-09-07 00:02:24 +02:00
CellPath,
2021-09-02 03:29:43 +02:00
Duration,
2021-10-05 04:27:39 +02:00
Date,
2021-09-02 03:29:43 +02:00
Filesize,
List(Box<Type>),
Number,
Nothing,
2021-11-19 05:30:27 +01:00
Record(Vec<(String, Type)>),
Table(Vec<(String, Type)>),
ListStream,
Any,
2021-09-06 01:16:27 +02:00
Error,
2021-09-23 18:42:03 +02:00
Binary,
Custom(String),
Signature,
2021-09-02 03:29:43 +02:00
}
2022-03-07 21:08:56 +01: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,
Type::Table(_) => SyntaxShape::Table,
2022-03-07 21:08:56 +01:00
Type::ListStream => SyntaxShape::List(Box::new(SyntaxShape::Any)),
Type::Any => SyntaxShape::Any,
2022-03-07 21:08:56 +01:00
Type::Error => SyntaxShape::Any,
Type::Binary => SyntaxShape::Binary,
Type::Custom(_) => SyntaxShape::Any,
2022-03-07 21:08:56 +01:00
Type::Signature => SyntaxShape::Signature,
}
}
}
2021-09-02 03:29:43 +02: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-07 00:02:24 +02:00
Type::CellPath => write!(f, "cell path"),
2021-10-05 04:27:39 +02:00
Type::Date => write!(f, "date"),
2021-09-02 03:29:43 +02:00
Type::Duration => write!(f, "duration"),
Type::Filesize => write!(f, "filesize"),
Type::Float => write!(f, "float"),
Type::Int => write!(f, "int"),
Type::Range => write!(f, "range"),
2021-11-19 05:30:27 +01:00
Type::Record(fields) => write!(
f,
"record<{}>",
fields
.iter()
.map(|(x, y)| format!("{}: {}", x, y))
2021-11-19 05:30:27 +01:00
.collect::<Vec<String>>()
.join(", "),
),
Type::Table(columns) => write!(
f,
"table<{}>",
columns
.iter()
.map(|(x, y)| format!("{}: {}", x, y))
.collect::<Vec<String>>()
.join(", ")
),
2021-09-02 03:29:43 +02:00
Type::List(l) => write!(f, "list<{}>", l),
Type::Nothing => write!(f, "nothing"),
Type::Number => write!(f, "number"),
Type::String => write!(f, "string"),
Type::ListStream => write!(f, "list stream"),
Type::Any => write!(f, "any"),
2021-09-06 01:16:27 +02:00
Type::Error => write!(f, "error"),
2021-09-23 18:42:03 +02:00
Type::Binary => write!(f, "binary"),
Type::Custom(custom) => write!(f, "{}", custom),
Type::Signature => write!(f, "signature"),
2021-09-02 03:29:43 +02:00
}
}
}