Set up plugin structure

This commit is contained in:
Jack Grigg
2020-12-31 21:17:35 +00:00
parent 05f7f1b076
commit 0052d91aad
5 changed files with 616 additions and 2 deletions
+31
View File
@@ -0,0 +1,31 @@
use std::fmt;
use std::io;
pub enum Error {
Io(io::Error),
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
// Rust only supports `fn main() -> Result<(), E: Debug>`, so we implement `Debug`
// manually to provide the error output we want.
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => writeln!(f, "Failed to set up YubiKey: {}", e)?,
}
writeln!(f)?;
writeln!(
f,
"[ Did this not do what you expected? Could an error be more useful? ]"
)?;
write!(
f,
"[ Tell us: https://str4d.xyz/age-plugin-yubikey/report ]"
)
}
}
+35 -2
View File
@@ -1,3 +1,36 @@
fn main() {
println!("Hello, world!");
use age_plugin::run_state_machine;
use gumdrop::Options;
mod error;
mod plugin;
use error::Error;
#[derive(Debug, Options)]
struct PluginOptions {
#[options(help = "Print this help message and exit.")]
help: bool,
#[options(
help = "Run the given age plugin state machine. Internal use only.",
meta = "STATE-MACHINE",
no_short
)]
age_plugin: Option<String>,
}
fn main() -> Result<(), Error> {
let opts = PluginOptions::parse_args_default_or_exit();
if let Some(state_machine) = opts.age_plugin {
run_state_machine(
&state_machine,
|| plugin::RecipientPlugin::default(),
|| plugin::IdentityPlugin::default(),
)?;
Ok(())
} else {
// TODO: CLI identity generation
Ok(())
}
}
+43
View File
@@ -0,0 +1,43 @@
use age_core::format::{FileKey, Stanza};
use age_plugin::{
identity::{self, Callbacks, IdentityPluginV1},
recipient::{self, RecipientPluginV1},
};
use std::collections::HashMap;
use std::io;
#[derive(Debug, Default)]
pub(crate) struct RecipientPlugin {}
impl RecipientPluginV1 for RecipientPlugin {
fn add_recipients<'a, I: Iterator<Item = &'a str>>(
&mut self,
recipients: I,
) -> Result<(), Vec<recipient::Error>> {
todo!()
}
fn wrap_file_key(&mut self, file_key: &FileKey) -> Result<Vec<Stanza>, Vec<recipient::Error>> {
todo!()
}
}
#[derive(Debug, Default)]
pub(crate) struct IdentityPlugin {}
impl IdentityPluginV1 for IdentityPlugin {
fn add_identities<'a, I: Iterator<Item = &'a str>>(
&mut self,
identities: I,
) -> Result<(), Vec<identity::Error>> {
todo!()
}
fn unwrap_file_keys(
&mut self,
files: Vec<Vec<Stanza>>,
mut callbacks: impl Callbacks,
) -> io::Result<HashMap<usize, Result<FileKey, Vec<identity::Error>>>> {
todo!()
}
}