fn config_toml(
    mut default_config: Config,
    matches: &ArgMatches,
) -> Result<Config, Box<dyn StdError>> {
    // default name for toml-file
    let mut name = "config.toml";

    // if user defined another toml-file, try to read from that
    if let Some(file) = matches.value_of("File") {
        let file_reg = Regex::new(r#".*\\.toml"#).unwrap();

        if file_reg.is_match(file) && Path::new(file).exists() {
            name = file;
        } else {
            return Err("invalid File in command line".into());
        }
    }

    // only proceed if toml-file exists
    if Path::new(name).exists() {
        let mut f = File::open(name)?;
        let mut s = String::new();

        // read file content as string
        f.read_to_string(&mut s)?;

        let value: Value = match s.parse() {
            Ok(n) => n,
            _ => return Err("corrupted config file".into()),
        };

        // Resolution Width
        let mut res_toml = match value.lookup("Graphic.resolution_width") {
            Some(n) => n,
            None => return Err("resolution_width in config file is invalid".into()),
        };
        let int_res_width = match res_toml.as_integer() {
            Some(n) => {
                if n < 1 {
                    return Err("resolution can not be negative".into());
                } else {
                    n as u32
                }
            }

            None => return Err("resolution_width in config file has no integer".into()),
        };

        // Resolution height
        res_toml = match value.lookup("Graphic.resolution_height") {
            Some(n) => n,
            None => return Err("resolution_height in config file is invalid".into()),
        };
        let int_res_height = match res_toml.as_integer() {
            Some(n) => {
                if n < 1 {
                    return Err("resolution can not be negative".into());
                } else {
                    n as u32
                }
            }

            None => return Err("resolution_height in config file has no integer".into()),
        };

        default_config.resolution = Dimension2::new(int_res_width, int_res_height);

        // Window mode
        let window = match value.lookup("Graphic.windowmode") {
            Some(n) => n,
            None => return Err("resolution_height in config file is invalid".into()),
        };

        match window.as_str() {
            Some(n) => match n {
                "Windowed" => default_config.window_mode = WindowMode::Windowed,
                "FullScreen" => default_config.window_mode = WindowMode::FullScreen,
                _ => return Err("invalid Window Mode in config file".into()),
            },
            _ => return Err("invalid Window Mode in config file".into()),
        }

        // Tessellation
        let tessellation = match value.lookup("Graphic.tessellation") {
            Some(n) => n,
            None => return Err("tessellation in config file is invalid".into()),
        };

        match tessellation.as_bool() {
            Some(n) => default_config.tessellation = n,
            None => return Err("tessellation value in config file is invalid".into()),
        };

        // Bloom
        let bloom = match value.lookup("Graphic.bloom") {
            Some(n) => n,
            None => return Err("bloom in config file is invalid".into()),
        };

        match bloom.as_bool() {
            Some(n) => default_config.bloom = n,
            None => return Err("bloom value in config file is invalid".into()),
        };

        // Vsync
        let sync = match value.lookup("Graphic.vsync") {
            Some(n) => n,
            None => return Err("vsync in config file is invalid".into()),
        };

        match sync.as_bool() {
            Some(n) => default_config.vsync = n,
            None => return Err("vsync value in config file is invalid".into()),
        };

        // Pillar Highlighting
        let highlight = match value.lookup("Game_settings.highlight_pillar") {
            Some(n) => n,
            None => return Err("highlight in config file is invalid".into()),
        };

        match highlight.as_bool() {
            Some(n) => default_config.highlight_pillar = n,
            None => return Err("highlight value in config file is invalid".into()),
        };

        // world seed
        let seed = match value.lookup("Game_settings.seed") {
            Some(n) => n,
            None => return Err("seed in config file is invalid".into()),
        };

        match seed.as_integer() {
            Some(n) => {
                if n < 0 {
                    return Err("seed can not be negative".into());
                } else {
                    default_config.seed = n as u64
                }
            }
            None => return Err("seed in config file is invalid".into()),
        };
    }

    Ok(default_config)
}
/// read configuration from toml-file
/// overwrite default values with toml-file values
/// return updated config
read configuration from toml-file
overwrite default values with toml-file values
return updated config
read configuration from toml - file overwrite default values with toml - file values return updated config
fn config_toml(
    mut default_config: Config,
    matches: &ArgMatches,
) -> Result<Config, Box<dyn StdError>> {
    // default name for toml-file
    let mut name = "config.toml";
    // if user defined another toml-file, try to read from that
    if let Some(file) = matches.value_of("File") {
        let file_reg = Regex::new(r#".*\\.toml"#).unwrap();
        if file_reg.is_match(file) && Path::new(file).exists() {
            name = file;
        } else {
            return Err("invalid File in command line".into());
        }
    }
    // only proceed if toml-file exists
    if Path::new(name).exists() {
        let mut f = File::open(name)?;
        let mut s = String::new();
        // read file content as string
        f.read_to_string(&mut s)?;
        let value: Value = match s.parse() {
            Ok(n) => n,
            _ => return Err("corrupted config file".into()),
        };
        // Resolution Width
        let mut res_toml = match value.lookup("Graphic.resolution_width") {
            Some(n) => n,
            None => return Err("resolution_width in config file is invalid".into()),
        };
        let int_res_width = match res_toml.as_integer() {
            Some(n) => {
                if n < 1 {
                    return Err("resolution can not be negative".into());
                } else {
                    n as u32
                }
            }
            None => return Err("resolution_width in config file has no integer".into()),
        };
        // Resolution height
        res_toml = match value.lookup("Graphic.resolution_height") {
            Some(n) => n,
            None => return Err("resolution_height in config file is invalid".into()),
        };
        let int_res_height = match res_toml.as_integer() {
            Some(n) => {
                if n < 1 {
                    return Err("resolution can not be negative".into());
                } else {
                    n as u32
                }
            }
            None => return Err("resolution_height in config file has no integer".into()),
        };
        default_config.resolution = Dimension2::new(int_res_width, int_res_height);
        // Window mode
        let window = match value.lookup("Graphic.windowmode") {
            Some(n) => n,
            None => return Err("resolution_height in config file is invalid".into()),
        };
        match window.as_str() {
            Some(n) => match n {
                "Windowed" => default_config.window_mode = WindowMode::Windowed,
                "FullScreen" => default_config.window_mode = WindowMode::FullScreen,
                _ => return Err("invalid Window Mode in config file".into()),
            },
            _ => return Err("invalid Window Mode in config file".into()),
        }
        // Tessellation
        let tessellation = match value.lookup("Graphic.tessellation") {
            Some(n) => n,
            None => return Err("tessellation in config file is invalid".into()),
        };
        match tessellation.as_bool() {
            Some(n) => default_config.tessellation = n,
            None => return Err("tessellation value in config file is invalid".into()),
        };
        // Bloom
        let bloom = match value.lookup("Graphic.bloom") {
            Some(n) => n,
            None => return Err("bloom in config file is invalid".into()),
        };
        match bloom.as_bool() {
            Some(n) => default_config.bloom = n,
            None => return Err("bloom value in config file is invalid".into()),
        };
        // Vsync
        let sync = match value.lookup("Graphic.vsync") {
            Some(n) => n,
            None => return Err("vsync in config file is invalid".into()),
        };
        match sync.as_bool() {
            Some(n) => default_config.vsync = n,
            None => return Err("vsync value in config file is invalid".into()),
        };
        // Pillar Highlighting
        let highlight = match value.lookup("Game_settings.highlight_pillar") {
            Some(n) => n,
            None => return Err("highlight in config file is invalid".into()),
        };
        match highlight.as_bool() {
            Some(n) => default_config.highlight_pillar = n,
            None => return Err("highlight value in config file is invalid".into()),
        };
        // world seed
        let seed = match value.lookup("Game_settings.seed") {
            Some(n) => n,
            None => return Err("seed in config file is invalid".into()),
        };
        match seed.as_integer() {
            Some(n) => {
                if n < 0 {
                    return Err("seed can not be negative".into());
                } else {
                    default_config.seed = n as u64
                }
            }
            None => return Err("seed in config file is invalid".into()),
        };
    }
    Ok(default_config)
}
fn config_toml ( mut default_config : Config , matches : & ArgMatches , ) -> Result < Config , Box < dyn StdError > > { // default name for toml-file let mut name = " " ; // if user defined another toml-file, try to read from that if let Some ( file ) = matches . value_of ( " " ) { let file_reg = Regex :: new ( r#".*\\.toml"# ) . unwrap ( ) ; if file_reg . is_match ( file ) && Path :: new ( file ) . exists ( ) { name = file ; } else { return Err ( " " . into ( ) ) ; } } // only proceed if toml-file exists if Path :: new ( name ) . exists ( ) { let mut f = File :: open ( name ) ? ; let mut s = String :: new ( ) ; // read file content as string f . read_to_string ( & mut s ) ? ; let value : Value = match s . parse ( ) { Ok ( n ) => n , _ => return Err ( " " . into ( ) ) , } ; // Resolution Width let mut res_toml = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; let int_res_width = match res_toml . as_integer ( ) { Some ( n ) => { if n < 1 { return Err ( " " . into ( ) ) ; } else { n as u32 } } None => return Err ( " " . into ( ) ) , } ; // Resolution height res_toml = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; let int_res_height = match res_toml . as_integer ( ) { Some ( n ) => { if n < 1 { return Err ( " " . into ( ) ) ; } else { n as u32 } } None => return Err ( " " . into ( ) ) , } ; default_config . resolution = Dimension2 :: new ( int_res_width , int_res_height ) ; // Window mode let window = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; match window . as_str ( ) { Some ( n ) => match n { " " => default_config . window_mode = WindowMode :: Windowed , " " => default_config . window_mode = WindowMode :: FullScreen , _ => return Err ( " " . into ( ) ) , } , _ => return Err ( " " . into ( ) ) , } // Tessellation let tessellation = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; match tessellation . as_bool ( ) { Some ( n ) => default_config . tessellation = n , None => return Err ( " " . into ( ) ) , } ; // Bloom let bloom = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; match bloom . as_bool ( ) { Some ( n ) => default_config . bloom = n , None => return Err ( " " . into ( ) ) , } ; // Vsync let sync = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; match sync . as_bool ( ) { Some ( n ) => default_config . vsync = n , None => return Err ( " " . into ( ) ) , } ; // Pillar Highlighting let highlight = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; match highlight . as_bool ( ) { Some ( n ) => default_config . highlight_pillar = n , None => return Err ( " " . into ( ) ) , } ; // world seed let seed = match value . lookup ( " " ) { Some ( n ) => n , None => return Err ( " " . into ( ) ) , } ; match seed . as_integer ( ) { Some ( n ) => { if n < 0 { return Err ( " " . into ( ) ) ; } else { default_config . seed = n as u64 } } None => return Err ( " " . into ( ) ) , } ; } Ok ( default_config ) }
read configuration from toml-file
overwrite default values with toml-file values
return updated config
read configuration from toml - file overwrite default values with toml - file values return updated config
default_config
Config
matches
&ArgMatches