-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee0c71c
commit 7895378
Showing
11 changed files
with
223 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use rust_decimal::Decimal; | ||
|
||
use crate::{cel_type::*, error::*, value::*}; | ||
|
||
use super::assert_arg; | ||
|
||
pub fn cast(args: Vec<CelValue>) -> Result<CelValue, CelError> { | ||
match args.first() { | ||
Some(CelValue::Decimal(d)) => Ok(CelValue::Decimal(*d)), | ||
Some(CelValue::String(s)) => Ok(CelValue::Decimal( | ||
s.parse() | ||
.map_err(|e| CelError::DecimalError(format!("{e:?}")))?, | ||
)), | ||
Some(v) => Err(CelError::BadType(CelType::Decimal, CelType::from(v))), | ||
None => Err(CelError::MissingArgument), | ||
} | ||
} | ||
|
||
pub fn add(args: Vec<CelValue>) -> Result<CelValue, CelError> { | ||
let a: &Decimal = assert_arg(args.first())?; | ||
let b: &Decimal = assert_arg(args.get(1))?; | ||
Ok(CelValue::Decimal(a + b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::{cel_type::*, error::*, value::*}; | ||
|
||
use super::assert_arg; | ||
|
||
pub fn cast(args: Vec<CelValue>) -> Result<CelValue, CelError> { | ||
match args.first() { | ||
Some(CelValue::String(s)) => Ok(CelValue::Timestamp( | ||
s.parse() | ||
.map_err(|e| CelError::TimestampError(format!("{e:?}")))?, | ||
)), | ||
Some(v) => Err(CelError::BadType(CelType::Timestamp, CelType::from(v))), | ||
None => Err(CelError::MissingArgument), | ||
} | ||
} | ||
|
||
pub fn format(target: &CelValue, args: Vec<CelValue>) -> Result<CelValue, CelError> { | ||
if let CelValue::Timestamp(ts) = target { | ||
let format: std::sync::Arc<String> = assert_arg(args.first())?; | ||
Ok(CelValue::String(ts.format(&format).to_string().into())) | ||
} else { | ||
Err(CelError::BadType(CelType::Timestamp, CelType::from(target))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use super::*; | ||
|
||
pub struct CelPackage { | ||
nested_ctx: CelContext, | ||
member_fns: HashMap<&'static str, CelMemberFunction>, | ||
} | ||
|
||
impl CelPackage { | ||
pub fn new( | ||
nested_ctx: CelContext, | ||
member_fns: HashMap<&'static str, CelMemberFunction>, | ||
) -> Self { | ||
Self { | ||
nested_ctx, | ||
member_fns, | ||
} | ||
} | ||
|
||
pub(crate) fn package_self(&self) -> Result<&ContextItem, CelError> { | ||
self.nested_ctx.lookup_ident(&SELF_PACKAGE_NAME) | ||
} | ||
|
||
pub(crate) fn lookup(&self, name: &str) -> Result<&ContextItem, CelError> { | ||
self.nested_ctx.lookup_ident(name) | ||
} | ||
|
||
pub(crate) fn lookup_member( | ||
&self, | ||
value: &CelValue, | ||
name: &str, | ||
) -> Result<&CelMemberFunction, CelError> { | ||
self.member_fns | ||
.get(name) | ||
.ok_or_else(|| CelError::UnknownAttribute(CelType::from(value), name.to_string())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use lazy_static::lazy_static; | ||
|
||
use std::collections::HashMap; | ||
|
||
use crate::builtins; | ||
|
||
use super::*; | ||
|
||
lazy_static! { | ||
pub static ref CEL_PACKAGE: CelPackage = { | ||
let mut idents = HashMap::new(); | ||
idents.insert( | ||
SELF_PACKAGE_NAME, | ||
ContextItem::Function(Box::new(builtins::timestamp::cast)), | ||
); | ||
|
||
let mut member_fns: HashMap<_, CelMemberFunction> = HashMap::new(); | ||
member_fns.insert("format", Box::new(builtins::timestamp::format)); | ||
|
||
CelPackage::new(CelContext { idents }, member_fns) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.