Skip to content

Commit

Permalink
simplify Args
Browse files Browse the repository at this point in the history
  • Loading branch information
rectalogic committed Feb 1, 2025
1 parent 6b05526 commit 8f85beb
Showing 1 changed file with 7 additions and 39 deletions.
46 changes: 7 additions & 39 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::app;
use bevy::prelude::*;
use clap::{Arg, Command, ValueHint};
use std::path::PathBuf;
use std::path::Path;

pub fn parse_cli() -> app::AppPlugin {
let matches = Command::new(clap::crate_name!())
Expand All @@ -28,10 +28,10 @@ pub fn parse_cli() -> app::AppPlugin {

match matches.subcommand() {
Some(("editor", editor_matches)) => {
app::AppPlugin::Editor(Args::from(editor_matches.get_one::<String>("project")))
app::AppPlugin::Editor(Args::new(editor_matches.get_one::<String>("project")))
}
Some(("player", player_matches)) => {
app::AppPlugin::Player(Args::from(player_matches.get_one::<String>("project")))
app::AppPlugin::Player(Args::new(player_matches.get_one::<String>("project")))
}
None => app::AppPlugin::Editor(Args::new(None)),
_ => unreachable!("All commands covered"),
Expand All @@ -41,53 +41,21 @@ pub fn parse_cli() -> app::AppPlugin {
#[derive(Resource, Clone, Debug)]
pub struct Args {
/// Project file
project: Option<PathBuf>,
project: Option<String>,
}

impl Args {
pub fn new(project: Option<&str>) -> Self {
pub fn new(project: Option<&String>) -> Self {
Self {
project: project.map(|p| p.into()),
}
}

pub fn project(&self) -> Option<&std::path::Path> {
pub fn project(&self) -> Option<&Path> {
if let Some(ref project) = self.project {
Some(project.as_path())
Some(Path::new(project))
} else {
None
}
}
}

impl From<&str> for Args {
fn from(value: &str) -> Self {
Self {
project: Some(value.into()),
}
}
}

impl From<&String> for Args {
fn from(value: &String) -> Self {
Self {
project: Some(value.into()),
}
}
}

impl From<Option<&str>> for Args {
fn from(value: Option<&str>) -> Self {
Self {
project: value.map(|v| v.into()),
}
}
}

impl From<Option<&String>> for Args {
fn from(value: Option<&String>) -> Self {
Self {
project: value.map(|v| v.into()),
}
}
}

0 comments on commit 8f85beb

Please sign in to comment.