pub struct CustomFormat { /* private fields */ }Expand description
A user-defined format: an id, the extensions it claims, and a parser
callback. It goes into Options::formats like any built-in format
(formats: [..the defaults, md.into()]), so the usual rule applies —
the last claimer of an extension owns it, and a custom format can take
over a built-in extension. Table-shaped custom formats can lower their
file into rows and reuse the generic table stage via
parse_table.
A markdown pipe-table as a config format (runs as a doc test — a string source can name a custom format directly, no files needed):
use c4::{CustomFormat, Loader};
let md = CustomFormat::new("md-table", ["md"], |text, path, options| {
let rows: Vec<Vec<String>> = text
.lines()
.map(str::trim)
.filter(|l| l.starts_with('|') && !l.contains("---"))
// markdown tables always start with a header row — dropping
// it here keeps the data rows positional (key,value[,format]),
// exactly what parse_table expects
.skip(1)
.map(|l| {
l.trim_matches('|')
.split('|')
.map(|cell| cell.trim().to_owned())
.collect()
})
.collect();
c4::parse_table(rows, path, options)
});
// to read `.md` files instead, push `md` into Options.formats —
// custom formats claim extensions exactly like built-ins do
let value: c4::Value = Loader::new(c4::Options {
sources: vec![(
md,
"| key | value | format |
| ---- | ----- | ------ |
| name | c4 | str |
| port | 8080 | u16 |",
)
.into()],
..c4::Options::default()
})
.load()?;
assert_eq!(value["name"].as_str(), Some("c4"));
assert_eq!(value["port"].as_u64(), Some(8080));Implementations§
Trait Implementations§
Source§impl Clone for CustomFormat
impl Clone for CustomFormat
Source§fn clone(&self) -> CustomFormat
fn clone(&self) -> CustomFormat
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CustomFormat
impl Debug for CustomFormat
Source§impl From<CustomFormat> for FormatKind
impl From<CustomFormat> for FormatKind
Source§fn from(custom: CustomFormat) -> Self
fn from(custom: CustomFormat) -> Self
Converts to this type from the input type.
Source§impl From<CustomFormat> for FormatSpec
impl From<CustomFormat> for FormatSpec
Source§fn from(custom: CustomFormat) -> Self
fn from(custom: CustomFormat) -> Self
Converts to this type from the input type.
Source§impl PartialEq for CustomFormat
impl PartialEq for CustomFormat
Auto Trait Implementations§
impl !RefUnwindSafe for CustomFormat
impl !UnwindSafe for CustomFormat
impl Freeze for CustomFormat
impl Send for CustomFormat
impl Sync for CustomFormat
impl Unpin for CustomFormat
impl UnsafeUnpin for CustomFormat
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more