Guess it's working now somehow

This commit is contained in:
Daniel Flanagan 2024-04-12 09:45:30 -05:00
parent c935d725aa
commit 427afa921d

View file

@ -25,9 +25,6 @@ pub struct Config {
pub version: u64, pub version: u64,
pub discord: Option<Discord>, pub discord: Option<Discord>,
pub open_ai: Option<OpenAI>, pub open_ai: Option<OpenAI>,
#[serde(serialize_with = "redact_secret")]
pub secret: Secret<String>,
} }
impl Config { impl Config {
@ -48,10 +45,10 @@ impl Config {
Ok(CConfig::builder() Ok(CConfig::builder()
.set_default("version", CURRENT_VERSION)? .set_default("version", CURRENT_VERSION)?
.set_default("discord", Option::<HashMap<String, config::Value>>::None)? .set_default("discord", Option::<HashMap<String, config::Value>>::None)?
.set_default("open_ai", Option::<HashMap<String, config::Value>>::None)? .set_default("open_ai", Option::<HashMap<String, config::Value>>::None)?)
.set_default("secret", "this is a secret!")?)
} }
#[allow(dead_code)]
pub fn defaults() -> Result<Self> { pub fn defaults() -> Result<Self> {
let c = Self::default_builder()?.build()?; let c = Self::default_builder()?.build()?;
Ok(c.try_deserialize()?) Ok(c.try_deserialize()?)
@ -60,17 +57,33 @@ impl Config {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::prelude::*; use crate::config::*;
#[test]
fn defaults_deserialize_to_valid_config() -> Result<()> {
println!("Defaults: {:?}", Config::defaults()?);
Ok(())
}
#[test] #[test]
fn config_does_not_leak_secrets() -> Result<()> { fn config_does_not_leak_secrets() -> Result<()> {
let defaults = Config::default_builder()? let conf: Config = match Config::default_builder()?
.set_override("discord.bot_token", Secret::new("THIS_IS_MY_BOT_TOKEN"))? // .set_override("discord", HashMap::<String, config::Value>::new())?
.build()?; .set_override("discord.bot_token", "THIS_IS_MY_BOT_TOKEN")?
let s = format!("{defaults:?}"); .set_override("version", 1337)?
.build()?
.try_deserialize()
{
Ok(conf) => conf,
Err(err) => {
println!("{err:#?}");
return Err(err.into());
}
};
let s = format!("{conf:?}");
println!("{s}"); println!("{s}");
assert!(s.contains("1337"));
assert!(!s.contains("THIS_IS_MY_BOT_TOKEN")); assert!(!s.contains("THIS_IS_MY_BOT_TOKEN"));
assert!(!s.contains("this is a secret"));
Ok(()) Ok(())
} }
} }