Skip to content

Commit

Permalink
CLI framework options with clap arg_enum and default
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored Nov 9, 2022
1 parent 77b2628 commit df65c53
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
25 changes: 20 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use seaography_generator::{write_project, WebFrameworkEnum};
use clap::{ArgEnum, Parser};
use seaography_generator::write_project;

#[derive(clap::Parser)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -28,8 +28,8 @@ pub struct Args {
#[clap(short, long)]
pub hidden_tables: Option<bool>,

#[clap(short, long)]
pub framework: Option<WebFrameworkEnum>,
#[clap(short, long, arg_enum, value_parser, default_value = "poem")]
pub framework: WebFrameworkEnum,
}

/**
Expand Down Expand Up @@ -143,10 +143,25 @@ async fn main() {
expanded_format,
tables,
sql_library,
args.framework.unwrap_or_else(|| WebFrameworkEnum::Poem),
args.framework.into(),
args.depth_limit,
args.complexity_limit,
)
.await
.unwrap();
}

#[derive(ArgEnum, Debug, Clone, Copy, Eq, PartialEq)]
pub enum WebFrameworkEnum {
Actix,
Poem,
}

impl From<WebFrameworkEnum> for seaography_generator::WebFrameworkEnum {
fn from(framework: WebFrameworkEnum) -> Self {
match framework {
WebFrameworkEnum::Actix => seaography_generator::WebFrameworkEnum::Actix,
WebFrameworkEnum::Poem => seaography_generator::WebFrameworkEnum::Poem,
}
}
}
24 changes: 0 additions & 24 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,6 @@ pub enum WebFrameworkEnum {
Poem,
}

impl std::str::FromStr for WebFrameworkEnum {
type Err = String;

fn from_str(input: &str) -> std::result::Result<WebFrameworkEnum, Self::Err> {
match input {
"actix" => Ok(Self::Actix),
"poem" => Ok(Self::Poem),
_ => Err(format!(
"Invalid framework '{}', 'actix' and 'poem' are supported!",
input
)),
}
}
}

impl std::fmt::Display for WebFrameworkEnum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WebFrameworkEnum::Actix => f.write_str("actix"),
WebFrameworkEnum::Poem => f.write_str("poem"),
}
}
}

pub async fn write_project<P: AsRef<Path>>(
path: &P,
db_url: &str,
Expand Down

0 comments on commit df65c53

Please sign in to comment.