summaryrefslogtreecommitdiff
path: root/core/modules.rs
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules.rs')
-rw-r--r--core/modules.rs60
1 files changed, 30 insertions, 30 deletions
diff --git a/core/modules.rs b/core/modules.rs
index ca850d0bb..58ad767b9 100644
--- a/core/modules.rs
+++ b/core/modules.rs
@@ -484,20 +484,14 @@ impl Deps {
}
}
- pub fn to_json(&self) -> String {
- let mut children = "[".to_string();
-
- if let Some(ref deps) = self.deps {
- for d in deps {
- children.push_str(&d.to_json());
- if !d.is_last {
- children.push_str(",");
- }
- }
+ pub fn to_json(&self) -> serde_json::Value {
+ let children;
+ if let Some(deps) = &self.deps {
+ children = deps.iter().map(|c| c.to_json()).collect();
+ } else {
+ children = Vec::new()
}
- children.push_str("]");
-
- format!("[\"{}\",{}]", self.name, children)
+ serde_json::json!([&self.name, children])
}
}
@@ -1056,6 +1050,29 @@ mod tests {
assert!(modules.deps(&specifier).is_none());
}
+ #[test]
+ fn deps_to_json() {
+ fn dep(name: &str, deps: Option<Vec<Deps>>) -> Deps {
+ Deps {
+ name: name.to_string(),
+ deps,
+ prefix: "".to_string(),
+ is_last: false,
+ }
+ }
+ let deps = dep(
+ "a",
+ Some(vec![
+ dep("b", Some(vec![dep("b2", None)])),
+ dep("c", Some(vec![])),
+ ]),
+ );
+ assert_eq!(
+ serde_json::json!(["a", [["b", [["b2", []]]], ["c", []]]]),
+ deps.to_json()
+ );
+ }
+
/* TODO(bartlomieju): reenable
#[test]
fn deps() {
@@ -1076,22 +1093,5 @@ mod tests {
assert_eq!(bar_deps.deps, Some(vec![]));
}
- #[test]
- fn test_deps_to_json() {
- let mut modules = Modules::new();
- modules.register(1, "foo");
- modules.register(2, "bar");
- modules.register(3, "baz");
- modules.register(4, "zuh");
- modules.add_child(1, "bar");
- modules.add_child(1, "baz");
- modules.add_child(3, "zuh");
- let maybe_deps = modules.deps("foo");
- assert!(maybe_deps.is_some());
- assert_eq!(
- "[\"foo\",[[\"bar\",[]],[\"baz\",[[\"zuh\",[]]]]]]",
- maybe_deps.unwrap().to_json()
- );
- }
*/
}