Add trait implementations for Goal

This commit is contained in:
i1i1 2023-08-09 22:54:17 +03:00 committed by Zhaofeng Li
parent 197010c492
commit 9ddc53136a
3 changed files with 33 additions and 11 deletions

View file

@ -1,5 +1,6 @@
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr;
use clap::{ use clap::{
builder::{ArgPredicate, PossibleValuesParser, ValueParser}, builder::{ArgPredicate, PossibleValuesParser, ValueParser},

View file

@ -1,5 +1,6 @@
use regex::Regex; use regex::Regex;
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr;
use clap::{builder::PossibleValuesParser, Arg, ArgMatches, Command as ClapCommand}; use clap::{builder::PossibleValuesParser, Arg, ArgMatches, Command as ClapCommand};
use tokio::fs; use tokio::fs;

View file

@ -1,7 +1,9 @@
//! Deployment goals. //! Deployment goals.
use std::str::FromStr;
/// The goal of a deployment. /// The goal of a deployment.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub enum Goal { pub enum Goal {
/// Build the configurations only. /// Build the configurations only.
Build, Build,
@ -10,6 +12,7 @@ pub enum Goal {
Push, Push,
/// Make the configuration the boot default and activate now. /// Make the configuration the boot default and activate now.
#[default]
Switch, Switch,
/// Make the configuration the boot default. /// Make the configuration the boot default.
@ -25,20 +28,37 @@ pub enum Goal {
UploadKeys, UploadKeys,
} }
impl Goal { impl FromStr for Goal {
pub fn from_str(s: &str) -> Option<Self> { type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
"build" => Some(Self::Build), "build" => Ok(Self::Build),
"push" => Some(Self::Push), "push" => Ok(Self::Push),
"switch" => Some(Self::Switch), "switch" => Ok(Self::Switch),
"boot" => Some(Self::Boot), "boot" => Ok(Self::Boot),
"test" => Some(Self::Test), "test" => Ok(Self::Test),
"dry-activate" => Some(Self::DryActivate), "dry-activate" => Ok(Self::DryActivate),
"keys" => Some(Self::UploadKeys), "keys" => Ok(Self::UploadKeys),
_ => None, _ => Err("Not one of [build, push, switch, boot, test, dry-activate, keys]."),
} }
} }
}
impl std::fmt::Display for Goal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Build => "build",
Self::Push => "push",
Self::Switch => "switch",
Self::Boot => "boot",
Self::Test => "test",
Self::DryActivate => "dry-activate",
Self::UploadKeys => "keys",
})
}
}
impl Goal {
pub fn as_str(&self) -> Option<&'static str> { pub fn as_str(&self) -> Option<&'static str> {
use Goal::*; use Goal::*;
match self { match self {