refactor: more entrypoint simplifications
initial model calculated in its own function
This commit is contained in:
38
src/main.rs
38
src/main.rs
@ -84,6 +84,26 @@ fn parse_args(args: Vec<String>) -> Option<CmdLineArgs> {
|
||||
});
|
||||
}
|
||||
|
||||
fn get_initial_model(file_path: &Option<PathBuf>) -> Result<Model> {
|
||||
return if let Some(ref path) = file_path {
|
||||
if path.exists() {
|
||||
let mut m = persistence::load(path)
|
||||
.with_context(|| format!("Failed to load {}", path.display()))?;
|
||||
m.normalize_view_state();
|
||||
Ok(m)
|
||||
} else {
|
||||
let name = path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("New Model")
|
||||
.to_string();
|
||||
Ok(Model::new(name))
|
||||
}
|
||||
} else {
|
||||
Ok(Model::new("New Model"))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let maybe_cmd_line_args = parse_args(args);
|
||||
@ -94,23 +114,7 @@ fn main() -> Result<()> {
|
||||
let cmd_line_args = maybe_cmd_line_args.unwrap();
|
||||
|
||||
// Load or create model
|
||||
let mut model = if let Some(ref path) = cmd_line_args.file_path {
|
||||
if path.exists() {
|
||||
let mut m = persistence::load(path)
|
||||
.with_context(|| format!("Failed to load {}", path.display()))?;
|
||||
m.normalize_view_state();
|
||||
m
|
||||
} else {
|
||||
let name = path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("New Model")
|
||||
.to_string();
|
||||
Model::new(name)
|
||||
}
|
||||
} else {
|
||||
Model::new("New Model")
|
||||
};
|
||||
let mut model = get_initial_model(&cmd_line_args.file_path)?;
|
||||
|
||||
// Headless mode
|
||||
if !cmd_line_args.headless_cmds.is_empty() || cmd_line_args.headless_script.is_some() {
|
||||
|
||||
Reference in New Issue
Block a user