January 2019
Intermediate to advanced
520 pages
14h 32m
English
Our server will expect to find a configuration file in the current working folder with the name microservice.toml. To read a configuration and convert it to the Config struct, we need to find and read this file if it exists. Add the following code to the main function of the server:
let config = File::open("microservice.toml") .and_then(|mut file| { let mut buffer = String::new(); file.read_to_string(&mut buffer)?; Ok(buffer) }) .and_then(|buffer| { toml::from_str::<Config>(&buffer) .map_err(|err| io::Error::new(io::ErrorKind::Other, err)) }) .map_err(|err| { warn!("Can't read config file: {}", err); }) .ok();
The preceding code is a chain of method calls that start with the File instance. We use the open method ...
Read now
Unlock full access