Add algorithm to the CLI

This commit is contained in:
Rodolphe Bréard 2023-03-26 17:15:41 +02:00
parent c38fa94f1d
commit 3dba0451cd
3 changed files with 48 additions and 0 deletions

40
src/algorithm.rs Normal file
View file

@ -0,0 +1,40 @@
use std::str::FromStr;
#[derive(Clone, Copy, Debug)]
pub enum Algorithm {
Ed25519Sha256,
Rsa2048Sha256,
Rsa3072Sha256,
Rsa4096Sha256,
}
impl Default for Algorithm {
fn default() -> Self {
Self::Ed25519Sha256
}
}
impl ToString for Algorithm {
fn to_string(&self) -> String {
match self {
Self::Ed25519Sha256 => String::from("ed25519-sha256"),
Self::Rsa2048Sha256 => String::from("rsa2048-sha256"),
Self::Rsa3072Sha256 => String::from("rsa3072-sha256"),
Self::Rsa4096Sha256 => String::from("rsa4096-sha256"),
}
}
}
impl FromStr for Algorithm {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"ed25519-sha256" => Ok(Self::Ed25519Sha256),
"rsa2048-sha256" => Ok(Self::Rsa2048Sha256),
"rsa3072-sha256" => Ok(Self::Rsa3072Sha256),
"rsa4096-sha256" => Ok(Self::Rsa4096Sha256),
_ => Err(format!("{s}: invalid signing algorithm")),
}
}
}

View file

@ -1,3 +1,4 @@
use crate::algorithm::Algorithm;
use crate::canonicalization::Canonicalization;
use clap::Parser;
use std::collections::HashSet;
@ -9,6 +10,8 @@ use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Config {
#[arg(short, long, default_value_t = Algorithm::default())]
algorithm: Algorithm,
#[arg(short, long, default_value_t = Canonicalization::default())]
canonicalization: Canonicalization,
#[arg(short, long)]
@ -37,6 +40,10 @@ impl Config {
Ok(cnf)
}
pub fn algorithm(&self) -> Algorithm {
self.algorithm
}
pub fn canonicalization(&self) -> Canonicalization {
self.canonicalization
}

View file

@ -1,3 +1,4 @@
mod algorithm;
mod canonicalization;
mod config;
mod entry;