serde-structprop
A serde serializer and deserializer for the structprop configuration file format — a simple, human-readable format for structured data.
Format overview
Structprop files are composed of three constructs:
# Lines beginning with # are comments (inline comments are also supported)
# Scalar key-value pair
key = value
key = "value with spaces or special chars"
key = 42
key = -7
key = true
# Nested object block
section {
nested_key = value
another = 123
}
# Array of scalars
list = { a b c }
list = {
a
b
c
}
Special characters in values (spaces, tabs, newlines, carriage returns,
#, {, }, =) must be wrapped in double quotes. Empty strings are
always quoted as "". The structprop format has no escape sequences.
A " character in the interior of an otherwise-bare value is emitted
as-is (e.g. hello"world → val = hello"world). Two cases are
unrepresentable and produce a serialization error:
- A value that requires quoting and contains
"(no way to embed"inside a quoted term). - A value whose first character is
"(the lexer treats a leading"as the start of a quoted string).
Keys follow the same rule.
Installation
Add to your Cargo.toml:
[]
= { = "0.2", = ["derive"] }
The derive feature enables serde's own derive macros. You still need a
direct serde dependency in your crate so that Serialize and Deserialize
are in scope. If you already depend on serde with features = ["derive"],
you can omit the feature flag here:
[]
= { = "1", = ["derive"] }
= "0.2"
Type mapping
| Rust / serde type | Structprop representation |
|---|---|
bool |
true or false |
i8 i16 i32 i64 u8 u16 u32 u64 |
bare integer scalar (e.g. 42, -7) |
f32, f64 |
bare float scalar (e.g. 3.14) |
char |
bare single-character scalar |
String / &str |
bare scalar, or "quoted" when it contains special chars or is empty; a " mixed with other special chars is unrepresentable (serialization error) |
Option<T> (Some) |
the inner value serialized normally |
Option<T> (None) / () |
null |
newtype struct (e.g. struct Meters(f64)) |
transparent — serializes as the inner type |
unit struct (e.g. struct Marker;) |
null |
| struct / map | key { … } block |
Vec<T> / sequence |
key = { … } list |
| tuple / tuple struct | key = { … } list of elements |
| unit enum variant | bare variant name |
| newtype enum variant | variant_name = <scalar or list> |
| tuple enum variant | variant_name = { … } list |
| struct enum variant | variant_name { … } block |
raw bytes (serialize_bytes / deserialize_bytes) |
unsupported — returns Error::UnsupportedType |
Quick start
Deserializing
use Deserialize;
use from_str;
Serializing
use Serialize;
use to_string;
Nested structs
use ;
use ;
Quoted values
Values containing spaces, tabs, newlines, or the special characters
(#, {, }, =) are quoted automatically on output and must be
quoted in the input. Empty strings are always quoted as "".
The structprop format has no escape sequences. A " in the interior
of an otherwise-bare value is emitted as-is (e.g. hello"world →
val = hello"world). Two cases are unrepresentable and return an error:
a value that requires quoting and contains ", and a value whose first
character is " (the lexer always interprets a leading " as the start
of a quoted string).
use ;
use ;
Optional fields
Fields typed as Option<T> are serialized as null when None and as
the inner value when Some. A missing key in the input also deserializes
as None:
use ;
use ;
Error handling
All functions return serde_structprop::Result<T>, an alias for
std::result::Result<T, serde_structprop::Error>.
use ;
match
Error variants
| Variant | When |
|---|---|
Error::Parse(String) |
Lexer or parser encountered unexpected input, or a scalar could not be coerced to the requested numeric type |
Error::Message(String) |
serde-generated error (e.g. missing required field, unknown variant) |
Error::UnsupportedType(&'static str) |
Type has no structprop equivalent (e.g. raw byte slices) |
Error::KeyMustBeString |
A map was serialized with a non-string key |
Module layout
| Module | Contents |
|---|---|
serde_structprop::lexer |
Tokenizer: converts raw text to Tokens |
serde_structprop::parse |
Recursive-descent parser: produces a Value tree |
serde_structprop::de |
serde::Deserializer implementation; from_str entry point |
serde_structprop::ser |
serde::Serializer implementation; to_string entry point |
serde_structprop::error |
Error enum and Result<T> alias |
License
Licensed under either of
at your option.