use super::KeyType;
use crate::{
chain,
error::{Error, ErrorKind::ConfigError},
prelude::*,
};
use serde::Deserialize;
use std::{
path::{Path, PathBuf},
str::FromStr,
};
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct SoftsignConfig {
pub chain_ids: Vec<chain::Id>,
#[serde(default)]
pub key_type: KeyType,
pub key_format: Option<KeyFormat>,
pub path: SoftPrivateKey,
}
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct SoftPrivateKey(PathBuf);
impl AsRef<Path> for SoftPrivateKey {
fn as_ref(&self) -> &Path {
self.0.as_ref()
}
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, Hash, PartialEq)]
pub enum KeyFormat {
#[serde(rename = "base64")]
#[default]
Base64,
#[serde(rename = "json")]
Json,
}
impl FromStr for KeyFormat {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
let format = match s {
"base64" => KeyFormat::Base64,
"json" => KeyFormat::Json,
other => fail!(ConfigError, "invalid key format: {}", other),
};
Ok(format)
}
}