use std::fmt;
use serde::Serialize;
use crate::output::Render;
#[derive(Clone, PartialEq, Eq, Serialize)]
pub struct SettingsAnswer {
pub kind: &'static str,
pub name: String,
pub path: String,
pub toml: String,
}
impl fmt::Debug for SettingsAnswer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"SettingsAnswer {{ kind: {:?}, name: {:?}, path: {:?}, toml: <{} bytes> }}",
self.kind,
self.name,
self.path,
self.toml.len()
)
}
}
impl Render for SettingsAnswer {
fn headers() -> &'static [&'static str] {
&["NAME", "KIND", "PATH"]
}
fn rows(&self) -> Vec<Vec<String>> {
vec![vec![
self.name.clone(),
self.kind.to_string(),
self.path.clone(),
]]
}
const JSON_ONLY: &'static [&'static str] = &["toml"];
const PRIORITIES: &'static [u8] = &[0, 6, 6];
}
#[cfg(test)]
mod tests {
use super::super::tests::assert_no_drift;
use super::*;
#[test]
fn settings_answer_matches_its_json_keys() {
let answer = SettingsAnswer {
kind: "sheep",
name: "api".to_string(),
path: "max_memory".to_string(),
toml: "max_memory = 1073741824\n".to_string(),
};
assert_no_drift(&answer, |json| json, &[]);
}
#[test]
fn settings_answer_debug_prints_a_byte_count_and_never_the_toml() {
let answer = SettingsAnswer {
kind: "dog",
name: "kelpie".to_string(),
path: "worker".to_string(),
toml: "[worker]\ntoken = \"hunter2\"\n".to_string(),
};
assert_eq!(
format!("{answer:?}"),
"SettingsAnswer { kind: \"dog\", name: \"kelpie\", path: \"worker\", toml: <27 bytes> }"
);
}
}