Skip to content

Commit

Permalink
Fixed variable type changing due to serialization (#68)
Browse files Browse the repository at this point in the history
* Fixed variable type changing due to serialization

* Fixed linter errors
  • Loading branch information
clarkmcc authored Jul 12, 2024
1 parent 2cb75cb commit 1ac36b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
14 changes: 14 additions & 0 deletions interpreter/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ fn _timestamp(i: &str) -> Result<DateTime<FixedOffset>> {
mod tests {
use crate::context::Context;
use crate::testing::test_script;
use crate::{Program, Value};
use chrono::{DateTime, FixedOffset};
use std::collections::HashMap;

fn assert_script(input: &(&str, &str)) {
Expand Down Expand Up @@ -721,6 +723,18 @@ mod tests {
.for_each(assert_script);
}

#[test]
fn test_timestamp_variable() {
let mut context = Context::default();
let ts: DateTime<FixedOffset> =
DateTime::parse_from_rfc3339("2023-05-29T00:00:00Z").unwrap();
context.add_variable("ts", Value::Timestamp(ts)).unwrap();

let program = Program::compile("ts == timestamp('2023-05-29T00:00:00Z')").unwrap();
let result = program.execute(&context).unwrap();
assert_eq!(result, true.into());
}

#[test]
fn test_string() {
[
Expand Down
18 changes: 8 additions & 10 deletions interpreter/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::ops;
use serde::{Serialize, Serializer};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::convert::{Infallible, TryFrom, TryInto};
use std::fmt::{Display, Formatter};
use std::sync::Arc;

Expand Down Expand Up @@ -130,15 +130,6 @@ impl<K: Into<Key>, V: Into<Value>> From<HashMap<K, V>> for Map {
}
}

impl Serialize for &Map {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.map.as_ref().serialize(serializer)
}
}

pub trait TryIntoValue {
type Error: std::error::Error + 'static;
fn try_into_value(self) -> Result<Value, Self::Error>;
Expand All @@ -151,6 +142,13 @@ impl<T: Serialize> TryIntoValue for T {
}
}

impl TryIntoValue for Value {
type Error = Infallible;
fn try_into_value(self) -> Result<Value, Self::Error> {
Ok(self)
}
}

#[derive(Debug, Clone)]
pub enum Value {
List(Arc<Vec<Value>>),
Expand Down
28 changes: 1 addition & 27 deletions interpreter/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
// also mentioned in the [serde documentation](https://serde.rs/).

use crate::{objects::Key, Value};
use serde::ser::Error as SerdeError;
use serde::{
ser::{self, Impossible},
Serialize, Serializer as SerdeSerializer,
Serialize,
};
use std::{collections::HashMap, fmt::Display, iter::FromIterator, sync::Arc};
use thiserror::Error;
Expand Down Expand Up @@ -561,31 +560,6 @@ impl ser::Serializer for KeySerializer {
}
}

impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: SerdeSerializer,
{
match *self {
Value::List(ref vec) => vec.as_ref().serialize(serializer),
Value::Map(ref map) => map.serialize(serializer),
Value::Int(i) => serializer.serialize_i64(i),
Value::UInt(u) => serializer.serialize_u64(u),
Value::Float(f) => serializer.serialize_f64(f),
Value::String(ref s) => serializer.serialize_str(s),
Value::Bool(b) => serializer.serialize_bool(b),
Value::Timestamp(ref dt) => serializer.serialize_str(&dt.to_rfc3339()),
Value::Bytes(ref b) => serializer.serialize_bytes(b),
Value::Null => serializer.serialize_unit(),
_ => Err(SerdeError::custom(format!("Unsupported value: {:?}", self))),
}
}
}

/*
* Tests
*/

#[cfg(test)]
mod tests {
use crate::{objects::Key, to_value, Value};
Expand Down

0 comments on commit 1ac36b9

Please sign in to comment.