forked from DGNum/colmena
key: Serialize KeySource through an intermediate struct
Well, still better than `if/else`-ing all the way. Also we definitely need unit tests. See #8.
This commit is contained in:
parent
2886662e18
commit
e49e9367c0
1 changed files with 34 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
convert::TryFrom,
|
||||||
io::{self, Cursor},
|
io::{self, Cursor},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
process::Stdio,
|
process::Stdio,
|
||||||
|
@ -14,6 +15,7 @@ use tokio::{
|
||||||
use validator::{Validate, ValidationError};
|
use validator::{Validate, ValidationError};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(try_from = "KeySources")]
|
||||||
enum KeySource {
|
enum KeySource {
|
||||||
#[serde(rename = "text")]
|
#[serde(rename = "text")]
|
||||||
Text(String),
|
Text(String),
|
||||||
|
@ -25,6 +27,38 @@ enum KeySource {
|
||||||
File(PathBuf),
|
File(PathBuf),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<KeySources> for KeySource {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(ks: KeySources) -> Result<Self, Self::Error> {
|
||||||
|
match (ks.text, ks.command, ks.file) {
|
||||||
|
(Some(text), None, None) => {
|
||||||
|
Ok(KeySource::Text(text))
|
||||||
|
}
|
||||||
|
(None, Some(command), None) => {
|
||||||
|
Ok(KeySource::Command(command))
|
||||||
|
}
|
||||||
|
(None, None, Some(file)) => {
|
||||||
|
Ok(KeySource::File(file))
|
||||||
|
}
|
||||||
|
x => {
|
||||||
|
Err(format!("Somehow 0 or more than 1 key source was specified: {:?}", x))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
struct KeySources {
|
||||||
|
text: Option<String>,
|
||||||
|
|
||||||
|
#[serde(rename = "keyCommand")]
|
||||||
|
command: Option<Vec<String>>,
|
||||||
|
|
||||||
|
#[serde(rename = "keyFile")]
|
||||||
|
file: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
|
||||||
pub struct Key {
|
pub struct Key {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue