mirror of
https://github.com/OMGeeky/confique.git
synced 2026-01-07 04:01:26 +01:00
Rename format to template (except template::format())
This commit is contained in:
24
src/json5.rs
24
src/json5.rs
@@ -5,19 +5,19 @@ use std::fmt::{self, Write};
|
||||
|
||||
use crate::{
|
||||
Config,
|
||||
format::{self, Formatter},
|
||||
template::{self, Formatter},
|
||||
meta::Expr,
|
||||
};
|
||||
|
||||
|
||||
|
||||
/// Options for generating a TOML template.
|
||||
/// Options for generating a JSON5 template.
|
||||
pub struct FormatOptions {
|
||||
/// Indentation per level. Default: 2.
|
||||
pub indent: u8,
|
||||
|
||||
/// Non-JSON5 specific options.
|
||||
general: format::Options,
|
||||
general: template::Options,
|
||||
}
|
||||
|
||||
impl Default for FormatOptions {
|
||||
@@ -29,7 +29,7 @@ impl Default for FormatOptions {
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats the configuration description as a TOML file.
|
||||
/// Formats the configuration description as a JSON5 file.
|
||||
///
|
||||
/// This can be used to generate a template file that you can give to the users
|
||||
/// of your application. It usually is a convenient to start with a correctly
|
||||
@@ -88,13 +88,13 @@ impl Default for FormatOptions {
|
||||
/// ";
|
||||
///
|
||||
/// fn main() {
|
||||
/// let json5 = confique::json5::format::<Conf>(FormatOptions::default());
|
||||
/// let json5 = confique::json5::template::<Conf>(FormatOptions::default());
|
||||
/// assert_eq!(json5, EXPECTED);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn format<C: Config>(options: FormatOptions) -> String {
|
||||
pub fn template<C: Config>(options: FormatOptions) -> String {
|
||||
let mut out = Json5Formatter::new(&options);
|
||||
format::format::<C>(&mut out, options.general);
|
||||
template::format::<C>(&mut out, options.general);
|
||||
out.finish()
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ impl Formatter for Json5Formatter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to emit `meta::Expr` into TOML.
|
||||
/// Helper to emit `meta::Expr` into JSON5.
|
||||
struct PrintExpr(&'static Expr);
|
||||
|
||||
impl From<&'static Expr> for PrintExpr {
|
||||
@@ -191,12 +191,12 @@ impl fmt::Display for PrintExpr {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test_utils::{self, include_format_output};
|
||||
use super::{format, FormatOptions};
|
||||
use super::{template, FormatOptions};
|
||||
use pretty_assertions::assert_str_eq;
|
||||
|
||||
#[test]
|
||||
fn default() {
|
||||
let out = format::<test_utils::example1::Conf>(FormatOptions::default());
|
||||
let out = template::<test_utils::example1::Conf>(FormatOptions::default());
|
||||
assert_str_eq!(&out, include_format_output!("1-default.json5"));
|
||||
}
|
||||
|
||||
@@ -204,13 +204,13 @@ mod tests {
|
||||
fn no_comments() {
|
||||
let mut options = FormatOptions::default();
|
||||
options.general.comments = false;
|
||||
let out = format::<test_utils::example1::Conf>(options);
|
||||
let out = template::<test_utils::example1::Conf>(options);
|
||||
assert_str_eq!(&out, include_format_output!("1-no-comments.json5"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn immediately_nested() {
|
||||
let out = format::<test_utils::example2::Conf>(Default::default());
|
||||
let out = template::<test_utils::example2::Conf>(Default::default());
|
||||
assert_str_eq!(&out, include_format_output!("2-default.json5"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,10 +176,10 @@ mod error;
|
||||
pub mod meta;
|
||||
|
||||
#[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))]
|
||||
mod format;
|
||||
mod file;
|
||||
|
||||
#[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))]
|
||||
mod file;
|
||||
mod template;
|
||||
|
||||
#[cfg(feature = "json5")]
|
||||
pub mod json5;
|
||||
|
||||
22
src/toml.rs
22
src/toml.rs
@@ -5,7 +5,7 @@ use std::fmt::{self, Write};
|
||||
|
||||
use crate::{
|
||||
Config,
|
||||
format::{self, Formatter},
|
||||
template::{self, Formatter},
|
||||
meta::Expr,
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@ pub struct FormatOptions {
|
||||
pub indent: u8,
|
||||
|
||||
/// Non-TOML specific options.
|
||||
general: format::Options,
|
||||
general: template::Options,
|
||||
}
|
||||
|
||||
impl Default for FormatOptions {
|
||||
@@ -86,13 +86,13 @@ impl Default for FormatOptions {
|
||||
/// ";
|
||||
///
|
||||
/// fn main() {
|
||||
/// let toml = confique::toml::format::<Conf>(FormatOptions::default());
|
||||
/// let toml = confique::toml::template::<Conf>(FormatOptions::default());
|
||||
/// assert_eq!(toml, EXPECTED);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn format<C: Config>(options: FormatOptions) -> String {
|
||||
pub fn template<C: Config>(options: FormatOptions) -> String {
|
||||
let mut out = TomlFormatter::new(&options);
|
||||
format::format::<C>(&mut out, options.general);
|
||||
template::format::<C>(&mut out, options.general);
|
||||
out.finish()
|
||||
}
|
||||
|
||||
@@ -177,12 +177,12 @@ impl fmt::Display for PrintExpr {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test_utils::{self, include_format_output};
|
||||
use super::{format, FormatOptions};
|
||||
use super::{template, FormatOptions};
|
||||
use pretty_assertions::assert_str_eq;
|
||||
|
||||
#[test]
|
||||
fn default() {
|
||||
let out = format::<test_utils::example1::Conf>(FormatOptions::default());
|
||||
let out = template::<test_utils::example1::Conf>(FormatOptions::default());
|
||||
assert_str_eq!(&out, include_format_output!("1-default.toml"));
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ mod tests {
|
||||
fn no_comments() {
|
||||
let mut options = FormatOptions::default();
|
||||
options.general.comments = false;
|
||||
let out = format::<test_utils::example1::Conf>(options);
|
||||
let out = template::<test_utils::example1::Conf>(options);
|
||||
assert_str_eq!(&out, include_format_output!("1-no-comments.toml"));
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ mod tests {
|
||||
fn indent_2() {
|
||||
let mut options = FormatOptions::default();
|
||||
options.indent = 2;
|
||||
let out = format::<test_utils::example1::Conf>(options);
|
||||
let out = template::<test_utils::example1::Conf>(options);
|
||||
assert_str_eq!(&out, include_format_output!("1-indent-2.toml"));
|
||||
}
|
||||
|
||||
@@ -206,13 +206,13 @@ mod tests {
|
||||
fn nested_gap_2() {
|
||||
let mut options = FormatOptions::default();
|
||||
options.general.nested_field_gap = 2;
|
||||
let out = format::<test_utils::example1::Conf>(options);
|
||||
let out = template::<test_utils::example1::Conf>(options);
|
||||
assert_str_eq!(&out, include_format_output!("1-nested-gap-2.toml"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn immediately_nested() {
|
||||
let out = format::<test_utils::example2::Conf>(Default::default());
|
||||
let out = template::<test_utils::example2::Conf>(Default::default());
|
||||
assert_str_eq!(&out, include_format_output!("2-default.toml"));
|
||||
}
|
||||
}
|
||||
|
||||
20
src/yaml.rs
20
src/yaml.rs
@@ -5,7 +5,7 @@ use std::fmt::{self, Write};
|
||||
|
||||
use crate::{
|
||||
Config,
|
||||
format::{self, Formatter},
|
||||
template::{self, Formatter},
|
||||
meta::Expr,
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@ pub struct FormatOptions {
|
||||
pub indent: u8,
|
||||
|
||||
/// Non-TOML specific options.
|
||||
general: format::Options,
|
||||
general: template::Options,
|
||||
}
|
||||
|
||||
impl Default for FormatOptions {
|
||||
@@ -88,13 +88,13 @@ impl Default for FormatOptions {
|
||||
///
|
||||
///
|
||||
/// fn main() {
|
||||
/// let yaml = confique::yaml::format::<Conf>(FormatOptions::default());
|
||||
/// let yaml = confique::yaml::template::<Conf>(FormatOptions::default());
|
||||
/// assert_eq!(yaml, EXPECTED);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn format<C: Config>(options: FormatOptions) -> String {
|
||||
pub fn template<C: Config>(options: FormatOptions) -> String {
|
||||
let mut out = YamlFormatter::new(&options);
|
||||
format::format::<C>(&mut out, options.general);
|
||||
template::format::<C>(&mut out, options.general);
|
||||
out.finish()
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ impl YamlFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
impl format::Formatter for YamlFormatter {
|
||||
impl Formatter for YamlFormatter {
|
||||
type ExprPrinter = PrintExpr;
|
||||
|
||||
fn buffer(&mut self) -> &mut String {
|
||||
@@ -206,12 +206,12 @@ impl fmt::Display for PrintExpr {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test_utils::{self, include_format_output};
|
||||
use super::{format, FormatOptions};
|
||||
use super::{template, FormatOptions};
|
||||
use pretty_assertions::assert_str_eq;
|
||||
|
||||
#[test]
|
||||
fn default() {
|
||||
let out = format::<test_utils::example1::Conf>(FormatOptions::default());
|
||||
let out = template::<test_utils::example1::Conf>(FormatOptions::default());
|
||||
assert_str_eq!(&out, include_format_output!("1-default.yaml"));
|
||||
}
|
||||
|
||||
@@ -219,13 +219,13 @@ mod tests {
|
||||
fn no_comments() {
|
||||
let mut options = FormatOptions::default();
|
||||
options.general.comments = false;
|
||||
let out = format::<test_utils::example1::Conf>(options);
|
||||
let out = template::<test_utils::example1::Conf>(options);
|
||||
assert_str_eq!(&out, include_format_output!("1-no-comments.yaml"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn immediately_nested() {
|
||||
let out = format::<test_utils::example2::Conf>(Default::default());
|
||||
let out = template::<test_utils::example2::Conf>(Default::default());
|
||||
assert_str_eq!(&out, include_format_output!("2-default.yaml"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user