You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
2.5 KiB

//! Parsed config
use super::*;
pub mod op {
use super::*;
/// The crypt mode
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum Mode {
Encrypt,
Decrypt,
}
#[derive(Debug, Clone,PartialEq, Eq, Hash)]
pub enum KeyFormat
{
Bin,
Text,
PEM,
}
impl Default for KeyFormat
{
#[inline]
fn default() -> Self
{
Self::Bin
}
}
#[derive(Debug, Clone,PartialEq, Eq, Default)]
pub struct Normal {
pub rsa: Vec<(String, Password)>,
pub sign: Vec<(String, Password)>,
pub aes: Vec<(String, Password)>,
pub in_place: bool,
pub mode: Option<Mode>,// `None` is autodetect mode per file
pub files: Vec<(String, Option<String>, Option<Mode> /* `None` is use global*/)>,
}
#[derive(Debug, Clone,PartialEq, Eq)]
pub enum GenerateKey {
Rsa(Rsa),
Aes(Aes),
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum Password
{
No,
Yes,
Specific(String),
}
impl Default for Password
{
#[inline]
fn default() -> Self
{
Self::No
}
}
#[derive(Debug ,PartialEq, Eq, Clone, Hash, Default, Serialize, Deserialize)]
pub struct KeyDescription
{
pub name: String,
pub email: String,
pub description: String,
pub other: String,
}
#[derive(Debug, Clone,PartialEq, Eq)]
pub struct Rsa
{
pub input: Option<(String, Password)>,
pub output_priv: String,
pub output_pub: Option<String>,
pub password: Password,
pub format: KeyFormat,
pub description: KeyDescription,
}
#[derive(Debug,Clone, PartialEq, Eq)]
pub struct Aes
{
pub input: Option<(String, Password)>,
pub output: String,
pub password: Password,
pub format: KeyFormat,
pub description: KeyDescription,
}
}
#[derive(Debug, Clone,PartialEq, Eq)]
pub enum Operation
{
Normal(op::Normal),
GenerateKey(op::GenerateKey),
KeyInfo(Vec<(String, op::Password)>),
Help,
}
#[derive(Debug, Clone,PartialEq, Eq)]
pub enum KeyKind
{
RsaPublic,
RsaPrivate,
Aes,
}
pub use op::Password;
impl Password
{
/// Consume into real password
#[instrument]
pub fn into_password(self, prompt: impl AsRef<str>) -> eyre::Result<Option<String>>
{
Ok(match self {
Self::No => None,
Self::Yes => Some(read_password(prompt.as_ref())?),
Self::Specific(passwd) => Some(passwd),
})
}
}
/// Read password from stdin
#[instrument(err)]
fn read_password(prompt: &str) -> eyre::Result<String>
{
rpassword::prompt_password_stderr(prompt)
.wrap_err(eyre!("Failed to read password from stdin"))
}