mod build;
mod doc;
mod init;
mod plugin;
mod serve;
mod upload;
use std::{
borrow::Cow,
env,
error::Error,
fmt,
path::{Path, PathBuf},
str::FromStr,
};
use structopt::StructOpt;
use thiserror::Error;
pub use self::build::*;
pub use self::doc::*;
pub use self::init::*;
pub use self::plugin::*;
pub use self::serve::*;
pub use self::upload::*;
#[derive(Debug, StructOpt)]
#[structopt(name = "Rojo", about, author)]
pub struct Options {
#[structopt(flatten)]
pub global: GlobalOptions,
#[structopt(subcommand)]
pub subcommand: Subcommand,
}
#[derive(Debug, StructOpt)]
pub struct GlobalOptions {
#[structopt(long("verbose"), short, global(true), parse(from_occurrences))]
pub verbosity: u8,
#[structopt(long("color"), global(true), default_value("auto"))]
pub color: ColorChoice,
}
#[derive(Debug, Clone, Copy)]
pub enum ColorChoice {
Auto,
Always,
Never,
}
impl FromStr for ColorChoice {
type Err = ColorChoiceParseError;
fn from_str(source: &str) -> Result<Self, Self::Err> {
match source {
"auto" => Ok(ColorChoice::Auto),
"always" => Ok(ColorChoice::Always),
"never" => Ok(ColorChoice::Never),
_ => Err(ColorChoiceParseError {
attempted: source.to_owned(),
}),
}
}
}
impl From<ColorChoice> for termcolor::ColorChoice {
fn from(value: ColorChoice) -> Self {
match value {
ColorChoice::Auto => termcolor::ColorChoice::Auto,
ColorChoice::Always => termcolor::ColorChoice::Always,
ColorChoice::Never => termcolor::ColorChoice::Never,
}
}
}
impl From<ColorChoice> for env_logger::WriteStyle {
fn from(value: ColorChoice) -> Self {
match value {
ColorChoice::Auto => env_logger::WriteStyle::Auto,
ColorChoice::Always => env_logger::WriteStyle::Always,
ColorChoice::Never => env_logger::WriteStyle::Never,
}
}
}
#[derive(Debug, Error)]
#[error("Invalid color choice '{attempted}'. Valid values are: auto, always, never")]
pub struct ColorChoiceParseError {
attempted: String,
}
#[derive(Debug, StructOpt)]
pub enum Subcommand {
Init(InitCommand),
Serve(ServeCommand),
Build(BuildCommand),
Upload(UploadCommand),
Doc,
Plugin(PluginCommand),
}
#[derive(Debug, StructOpt)]
pub struct InitCommand {
#[structopt(default_value = "")]
pub path: PathBuf,
#[structopt(long, default_value = "place")]
pub kind: InitKind,
}
impl InitCommand {
pub fn absolute_path(&self) -> Cow<'_, Path> {
resolve_path(&self.path)
}
}
#[derive(Debug, Clone, Copy)]
pub enum InitKind {
Place,
Model,
}
impl FromStr for InitKind {
type Err = InitKindParseError;
fn from_str(source: &str) -> Result<Self, Self::Err> {
match source {
"place" => Ok(InitKind::Place),
"model" => Ok(InitKind::Model),
_ => Err(InitKindParseError {
attempted: source.to_owned(),
}),
}
}
}
#[derive(Debug)]
pub struct InitKindParseError {
attempted: String,
}
impl Error for InitKindParseError {}
impl fmt::Display for InitKindParseError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"Invalid init kind '{}'. Valid kinds are: place, model",
self.attempted
)
}
}
#[derive(Debug, StructOpt)]
pub struct ServeCommand {
#[structopt(default_value = "")]
pub project: PathBuf,
#[structopt(long)]
pub port: Option<u16>,
}
impl ServeCommand {
pub fn absolute_project(&self) -> Cow<'_, Path> {
resolve_path(&self.project)
}
}
#[derive(Debug, StructOpt)]
pub struct BuildCommand {
#[structopt(default_value = "")]
pub project: PathBuf,
#[structopt(long, short)]
pub output: PathBuf,
#[structopt(long)]
pub watch: bool,
}
impl BuildCommand {
pub fn absolute_project(&self) -> Cow<'_, Path> {
resolve_path(&self.project)
}
}
#[derive(Debug, StructOpt)]
pub struct UploadCommand {
#[structopt(default_value = "")]
pub project: PathBuf,
#[structopt(long)]
pub cookie: Option<String>,
#[structopt(long = "asset_id")]
pub asset_id: u64,
}
impl UploadCommand {
pub fn absolute_project(&self) -> Cow<'_, Path> {
resolve_path(&self.project)
}
}
#[derive(Debug, Clone, Copy)]
pub enum UploadKind {
Place,
Model,
}
impl FromStr for UploadKind {
type Err = UploadKindParseError;
fn from_str(source: &str) -> Result<Self, Self::Err> {
match source {
"place" => Ok(UploadKind::Place),
"model" => Ok(UploadKind::Model),
_ => Err(UploadKindParseError {
attempted: source.to_owned(),
}),
}
}
}
#[derive(Debug)]
pub struct UploadKindParseError {
attempted: String,
}
impl Error for UploadKindParseError {}
impl fmt::Display for UploadKindParseError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"Invalid upload kind '{}'. Valid kinds are: place, model",
self.attempted
)
}
}
fn resolve_path(path: &Path) -> Cow<'_, Path> {
if path.is_absolute() {
Cow::Borrowed(path)
} else {
Cow::Owned(env::current_dir().unwrap().join(path))
}
}
#[derive(Debug, StructOpt)]
pub enum PluginSubcommand {
Install,
Uninstall,
}
#[derive(Debug, StructOpt)]
pub struct PluginCommand {
#[structopt(subcommand)]
subcommand: PluginSubcommand,
}