Skip to main content

ragit/schema/
model.rs

1use super::Prettify;
2use crate::error::Error;
3use ragit_api::{Model, ModelRaw};
4use serde_json::Value;
5
6pub type ModelSchema = Model;
7
8impl Prettify for ModelSchema {
9    fn prettify(&self) -> Result<Value, Error> {
10        let result = ModelRaw::from(self);
11        let mut result = serde_json::to_value(result)?;
12
13        if let Value::Object(obj) = &mut result {
14            match obj.get_mut("api_key") {
15                Some(Value::String(key)) => {
16                    let chars_count = key.chars().count();
17
18                    if chars_count > 8 {
19                        *key = format!(
20                            "{}{}",
21                            key.chars().take(4).collect::<String>(),
22                            "*".repeat(chars_count - 4),
23                        );
24                    }
25
26                    else {
27                        *key = String::from("*".repeat(chars_count));
28                    }
29                },
30                _ => {},
31            }
32
33            for key in [
34                "api_timeout",
35                "explanation",
36                "api_key",
37                "api_env_var",
38            ] {
39                if let Some(Value::Null) = obj.get(key) {
40                    obj.remove(key);
41                }
42            }
43        }
44
45        Ok(result)
46    }
47}