Repo of no-std crates for my personal embedded projects

feat: Config format tweak with stronger type validation #8

merged opened by sachy.dev targeting main from format-validate

Fixes the config format to require values to be strings, so to avoid problems with toml Value not being able to accommodate certain types, but then adds parsing according to the declared type to ensure the values are valid before being formatted into constants.

Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:usjm3ynnir6y4inkcdovrfei/sh.tangled.repo.pull/3md646poqio22
+14 -1
Interdiff #0 #1
.tangled/workflows/test.yml

This file has not been changed.

+14 -1
sachy-config/src/lib.rs
··· 91 "i128" => validate_kind_to_string::<i128>(name, kind, value)?, 92 "f32" => validate_kind_to_string::<f32>(name, kind, value)?, 93 "f64" => validate_kind_to_string::<f64>(name, kind, value)?, 94 - "&str" => bail!("{} is a string, use [statics] for these", kind), 95 _ => bail!("Unsupported type: {}", kind), 96 }; 97 ··· 155 assert!(error_chain.next().is_none()); 156 157 Ok(()) 158 } 159 160 #[test]
··· 91 "i128" => validate_kind_to_string::<i128>(name, kind, value)?, 92 "f32" => validate_kind_to_string::<f32>(name, kind, value)?, 93 "f64" => validate_kind_to_string::<f64>(name, kind, value)?, 94 + "&str" => bail!("{} is a {}, use [statics] for these", name, kind), 95 _ => bail!("Unsupported type: {}", kind), 96 }; 97 ··· 155 assert!(error_chain.next().is_none()); 156 157 Ok(()) 158 + } 159 + 160 + #[test] 161 + fn it_disallows_string_constants() { 162 + let input = r#"[constants] 163 + FIRST = { type = "&str", value = "42" } 164 + "#; 165 + 166 + let expected = "FIRST is a &str, use [statics] for these"; 167 + 168 + let output = parse_config(input).expect_err("Output somehow successfully parsed"); 169 + 170 + assert_eq!(output.to_string(), expected); 171 } 172 173 #[test]

History

2 rounds 0 comments
sign up or login to add to the discussion
sachy.dev submitted #1
1 commit
expand
feat: Config format tweak with stronger type validation
2/2 success
expand
expand 0 comments
pull request successfully merged
sachy.dev submitted #0
1 commit
expand
feat: Config format tweak with stronger type validation
2/2 success
expand
expand 0 comments