Include documentation in printed config files (#41)

* Start work on printing toml config with comments

* WIP: toml_config: extract default values for fields

* WIP: toml_config: handle single-level nested structs

* WIP: toml_config: improve comment handling, std type trait impls

* WIP: toml_config: add Private trait, improve comment handling, clean up

* toml_config: fix default value bug; improve tests

* Use toml_config in all applicable crates; add toml_config enum support

* toml_config: improve comments

* toml_config_derive: support enum comments

* Improve config comments for udp, cli_helpers, common

* Improve config comments

* Add tests for Config struct TomlConfig implementations

* Improve Config comments

* Improve Config comments

* ws, http: add config comments for tls cert and private key lines

* small fixes to toml_config and toml_config_derive

* Run cargo fmt

* Fix typo in several config comments

* Update README

* Update README
This commit is contained in:
Joakim Frostegård 2021-12-26 11:33:27 +01:00 committed by GitHub
parent d694785244
commit a208775104
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 581 additions and 92 deletions

126
toml_config/src/lib.rs Normal file
View file

@ -0,0 +1,126 @@
pub use toml;
pub use toml_config_derive::TomlConfig;
/// Run this on your struct implementing TomlConfig to generate a
/// serialization/deserialization test for it.
#[macro_export]
macro_rules! gen_serialize_deserialize_test {
($ident:ident) => {
#[test]
fn test_cargo_toml_serialize_deserialize() {
use ::toml_config::TomlConfig;
let serialized = $ident::default_to_string();
let deserialized = ::toml_config::toml::de::from_str(&serialized).unwrap();
assert_eq!($ident::default(), deserialized);
}
};
}
/// Export structs to toml, converting Rust doc strings to comments.
///
/// Supports one level of nesting. Fields containing structs must come
/// after regular fields.
///
/// Usage:
/// ```
/// use toml_config::TomlConfig;
///
/// #[derive(TomlConfig)]
/// struct SubConfig {
/// /// A
/// a: usize,
/// /// B
/// b: String,
/// }
///
/// impl Default for SubConfig {
/// fn default() -> Self {
/// Self {
/// a: 200,
/// b: "subconfig hello".into(),
/// }
/// }
/// }
///
/// #[derive(TomlConfig)]
/// struct Config {
/// /// A
/// a: usize,
/// /// B
/// b: String,
/// /// C
/// c: SubConfig,
/// }
///
/// impl Default for Config {
/// fn default() -> Self {
/// Self {
/// a: 100,
/// b: "hello".into(),
/// c: Default::default(),
/// }
/// }
/// }
///
/// let expected = "# A\na = 100\n# B\nb = \"hello\"\n\n# C\n[c]\n# A\na = 200\n# B\nb = \"subconfig hello\"\n";
///
/// assert_eq!(
/// Config::default_to_string(),
/// expected,
/// );
/// ```
pub trait TomlConfig: Default {
fn default_to_string() -> String;
}
pub mod __private {
use std::net::SocketAddr;
use std::path::PathBuf;
pub trait Private {
fn __to_string(&self, comment: Option<String>, field_name: String) -> String;
}
macro_rules! impl_trait {
($ident:ident) => {
impl Private for $ident {
fn __to_string(&self, comment: Option<String>, field_name: String) -> String {
let mut output = String::new();
if let Some(comment) = comment {
output.push_str(&comment);
}
let value = crate::toml::ser::to_string(self).unwrap();
output.push_str(&format!("{} = {}\n", field_name, value));
output
}
}
};
}
impl_trait!(isize);
impl_trait!(i8);
impl_trait!(i16);
impl_trait!(i32);
impl_trait!(i64);
impl_trait!(usize);
impl_trait!(u8);
impl_trait!(u16);
impl_trait!(u32);
impl_trait!(u64);
impl_trait!(f32);
impl_trait!(f64);
impl_trait!(bool);
impl_trait!(String);
impl_trait!(PathBuf);
impl_trait!(SocketAddr);
}