Skip to content

Commit

Permalink
Add fallback for missing config file
Browse files Browse the repository at this point in the history
Should we for some reason not be able to open `./config.toml`, a default
configuration is provided, that should reflect what is needed on the
used CM3.

Changelog: Added
  • Loading branch information
Florian Guggi committed Jul 13, 2024
1 parent 2f8894b commit 52e4540
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ packs:

clean:
rm -rf *.profraw
rm -f ThreadId*
rm -f ThreadId* updatepin
rm -f data/*
rm -rf archives/*
rm -rf tests/tmp
Expand Down
25 changes: 21 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,34 @@ struct Configuration {
socket: String,
}

impl Default for Configuration {
fn default() -> Self {
Self {
uart: "/dev/serial0".to_string(),
baudrate: 921_600,
heartbeat_pin: 34,
update_pin: 35,
heartbeat_freq: 10,
socket: "/tmp/scheduler_socket".to_string(),
}
}
}

fn main() -> ! {
let _ = sl::WriteLogger::init(
sl::LevelFilter::Info,
sl::Config::default(),
std::fs::OpenOptions::new().create(true).append(true).open("log").unwrap(),
);

let config: Configuration = toml::from_str(
&std::fs::read_to_string("./config.toml").expect("Could not open config file"),
)
.unwrap();
let config: Configuration = if let Ok(s) = std::fs::read_to_string("./config.toml") {
toml::from_str(&s)
.map_err(|e| log::error!("Could not parse config {e:?}, using default"))
.unwrap_or_default()
} else {
log::error!("Could not open config.toml, using default");
Configuration::default()
};

create_directory_if_not_exists("archives").unwrap();
create_directory_if_not_exists("data").unwrap();
Expand Down

0 comments on commit 52e4540

Please sign in to comment.