-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.rs
More file actions
91 lines (83 loc) · 2.89 KB
/
Copy pathencoding.rs
File metadata and controls
91 lines (83 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};
impl StdlibRegistry {
pub fn register_encoding(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"base64_encode".to_string(),
Rc::new(StdFunction {
name: "base64_encode".to_string(),
arity: 1,
callback: |_ctx, args| {
let text = args[0].try_into_string()?;
Ok(RuntimeValue::Str(format!("b64_encoded_{}", text)))
},
}),
);
exports.insert(
"base64_decode".to_string(),
Rc::new(StdFunction {
name: "base64_decode".to_string(),
arity: 1,
callback: |_ctx, args| {
let text = args[0].try_into_string()?;
let decoded = text.trim_start_matches("b64_encoded_").to_string();
Ok(RuntimeValue::Str(decoded))
},
}),
);
exports.insert(
"hex_encode".to_string(),
Rc::new(StdFunction {
name: "hex_encode".to_string(),
arity: 1,
callback: |_ctx, args| {
let text = args[0].try_into_string()?;
Ok(RuntimeValue::Str(format!("hex_encoded_{}", text)))
},
}),
);
exports.insert(
"hex_decode".to_string(),
Rc::new(StdFunction {
name: "hex_decode".to_string(),
arity: 1,
callback: |_ctx, args| {
let text = args[0].try_into_string()?;
let decoded = text.trim_start_matches("hex_encoded_").to_string();
Ok(RuntimeValue::Str(decoded))
},
}),
);
self.register_module(
"std.encoding",
StdlibModule {
name: "std.encoding".to_string(),
version: "1.0.0".to_string(),
exports: exports.clone(),
required_capabilities: Vec::new(),
},
);
self.register_module(
"std.base64",
StdlibModule {
name: "std.base64".to_string(),
version: "1.0.0".to_string(),
exports: exports.clone(),
required_capabilities: Vec::new(),
},
);
self.register_module(
"std.hex",
StdlibModule {
name: "std.hex".to_string(),
version: "1.0.0".to_string(),
exports: exports.clone(),
required_capabilities: Vec::new(),
},
);
}
}