Add example module for documentation

It currently doesn't show anything as all items are pub(crate).
This commit is contained in:
Lukas Kalbertodt
2021-04-29 19:00:36 +02:00
parent 1ae45a3e7c
commit bfab0e8798
5 changed files with 53 additions and 8 deletions

View File

@@ -4,9 +4,17 @@ version = "0.1.0"
authors = ["Lukas Kalbertodt <lukas.kalbertodt@gmail.com>"]
edition = "2018"
[dependencies]
confique-macro = { path = "macro" }
serde = { version = "1", features = ["derive"] }
log = { version = "0.4", features = ["serde"], optional = true }
[dev-dependencies]
log = { version = "0.4", features = ["serde", "std"] }
log = { version = "0.4", features = ["serde"] }
[features]
# This is used to enable the `example` module. This is only useful to generate
# the docs for this library!
doc-example = ["log"]

View File

@@ -10,10 +10,10 @@ mod config {
level: log::LevelFilter = "debug",
/// If this is set, log messages are also written to this file.
#[example = "/var/log/tobira.log"]
#[example = "/var/log/test.log"]
file: Option<PathBuf>,
}
}
},
}

View File

@@ -56,7 +56,7 @@ fn gen_raw_mod(input: &Input, visibility: &TokenStream) -> TokenStream {
quote! {
#name: Some({
let result: Result<_, ::confique::serde::de::value::Error>
let result: Result<_, confique::serde::de::value::Error>
= Deserialize::deserialize(#expr.into_deserializer());
result.expect(#msg)
}),
@@ -83,7 +83,7 @@ fn gen_raw_mod(input: &Input, visibility: &TokenStream) -> TokenStream {
});
contents.extend(quote! {
#[derive(Debug, Default, ::confique::serde::Deserialize)]
#[derive(Debug, Default, confique::serde::Deserialize)]
#[serde(deny_unknown_fields)]
#visibility struct #type_name {
#raw_fields
@@ -116,7 +116,7 @@ fn gen_raw_mod(input: &Input, visibility: &TokenStream) -> TokenStream {
/// These types implement `serde::Deserialize`.
mod raw {
use super::*;
use ::confique::serde::{Deserialize, de::IntoDeserializer};
use confique::serde::{Deserialize, de::IntoDeserializer};
#contents
}
@@ -176,7 +176,7 @@ fn gen_root_mod(input: &Input, visibility: &TokenStream) -> TokenStream {
}
impl std::convert::TryFrom<raw::#type_name> for #type_name {
type Error = ::confique::TryFromError;
type Error = confique::TryFromError;
fn try_from(src: raw::#type_name) -> Result<Self, Self::Error> {
Ok(Self {
#try_from_fields

35
src/example.rs Normal file
View File

@@ -0,0 +1,35 @@
//! This module demonstrate what a `config!` invocation will generate. This
//! module exists merely for documentation purposes and is not usable from your
//! crate.
//!
//! TODO
use std::{net::IpAddr, path::PathBuf};
// This is necessary because the macro generates a bunch of paths starting with
// `confique`, assuming that symbol is in scope.
#[doc(hidden)]
use crate as confique;
crate::config! {
dns: {
/// The DNS server IP address.
#[example = "1.1.1.1"]
server: IpAddr,
/// Whether to use a local DNS resolution cache.
use_cache: bool = true,
/// How often to reattempt reaching the DNS server.
retry_attempts: u32 = 27,
},
log: {
/// Sets the log level. Possible values: "trace", "debug", "info",
/// "warn", "error" and "off".
level: log::LevelFilter = "info",
/// If this is set, log messages are also written to this file.
#[example = "/var/log/test.log"]
file: Option<PathBuf>,
},
}

View File

@@ -1,10 +1,12 @@
use std::fmt;
pub use confique_macro::config as config;
pub use serde;
use std::fmt;
#[cfg(feature = "doc-example")]
pub mod example;
/// Error for the `TryFrom` conversion from raw types to the main types.