From 4b56fba7d6ba4309b68e5f96015eecb642b6af15 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Fri, 5 Jul 2024 05:17:49 -0700 Subject: [PATCH] add datetime expression, remove todo compileerror --- crates/nu-engine/src/compile/builder.rs | 1 + crates/nu-engine/src/compile/expression.rs | 7 +------ crates/nu-engine/src/eval_ir.rs | 1 + crates/nu-protocol/src/errors/compile_error.rs | 8 -------- crates/nu-protocol/src/ir/display.rs | 1 + crates/nu-protocol/src/ir/mod.rs | 2 ++ tests/eval/mod.rs | 5 +++++ 7 files changed, 11 insertions(+), 14 deletions(-) diff --git a/crates/nu-engine/src/compile/builder.rs b/crates/nu-engine/src/compile/builder.rs index b65f5b265f..1120954b8c 100644 --- a/crates/nu-engine/src/compile/builder.rs +++ b/crates/nu-engine/src/compile/builder.rs @@ -137,6 +137,7 @@ impl BlockBuilder { | Literal::String(_) | Literal::RawString(_) | Literal::CellPath(_) + | Literal::Date(_) | Literal::Nothing => (), } } diff --git a/crates/nu-engine/src/compile/expression.rs b/crates/nu-engine/src/compile/expression.rs index 4978cb9a15..91bbf3fbf6 100644 --- a/crates/nu-engine/src/compile/expression.rs +++ b/crates/nu-engine/src/compile/expression.rs @@ -46,11 +46,6 @@ pub(crate) fn compile_expression( builder.load_empty(out_reg) }; - let todo = |msg: &str| CompileError::Todo { - msg: msg.into(), - span: Some(expr.span), - }; - let unexpected = |expr_name: &str| CompileError::UnexpectedExpression { expr_name: expr_name.into(), span: expr.span, @@ -375,7 +370,7 @@ pub(crate) fn compile_expression( Expr::ValueWithUnit(value_with_unit) => { lit(builder, literal_from_value_with_unit(value_with_unit)?) } - Expr::DateTime(_) => Err(todo("DateTime")), + Expr::DateTime(dt) => lit(builder, Literal::Date(Box::new(*dt))), Expr::Filepath(path, no_expand) => { let val = builder.data(path)?; lit( diff --git a/crates/nu-engine/src/eval_ir.rs b/crates/nu-engine/src/eval_ir.rs index 8499b95853..09811f721d 100644 --- a/crates/nu-engine/src/eval_ir.rs +++ b/crates/nu-engine/src/eval_ir.rs @@ -745,6 +745,7 @@ fn literal_value( Literal::String(s) => Value::string(ctx.get_str(*s, span)?, span), Literal::RawString(s) => Value::string(ctx.get_str(*s, span)?, span), Literal::CellPath(path) => Value::cell_path(CellPath::clone(&path), span), + Literal::Date(dt) => Value::date(**dt, span), Literal::Nothing => Value::nothing(span), }) } diff --git a/crates/nu-protocol/src/errors/compile_error.rs b/crates/nu-protocol/src/errors/compile_error.rs index 4810f9f38f..de38709a82 100644 --- a/crates/nu-protocol/src/errors/compile_error.rs +++ b/crates/nu-protocol/src/errors/compile_error.rs @@ -128,12 +128,4 @@ pub enum CompileError { #[label("{msg}")] span: Span, }, - - #[error("TODO: {msg}")] - #[diagnostic(code(nu::compile::todo), help("IR compilation is a work in progress"))] - Todo { - msg: String, - #[label("not yet implemented")] - span: Option, - }, } diff --git a/crates/nu-protocol/src/ir/display.rs b/crates/nu-protocol/src/ir/display.rs index 55b886939b..764b1b758a 100644 --- a/crates/nu-protocol/src/ir/display.rs +++ b/crates/nu-protocol/src/ir/display.rs @@ -323,6 +323,7 @@ impl<'a> fmt::Display for FmtLiteral<'a> { Literal::String(s) => write!(f, "string({})", FmtData(self.data, *s)), Literal::RawString(rs) => write!(f, "raw-string({})", FmtData(self.data, *rs)), Literal::CellPath(p) => write!(f, "cell-path({p})"), + Literal::Date(dt) => write!(f, "date({dt})"), Literal::Nothing => write!(f, "nothing"), } } diff --git a/crates/nu-protocol/src/ir/mod.rs b/crates/nu-protocol/src/ir/mod.rs index 496485eaa5..f44d53d023 100644 --- a/crates/nu-protocol/src/ir/mod.rs +++ b/crates/nu-protocol/src/ir/mod.rs @@ -6,6 +6,7 @@ use crate::{ BlockId, DeclId, RegId, Span, Value, VarId, }; +use chrono::{DateTime, FixedOffset}; use serde::{Deserialize, Serialize}; mod call; @@ -278,6 +279,7 @@ pub enum Literal { String(DataSlice), RawString(DataSlice), CellPath(Box), + Date(Box>), Nothing, } diff --git a/tests/eval/mod.rs b/tests/eval/mod.rs index a6c5b1a1ca..e9afd401e9 100644 --- a/tests/eval/mod.rs +++ b/tests/eval/mod.rs @@ -164,6 +164,11 @@ fn literal_raw_string() { test_eval(r#"r#'bazquux'#"#, Eq("bazquux")) } +#[test] +fn literal_date() { + test_eval("2020-01-01T00:00:00Z", Matches("2020")) +} + #[test] fn literal_nothing() { test_eval("null", Eq(""))