#[allow(dead_code)]
fn main() {
use std::path::PathBuf;
#[derive(fieldwork::Fieldwork, Default)]
#[fieldwork(get, set, with, without, get_mut, rename_predicates)]
struct ServerConfig {
#[field(into)] host: String,
port: u16,
#[field(into, option_set_some)] log_dir: Option<PathBuf>,
tls_required: bool,
verbose: bool,
}
let mut config = ServerConfig::default()
.with_host("LocalHost") .with_port(8080)
.with_log_dir("/var/log") .with_tls_required();
config.host_mut().make_ascii_lowercase();
assert_eq!(config.host(), "localhost"); assert_eq!(config.port(), 8080);
assert_eq!(
config.log_dir().unwrap(),
PathBuf::from("/var/log").as_path()
);
assert!(config.is_tls_required()); assert!(!config.is_verbose());
config.set_port(9090).set_verbose(true);
config = config.without_log_dir(); assert!(config.log_dir().is_none());
#[derive(fieldwork::Fieldwork)]
#[fieldwork(get, into_field)]
enum ServerEvent {
Started {
host: String,
port: u16,
},
Request {
host: String,
port: u16,
path: String,
},
Shutdown {
host: String,
port: u16,
},
}
let event = ServerEvent::Request {
host: "example.com".to_string(),
port: 8080,
path: "/api/health".to_string(),
};
assert_eq!(event.host(), "example.com"); assert_eq!(event.port(), 8080);
assert_eq!(event.path(), Some("/api/health"));
assert_eq!(
ServerEvent::Started {
host: "example.com".to_string(),
port: 8080
}
.path(),
None,
);
assert_eq!(event.into_host(), "example.com");
}