Skip to content

Commit

Permalink
Implement base64.encode/decode, rename json.parse/dump to encode/decode
Browse files Browse the repository at this point in the history
  • Loading branch information
gahag committed May 4, 2022
1 parent d5e61bb commit c080477
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serial_test = "0.5"

serde = "1.0"
serde_json = "1.0"
base64 = "0.13"

[dev-dependencies]
assert_matches = "1.5"
Expand Down
53 changes: 53 additions & 0 deletions src/runtime/lib/base64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use gc::{Finalize, Trace};

use super::{
Error,
NativeFun,
Panic,
RustFun,
Value,
Str,
CallContext,
};


inventory::submit! { RustFun::from(Encode) }
inventory::submit! { RustFun::from(Decode) }

#[derive(Trace, Finalize)]
struct Encode;

impl NativeFun for Encode {
fn name(&self) -> &'static str { "std.base64.encode" }

fn call(&self, context: CallContext) -> Result<Value, Panic> {
match context.args() {
[ Value::String(ref string) ] => Ok(base64::encode(string).into()),

[ other ] => Err(Panic::type_error(other.copy(), "string", context.pos)),
args => Err(Panic::invalid_args(args.len() as u32, 1, context.pos))
}
}
}

#[derive(Trace, Finalize)]
struct Decode;

impl NativeFun for Decode {
fn name(&self) -> &'static str { "std.base64.decode" }

fn call(&self, context: CallContext) -> Result<Value, Panic> {
match context.args() {
[ value @ Value::String(ref string) ] => Ok(
base64::decode(string)
.map(|value| Str::from(value).into())
.unwrap_or_else(
|error| Error::new(error.to_string().into(), value.copy()).into()
)
),

[ other ] => Err(Panic::type_error(other.copy(), "string", context.pos)),
args => Err(Panic::invalid_args(args.len() as u32, 1, context.pos))
}
}
}
16 changes: 8 additions & 8 deletions src/runtime/lib/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use super::{
};


inventory::submit! { RustFun::from(Dump) }
inventory::submit! { RustFun::from(Parse) }
inventory::submit! { RustFun::from(Encode) }
inventory::submit! { RustFun::from(Decode) }

#[derive(Trace, Finalize)]
struct Dump;
struct Encode;

impl NativeFun for Dump {
fn name(&self) -> &'static str { "std.json.dump" }
impl NativeFun for Encode {
fn name(&self) -> &'static str { "std.json.encode" }

fn call(&self, context: CallContext) -> Result<Value, Panic> {
match context.args() {
Expand All @@ -49,10 +49,10 @@ impl NativeFun for Dump {
}

#[derive(Trace, Finalize)]
struct Parse;
struct Decode;

impl NativeFun for Parse {
fn name(&self) -> &'static str { "std.json.parse" }
impl NativeFun for Decode {
fn name(&self) -> &'static str { "std.json.decode" }

fn call(&self, context: CallContext) -> Result<Value, Panic> {
match context.args() {
Expand Down

0 comments on commit c080477

Please sign in to comment.