Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup + readability improvements #110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/apps/oath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Credential {
pub fn id(&self) -> String {
let mut id = String::new();
if let Kind::Totp(totp) = self.kind {
if totp != Default::default() {
if totp != Totp::default() {
write!(id, "{}/", totp.period).ok();
}
}
Expand Down Expand Up @@ -325,7 +325,7 @@ impl Decodable<'_> for Tag {

impl App<'_> {
/// Returns the credential ID.
pub fn register(&mut self, credential: Credential) -> Result<String> {
pub fn register(&mut self, credential: &Credential) -> Result<String> {
info!(" registering credential {:?}", &credential);
// data = Tlv(TAG_NAME, cred_id) + Tlv(
// TAG_KEY,
Expand Down Expand Up @@ -409,7 +409,7 @@ impl App<'_> {
assert_eq!(response[1], 5);
let digits = response[2] as usize;
let truncated_code = u32::from_be_bytes(response[3..].try_into().unwrap());
let code = (truncated_code & 0x7FFFFFFF) % 10u32.pow(digits as _);
let code = (truncated_code & 0x7FFF_FFFF) % 10u32.pow(digits as _);
Ok(format!("{:0digits$}", code, digits = digits))
}

Expand Down
2 changes: 1 addition & 1 deletion src/bin/solo2/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn try_main(args: cli::Cli) -> anyhow::Result<()> {
algorithm: digest,
digits: args.digits,
};
let credential_id = app.register(credential)?;
let credential_id = app.register(&credential)?;
println!("{}", credential_id);
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,22 @@ impl UuidSelectable for Solo2 {
fn list() -> Vec<Self> {
// iterator/lifetime woes avoiding the explicit for loop
let mut ctaps = BTreeMap::new();
for mut device in ctap::list().into_iter() {
for mut device in ctap::list() {
if let Ok(uuid) = device.try_uuid() {
ctaps.insert(uuid, device);
}
}
// iterator/lifetime woes avoiding the explicit for loop
let mut pcscs = BTreeMap::new();
for mut device in pcsc::list().into_iter() {
for mut device in pcsc::list() {
if let Ok(uuid) = device.try_uuid() {
pcscs.insert(uuid, device);
}
}

let uuids = BTreeSet::from_iter(ctaps.keys().chain(pcscs.keys()).copied());
let uuids: BTreeSet<Uuid> = ctaps.keys().chain(pcscs.keys()).copied().collect();
let mut devices = Vec::new();
for uuid in uuids.iter() {
for uuid in &uuids {
// a bit roundabout, but hey, "it works".
let mut device = Self {
ctap: ctaps.remove(uuid),
Expand Down
4 changes: 1 addition & 3 deletions src/device/ctap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ pub struct Device {
}

pub fn list() -> Vec<Device> {
Session::new()
.map(|session| session.devices())
.unwrap_or_else(|_| vec![])
Session::new().map_or_else(|_| vec![], |session| session.devices())
}

impl From<hidapi::DeviceInfo> for Info {
Expand Down
4 changes: 1 addition & 3 deletions src/device/pcsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ pub struct Device {
}

pub fn list() -> Vec<Device> {
Session::new()
.map(|session| session.devices())
.unwrap_or_else(|_| vec![])
Session::new().map_or_else(|_| vec![], |session| session.devices())
}

impl Session {
Expand Down
6 changes: 1 addition & 5 deletions src/firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ impl Firmware {
// pub fn verify(&self) -> Result<()> {
// }

pub fn write_to<'a>(
&self,
bootloader: &lpc55::Bootloader,
progress: Option<&'a dyn Fn(usize)>,
) {
pub fn write_to(&self, bootloader: &lpc55::Bootloader, progress: Option<&dyn Fn(usize)>) {
bootloader.receive_sb_file(&self.content, progress);
}

Expand Down
2 changes: 1 addition & 1 deletion src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Transport for ctap::Device {
use ctap::{Code, Command};
let init = self.init()?;
let command = Command::new(Code::from(instruction)).with_data(data);
ctap::Device::call(self, init.channel, command)
ctap::Device::call(self, init.channel, &command)
}

fn call_iso(&mut self, _: u8, _: u8, _: u8, _: u8, _: &[u8]) -> Result<Vec<u8>> {
Expand Down
8 changes: 4 additions & 4 deletions src/transport/ctap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Channel {
}

impl Device {
pub fn call(&self, channel: Channel, request: Command) -> Result<Vec<u8>> {
pub fn call(&self, channel: Channel, request: &Command) -> Result<Vec<u8>> {
let result: Result<Vec<()>> = request
.packets(channel)
.enumerate()
Expand Down Expand Up @@ -260,7 +260,7 @@ impl Device {
getrandom::getrandom(&mut nonce).unwrap();
// dbg!(hex::encode(&nonce));
let command = Command::new(Code::Init).with_data(&nonce);
let response = self.call(Channel::BROADCAST, command)?;
let response = self.call(Channel::BROADCAST, &command)?;
// let mut packet = [0u8; 64];
// let read = self.device.read(&mut packet)?;
assert_eq!(response.len(), 17);
Expand All @@ -286,15 +286,15 @@ impl Device {

pub fn ping(&self, channel: Channel, data: &[u8]) -> Result<Vec<u8>> {
let command = Command::new(Code::Ping).with_data(data);
let response = self.call(channel, command)?;
let response = self.call(channel, &command)?;

assert_eq!(data, response);
Ok(response)
}

pub fn wink(&self, channel: Channel) -> Result<Vec<u8>> {
let command = Command::new(Code::Wink);
let response = self.call(channel, command)?;
let response = self.call(channel, &command)?;
Ok(response)
}
}
Expand Down