~ireas/nitrokey-rs

35be248b1666485dc10b502400971191ecfba6fb — Robin Krahl 3 months ago b2aedd0
Use Default::default for nitrokey-sys structs

Since version 3.6.0, nitrokey-sys derives the Default trait for all
structs.  This allows us to simplify code snippets where we have to
construct an empty struct that is then filled by nitrokey-sys.
3 files changed, 7 insertions(+), 55 deletions(-)

M CHANGELOG.md
M src/device/pro.rs
M src/device/storage.rs
M CHANGELOG.md => CHANGELOG.md +2 -0
@@ 16,6 16,8 @@ SPDX-License-Identifier: CC0-1.0
  - Use `NK_free_password_safe_slot_status` to free the pointer returned by
    `NK_get_password_safe_slot_status` in `PasswordSafe::get_slot_status`.
  - Use the `NK_config` struct for configuration handling.
  - Use the derived `Default` implementation for the structs defined by
    `nitrokey-sys`.

# v0.7.1 (2020-08-30)
- Remove the custom `std::error::Error::source` implementation for

M src/device/pro.rs => src/device/pro.rs +1 -9
@@ 76,15 76,7 @@ impl<'a> Device<'a> for Pro<'a> {
    }

    fn get_status(&self) -> Result<Status, Error> {
        let mut raw_status = nitrokey_sys::NK_status {
            firmware_version_major: 0,
            firmware_version_minor: 0,
            serial_number_smart_card: 0,
            config_numlock: 0,
            config_capslock: 0,
            config_scrolllock: 0,
            otp_user_password: false,
        };
        let mut raw_status = nitrokey_sys::NK_status::default();
        get_command_result(unsafe { nitrokey_sys::NK_get_status(&mut raw_status) })?;
        Ok(raw_status.into())
    }

M src/device/storage.rs => src/device/storage.rs +4 -46
@@ 549,24 549,7 @@ impl<'a> Storage<'a> {
    /// # }
    /// ```
    pub fn get_storage_status(&self) -> Result<StorageStatus, Error> {
        let mut raw_status = nitrokey_sys::NK_storage_status {
            unencrypted_volume_read_only: false,
            unencrypted_volume_active: false,
            encrypted_volume_read_only: false,
            encrypted_volume_active: false,
            hidden_volume_read_only: false,
            hidden_volume_active: false,
            firmware_version_major: 0,
            firmware_version_minor: 0,
            firmware_locked: false,
            serial_number_sd_card: 0,
            serial_number_smart_card: 0,
            user_retry_count: 0,
            admin_retry_count: 0,
            new_sd_card_found: false,
            filled_with_random: false,
            stick_initialized: false,
        };
        let mut raw_status = nitrokey_sys::NK_storage_status::default();
        let raw_result = unsafe { nitrokey_sys::NK_get_status_storage(&mut raw_status) };
        get_command_result(raw_result).map(|_| StorageStatus::from(raw_status))
    }


@@ 594,21 577,7 @@ impl<'a> Storage<'a> {
    /// # }
    /// ```
    pub fn get_production_info(&self) -> Result<StorageProductionInfo, Error> {
        let mut raw_data = nitrokey_sys::NK_storage_ProductionTest {
            FirmwareVersion_au8: [0, 2],
            FirmwareVersionInternal_u8: 0,
            SD_Card_Size_u8: 0,
            CPU_CardID_u32: 0,
            SmartCardID_u32: 0,
            SD_CardID_u32: 0,
            SC_UserPwRetryCount: 0,
            SC_AdminPwRetryCount: 0,
            SD_Card_ManufacturingYear_u8: 0,
            SD_Card_ManufacturingMonth_u8: 0,
            SD_Card_OEM_u16: 0,
            SD_WriteSpeed_u16: 0,
            SD_Card_Manufacturer_u8: 0,
        };
        let mut raw_data = nitrokey_sys::NK_storage_ProductionTest::default();
        let raw_result = unsafe { nitrokey_sys::NK_get_storage_production_info(&mut raw_data) };
        get_command_result(raw_result).map(|_| StorageProductionInfo::from(raw_data))
    }


@@ 666,10 635,7 @@ impl<'a> Storage<'a> {
    /// # Ok::<(), nitrokey::Error>(())
    /// ```
    pub fn get_sd_card_usage(&self) -> Result<ops::Range<u8>, Error> {
        let mut usage_data = nitrokey_sys::NK_SD_usage_data {
            write_level_min: 0,
            write_level_max: 0,
        };
        let mut usage_data = nitrokey_sys::NK_SD_usage_data::default();
        let result = unsafe { nitrokey_sys::NK_get_SD_usage_data(&mut usage_data) };
        match get_command_result(result) {
            Ok(_) => {


@@ 811,15 777,7 @@ impl<'a> Device<'a> for Storage<'a> {
        // [0] https://github.com/Nitrokey/nitrokey-storage-firmware/issues/96
        // [1] https://github.com/Nitrokey/libnitrokey/issues/166

        let mut raw_status = nitrokey_sys::NK_status {
            firmware_version_major: 0,
            firmware_version_minor: 0,
            serial_number_smart_card: 0,
            config_numlock: 0,
            config_capslock: 0,
            config_scrolllock: 0,
            otp_user_password: false,
        };
        let mut raw_status = nitrokey_sys::NK_status::default();
        get_command_result(unsafe { nitrokey_sys::NK_get_status(&mut raw_status) })?;
        let mut status = Status::from(raw_status);