Props-Util
A Rust library for easily loading and parsing properties files into strongly-typed structs.
Overview
Props-Util provides a procedural macro that allows you to derive a Properties trait for your structs, enabling automatic parsing of properties files into your struct fields. This makes configuration management in Rust applications more type-safe and convenient.
Features
- Derive macro for automatic properties parsing
- Support for default values
- Type conversion from string to your struct's field types
- Error handling for missing or malformed properties
- Support for both file-based and default initialization
Installation
Add the following to your Cargo.toml:
[]
= "0.1.0" # Replace with the actual version
Usage
Basic Example
- Define a struct with the
Propertiesderive macro:
use Properties;
- Load properties from a file:
- Create a properties file (e.g.,
config.properties):
Attribute Parameters
The #[prop] attribute accepts the following parameters:
key: The property key to look for in the properties file (optional). If not specified, the field name will be used as the key.default: A default value to use if the property is not found in the file (optional)
Field Types
Props-Util supports any type that implements FromStr. This includes:
String- Numeric types (
u8,u16,u32,u64,i8,i16,i32,i64,f32,f64) - Boolean (
bool) Vec<T>whereTimplementsFromStr(values are comma-separated in the properties file)Option<T>whereTimplementsFromStr(optional fields that may or may not be present in the properties file)- Custom types that implement
FromStr
Example of using Vec and Option types:
In the properties file:
# optional_host is not set, so it will be None
Converting Between Different Types
You can use the from function to convert between different configuration types. This is particularly useful when you have multiple structs that share similar configuration fields but with different types or structures:
use Properties;
use Result;
Important: When converting between types using
from, thekeyattribute values must match between the source and target types. If nokeyis specified, the field names must match. This ensures that the configuration values are correctly mapped between the different types.
This approach is useful when:
- You need to migrate between different configuration formats
- You have multiple applications that share configuration but use different struct layouts
- You want to transform configuration between different versions of your application
Error Handling
The from_file method returns a std::io::Result<T>, which will contain:
Ok(T)if the properties file was successfully parsedErrwith an appropriate error message if:- The file couldn't be opened or read
- A required property is missing (and no default is provided)
- A property value couldn't be parsed into the expected type
- The properties file is malformed (e.g., missing
=character)
Default Initialization
You can also create an instance with default values without reading from a file:
let config = default?;
This will use the default values specified in the #[prop] attributes.
Properties File Format
The properties file follows a simple key-value format:
- Each line represents a single property
- The format is
key=value - Lines starting with
#or!are treated as comments and ignored - Empty lines are ignored
- Leading and trailing whitespace around both key and value is trimmed
Example:
# Application settings
# Database configuration
# Logging settings
# Network settings
# Features
# Optional settings