Skip to content

Commit

Permalink
added to_json for return result
Browse files Browse the repository at this point in the history
  • Loading branch information
andthattoo committed Jul 22, 2024
1 parent 430fba3 commit c448a15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/memory/types.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use serde::{Deserialize, Serialize};
use std::fmt;

pub type ID = String;
pub type StackPage = Vec<Entry>;
pub type FilePage = (String, Vec<f32>);

/// Entry is an enum that can be either a String or a Json Value.
/// It is used for I/O operations in the memory module.
#[derive(Debug, serde::Deserialize, PartialEq)]
#[derive(Debug, Serialize, serde::Deserialize, PartialEq)]
pub enum Entry {
String(String),
Json(serde_json::Value),
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Clone for Entry {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryReturnType {
//<'a>
//EntryRef(Option<&'a Entry>),
Expand All @@ -55,6 +55,10 @@ impl MemoryReturnType {
MemoryReturnType::EntryVec(entry_vec) => entry_vec.is_none(),
}
}

pub fn to_json(&self) -> serde_json::Result<String> {
serde_json::to_string(&self)
}
}

impl fmt::Display for MemoryReturnType {
Expand Down
7 changes: 7 additions & 0 deletions src/program/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ impl Executor {
};
}
}

if rv.to_json.is_some() && rv.to_json.unwrap() {
let res = return_value.to_json();
if res.is_ok() {
return_string = res.unwrap();
}
}
return_string
}

Expand Down

0 comments on commit c448a15

Please sign in to comment.