forked from hush-shell/hush
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement base64.encode/decode, rename json.parse/dump to encode/decode
- Loading branch information
Showing
4 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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)) | ||
} | ||
} | ||
} |
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