fastn_jdebug/
lib.rs

1#![allow(clippy::derive_partial_eq_without_eq, clippy::get_first)]
2#![deny(unused_crate_dependencies)]
3#![warn(clippy::used_underscore_binding)]
4
5extern crate self as fastn_jdebug;
6
7pub trait JDebug {
8    fn debug(&self, source: &str) -> serde_json::Value;
9}
10
11impl<T: fastn_jdebug::JDebug> fastn_jdebug::JDebug for Vec<T> {
12    fn debug(&self, source: &str) -> serde_json::Value {
13        serde_json::Value::Array(self.iter().map(|v| v.debug(source)).collect())
14    }
15}
16
17impl<T: fastn_jdebug::JDebug> fastn_jdebug::JDebug for Option<T> {
18    fn debug(&self, source: &str) -> serde_json::Value {
19        self.as_ref()
20            .map(|v| v.debug(source))
21            .unwrap_or(serde_json::Value::Null)
22    }
23}
24
25impl<K: AsRef<std::ops::Range<usize>>, V: fastn_jdebug::JDebug> fastn_jdebug::JDebug
26    for std::collections::HashMap<K, V>
27{
28    fn debug(&self, source: &str) -> serde_json::Value {
29        let mut o = serde_json::Map::new();
30        for (k, v) in self {
31            let r = k.as_ref();
32            o.insert(source[r.start..r.end].to_string(), v.debug(source));
33        }
34        serde_json::Value::Object(o)
35    }
36}