From 9ddc53136a3bf622306b8ab50e90be832bebc35b Mon Sep 17 00:00:00 2001 From: i1i1 Date: Wed, 9 Aug 2023 22:54:17 +0300 Subject: [PATCH] Add trait implementations for `Goal` --- src/command/apply.rs | 1 + src/command/apply_local.rs | 1 + src/nix/deployment/goal.rs | 42 ++++++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/command/apply.rs b/src/command/apply.rs index 1bcf110..626022a 100644 --- a/src/command/apply.rs +++ b/src/command/apply.rs @@ -1,5 +1,6 @@ use std::env; use std::path::PathBuf; +use std::str::FromStr; use clap::{ builder::{ArgPredicate, PossibleValuesParser, ValueParser}, diff --git a/src/command/apply_local.rs b/src/command/apply_local.rs index b712a7e..6111e98 100644 --- a/src/command/apply_local.rs +++ b/src/command/apply_local.rs @@ -1,5 +1,6 @@ use regex::Regex; use std::collections::HashMap; +use std::str::FromStr; use clap::{builder::PossibleValuesParser, Arg, ArgMatches, Command as ClapCommand}; use tokio::fs; diff --git a/src/nix/deployment/goal.rs b/src/nix/deployment/goal.rs index c3c51ea..a7af4d5 100644 --- a/src/nix/deployment/goal.rs +++ b/src/nix/deployment/goal.rs @@ -1,7 +1,9 @@ //! Deployment goals. +use std::str::FromStr; + /// The goal of a deployment. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)] pub enum Goal { /// Build the configurations only. Build, @@ -10,6 +12,7 @@ pub enum Goal { Push, /// Make the configuration the boot default and activate now. + #[default] Switch, /// Make the configuration the boot default. @@ -25,20 +28,37 @@ pub enum Goal { UploadKeys, } -impl Goal { - pub fn from_str(s: &str) -> Option { +impl FromStr for Goal { + type Err = &'static str; + fn from_str(s: &str) -> Result { match s { - "build" => Some(Self::Build), - "push" => Some(Self::Push), - "switch" => Some(Self::Switch), - "boot" => Some(Self::Boot), - "test" => Some(Self::Test), - "dry-activate" => Some(Self::DryActivate), - "keys" => Some(Self::UploadKeys), - _ => None, + "build" => Ok(Self::Build), + "push" => Ok(Self::Push), + "switch" => Ok(Self::Switch), + "boot" => Ok(Self::Boot), + "test" => Ok(Self::Test), + "dry-activate" => Ok(Self::DryActivate), + "keys" => Ok(Self::UploadKeys), + _ => 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> { use Goal::*; match self {