where
T: fmt::Debug,
{
fn unwrap_str_err(self) -> String {
match self.unwrap_err() {
crate::Error::ClapError(err) => {
if err.use_stderr() {
err.message
} else {
String::new()
}
}
crate::Error::Error(err) => err,
err => panic!("Unexpected error variant found: {:?}", err),
}
}
fn unwrap_cmd_err(self) -> (Option<&'static str>, nitrokey::CommandError) {
match self.unwrap_err() {
crate::Error::NitrokeyError(ctx, err) => match err {
nitrokey::Error::CommandError(err) => (ctx, err),
err => panic!("Unexpected error variant found: {:?}", err),
},
err => panic!("Unexpected error variant found: {:?}", err),
}
}
fn unwrap_lib_err(self) -> (Option<&'static str>, nitrokey::LibraryError) {
match self.unwrap_err() {
crate::Error::NitrokeyError(ctx, err) => match err {
nitrokey::Error::LibraryError(err) => (ctx, err),
err => panic!("Unexpected error variant found: {:?}", err),
},
err => panic!("Unexpected error variant found: {:?}", err),
}
}
}
struct Builder(Nitrocli);
impl Builder {
/// Set the model to use.
fn model(mut self, model: nitrokey::Model) -> Self {
self.0.model = Some(model);
self
}
/// Set the `PATH` used for looking up extensions.
fn path(mut self, path: P) -> Self
where
P: Into,
{
self.0.path = Some(path.into());
self
}
/// Set the password to use for certain operations.
fn password(mut self, password: P) -> Self
where
P: Into,
{
self.0.password = Some(password.into());
self
}
/// Build the final `Nitrocli` object.
fn build(self) -> Nitrocli {
self.0
}
}
struct Nitrocli {
model: Option,
path: Option,
admin_pin: Option,
user_pin: Option,
new_admin_pin: Option,
new_user_pin: Option,
password: Option,
}
impl Nitrocli {
pub fn new() -> Self {
Self {
model: None,
path: None,
admin_pin: Some(nitrokey::DEFAULT_ADMIN_PIN.into()),
user_pin: Some(nitrokey::DEFAULT_USER_PIN.into()),
new_admin_pin: None,
new_user_pin: None,
password: None,
}
}
pub fn make() -> Builder {
Builder(Self::new())
}
pub fn admin_pin(&mut self, pin: impl Into) {
self.admin_pin = Some(pin.into())
}
pub fn new_admin_pin(&mut self, pin: impl Into) {
self.new_admin_pin = Some(pin.into())
}
pub fn user_pin(&mut self, pin: impl Into) {
self.user_pin = Some(pin.into())
}
pub fn new_user_pin(&mut self, pin: impl Into) {
self.new_user_pin = Some(pin.into())
}
fn model_to_arg(model: nitrokey::Model) -> &'static str {
match model {
nitrokey::Model::Pro => "--model=pro",
nitrokey::Model::Storage => "--model=storage",
}
}
fn do_run(&mut self, args: &[&str], f: F) -> (R, Vec, Vec)
where
F: FnOnce(&mut crate::RunCtx<'_>, Vec) -> R,
{
let args = ["nitrocli"]
.iter()
.cloned()
.chain(self.model.map(Self::model_to_arg))
.chain(args.iter().cloned())
.map(ToOwned::to_owned)
.collect();
let mut stdout = Vec::new();
let mut stderr = Vec::new();
let ctx = &mut crate::RunCtx {
stdout: &mut stdout,
stderr: &mut stderr,
path: self.path.clone(),
admin_pin: self.admin_pin.clone(),
user_pin: self.user_pin.clone(),
new_admin_pin: self.new_admin_pin.clone(),
new_user_pin: self.new_user_pin.clone(),
password: self.password.clone(),
no_cache: true,
};
(f(ctx, args), stdout, stderr)
}
/// Run `nitrocli`'s `run` function.
pub fn run(&mut self, args: &[&str]) -> (i32, Vec, Vec) {
self.do_run(args, |c, a| crate::run(c, a))
}
/// Run `nitrocli`'s `handle_arguments` function.
pub fn handle(&mut self, args: &[&str]) -> crate::Result {
let (res, out, _) = self.do_run(args, |c, a| crate::handle_arguments(c, a));
res.map(|_| String::from_utf8_lossy(&out).into_owned())
}
pub fn model(&self) -> Option {
self.model
}
}