Always validate configuration options and provide clear feedback to users about potential issues. This includes warning about unused configuration keys, preventing redundant options, and ensuring cross-platform compatibility.
Always validate configuration options and provide clear feedback to users about potential issues. This includes warning about unused configuration keys, preventing redundant options, and ensuring cross-platform compatibility.
Key practices:
None
and default enum values - choose one approach consistentlyallow(unused)
instead of cfg
attributes for configuration fields to prevent warnings on platforms where the feature isn’t availableExample implementation:
// Warn about unused keys during deserialization
for key in unused.keys() {
log::warn!(target: LOG_TARGET, "Unused config key: {}", key);
}
// Avoid redundant options - choose Option<T> OR T::Default, not both
pub struct WindowConfig {
// Good: Clear intent
pub window_level: WindowLevel, // with Default trait
// Avoid: Ambiguous between None and WindowLevel::Normal
// pub window_level: Option<WindowLevel>,
}
// Validate required combinations
if !content.hyperlinks && content.regex.is_none() {
return Err("At least one of hyperlink or regex must be specified");
}
This approach prevents configuration errors that can be difficult to debug and improves the user experience by providing immediate feedback about configuration issues.
Enter the URL of a public GitHub repository