pub struct Documenter<'d> { /* private fields */ }Expand description
Contains the option for documenting YAML
Implementations§
Source§impl<'d> Documenter<'d>
impl<'d> Documenter<'d>
Sourcepub fn description_field(self, field: &'d str) -> Self
pub fn description_field(self, field: &'d str) -> Self
Change the description field to describe a field that contains other field. Default: “description”“
E.g. if you have the following YAML structure:
foo:
bar: true
baz: falseYou can document it with the following YAML code:
foo:
__description__: This field is needed for foo because it contains nested fields
bar: No need for inned __description__ since bar contains only a value
baz: Same for bazBy default you shouldn’t need to change this, except if your YAML structure actually contains
a field called __description__.
Sourcepub fn type_name(self, f: &'d dyn Fn(&ValueType) -> String) -> Self
pub fn type_name(self, f: &'d dyn Fn(&ValueType) -> String) -> Self
Change the way to display types.
The default function is a sensible one for english, but for other languages or if you want to tweak the display (e.g. not printing the type’s name between parenthesis) you can change it.
§Argument
- f: reference to a (&ValueType) -> String closure or function. It it responsible for
returning the type name as string. Typically you will want to match on
yaml_extras::document::ValueTypeand maybe call theyaml_extras_document_ValueType::to_strfunction, which is the default.
§Example
let yaml = serde_yaml::from_str("foo: 42").unwrap();
let mut d = yaml_extras::Documenter::new()
.type_name(&|t| format!(" (whatever)"));
let mut actual = d.apply_value(&yaml, None).unwrap();
assert_eq!(actual, "foo (whatever): 42");
d = d.type_name(&|t| String::new());
actual = d.apply_value(&yaml, None).unwrap();
assert_eq!(actual, "foo: 42");Sourcepub fn format_key(self, f: &'d dyn Fn(KeyArgs<'_>) -> String) -> Self
pub fn format_key(self, f: &'d dyn Fn(KeyArgs<'_>) -> String) -> Self
Change the way Mappings keys are displayed.
§Example
let yaml = serde_yaml::from_str::<serde_yaml::Value>(r#"foo: 42
bar: true"#).unwrap();
let actual = yaml_extras::Documenter::new()
// Quite useless way to display the info
.format_key(&|args| format!("{}!!!", args.key.to_uppercase()))
.apply_value(&yaml, None)
.unwrap();
assert_eq!(actual, "FOO!!!\nBAR!!!");Sourcepub fn format_mapping(self, f: &'d dyn Fn(InnerArgs<'_>) -> String) -> Self
pub fn format_mapping(self, f: &'d dyn Fn(InnerArgs<'_>) -> String) -> Self
Change the way Mappings are displayed.
Sourcepub fn format_list(self, f: &'d dyn Fn(InnerArgs<'_>) -> String) -> Self
pub fn format_list(self, f: &'d dyn Fn(InnerArgs<'_>) -> String) -> Self
Change the way Sequences are displayed.
Sourcepub fn indent(self, indent: &'d str) -> Self
pub fn indent(self, indent: &'d str) -> Self
Change the indent. Default: 4 spaces.
§Example
let d = yaml_extras::Documenter::new()
.indent("\t");Sourcepub fn apply_value(
&self,
value: &Value,
description: Option<&Value>,
) -> Result<String>
pub fn apply_value( &self, value: &Value, description: Option<&Value>, ) -> Result<String>
Uses a YAML representation from a default struct to document an API or options in a YAML-looking way
The idea is to first generate a yaml representation with YourStruct::default() to get a mostly automated API description.
§Arguments
value: should correspond to aserde_yamlValue with the default values of your structuredescription: an optionalserde_yamlvalue mirroring thevaluebut with descriptions for fields you want to document. Use__description__inside aMappingto document the upper-level field.
§Example
let desc_yaml = r#"
foo:
__description__: Description for foo
bar: Description for bar
"#;
let yaml = r#"
foo:
bar: 42
"#;
let expected = "# Description for foo
foo: \n # Description for bar
bar (Number): 42";
let value: serde_yaml::Value = serde_yaml::from_str(&yaml).unwrap();
let desc: serde_yaml::Value = serde_yaml::from_str(&desc_yaml).unwrap();
let s = yaml_extras::Documenter::new()
.apply_value(&value, Some(&desc))
.unwrap();
assert_eq!(s, expected);