-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.rs
More file actions
65 lines (61 loc) · 2.38 KB
/
Copy pathxml.rs
File metadata and controls
65 lines (61 loc) · 2.38 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
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};
impl StdlibRegistry {
pub fn register_xml(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"parse".to_string(),
Rc::new(StdFunction {
name: "parse".to_string(),
arity: 1,
callback: |_ctx, args| {
let xml = args[0].try_into_string()?;
let mut map = IndexMap::new();
if xml.starts_with('<') && xml.contains('>') {
let tag_name = xml[1..xml.find('>').unwrap_or(1)].to_string();
let close_tag = format!("</{}>", tag_name);
if let Some(close_pos) = xml.find(&close_tag) {
let content = xml[xml.find('>').unwrap() + 1..close_pos].to_string();
map.insert(tag_name, RuntimeValue::Str(content));
}
}
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(map)),
is_const: false,
})
},
}),
);
exports.insert(
"stringify".to_string(),
Rc::new(StdFunction {
name: "stringify".to_string(),
arity: 1,
callback: |_ctx, args| {
let mut result = String::new();
if let RuntimeValue::Map { entries, .. } = &args[0] {
for (k, v) in entries.borrow().iter() {
let val_str = v.try_into_string().unwrap_or_default();
result.push_str(&format!("<{}>{}</{}>", k, val_str, k));
}
}
Ok(RuntimeValue::Str(result))
},
}),
);
self.register_module(
"std.xml",
StdlibModule {
name: "std.xml".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
}