From d64d379e97a600c08b1e145f6f986aaf95ddcc8f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 10 Jan 2019 23:15:41 +0000 Subject: Refactor get_device to use nitrokey::connect_model nitrokey 0.3.1 introduced the connect_model function that connects to a specific model given by an enum variant and returns a DeviceWrapper. This new function allows us to remove the manual selection of a connection method from the get_device function. We only have to implement From for nitrokey::Model to be able to convert our model enum to nitrokey's model enum. --- nitrocli/Cargo.toml | 2 +- nitrocli/src/args.rs | 9 +++++++++ nitrocli/src/commands.rs | 7 +------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/nitrocli/Cargo.toml b/nitrocli/Cargo.toml index 0c567c7..ae27e63 100644 --- a/nitrocli/Cargo.toml +++ b/nitrocli/Cargo.toml @@ -54,7 +54,7 @@ version = "0.2" path = "../libc" [dependencies.nitrokey] -version = "0.3" +version = "0.3.1" path = "../nitrokey" [dev-dependencies.nitrokey-test] diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index 6f02832..246490e 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -64,6 +64,15 @@ Enum! {DeviceModel, [ Storage => "storage" ]} +impl From for nitrokey::Model { + fn from(model: DeviceModel) -> nitrokey::Model { + match model { + DeviceModel::Pro => nitrokey::Model::Pro, + DeviceModel::Storage => nitrokey::Model::Storage, + } + } +} + /// A top-level command for nitrocli. Enum! {Command, [ Config => "config", diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index ed3c2c4..b37f9c5 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -59,12 +59,7 @@ fn get_device(ctx: &mut args::ExecCtx<'_>) -> Result { set_log_level(ctx); match ctx.model { - Some(model) => match model { - args::DeviceModel::Pro => nitrokey::Pro::connect().map(nitrokey::DeviceWrapper::Pro), - args::DeviceModel::Storage => { - nitrokey::Storage::connect().map(nitrokey::DeviceWrapper::Storage) - } - }, + Some(model) => nitrokey::connect_model(model.into()), None => nitrokey::connect(), } .map_err(|_| Error::Error("Nitrokey device not found".to_string())) -- cgit v1.2.3