Skip to content

Commit

Permalink
Merge branch 'winapi'. Bump version to 0.52.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gentoo90 committed Nov 19, 2023
2 parents fc6521e + 4b0ba3e commit 1c56127
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 17 deletions.
28 changes: 26 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,36 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Restrict iana-time-zone version
if: matrix.restrict_deps_versions
uses: actions-rs/cargo@v1
with:
command: update
args: --package iana-time-zone --precise 0.1.54
- name: Restrict js-sys version
if: matrix.restrict_deps_versions
uses: actions-rs/cargo@v1
with:
command: update
args: --package js-sys --precise 0.3.50
- name: Restrict wasm-bindgen version
if: matrix.restrict_deps_versions
uses: actions-rs/cargo@v1
with:
command: update
args: --package wasm-bindgen --precise 0.2.84
- name: Restrict serde_bytes version
if: matrix.restrict_deps_versions
uses: actions-rs/cargo@v1
with:
command: update
args: --package serde_bytes --precise 0.11.10
- name: Restrict serde_json version
if: matrix.restrict_deps_versions
uses: actions-rs/cargo@v1
with:
command: update
args: --package serde_json --precise 1.0.72
- name: Restrict serde version
if: matrix.restrict_deps_versions
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -88,13 +112,13 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: update
args: --package windows-targets --precise 0.48.1
args: --package windows-targets:0.48.5 --precise 0.48.5
- name: Restrict windows_x86_64_msvc version
if: matrix.restrict_deps_versions
uses: actions-rs/cargo@v1
with:
command: update
args: --package windows_x86_64_msvc --precise 0.48.0
args: --package windows_x86_64_msvc:0.48.5 --precise 0.48.5
- name: Check formatting
if: matrix.lint
uses: actions-rs/cargo@v1
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## 0.52.0
* Breaking change: `.commit()` and `.rollback()` now consume the transaction ([#62](https://github.com/gentoo90/winreg-rs/issues/62))
* Add `RegKey::rename_subkey()` method ([#58](https://github.com/gentoo90/winreg-rs/issues/58))
* Make serialization modules public ([#59](https://github.com/gentoo90/winreg-rs/issues/59))
* Fix UB in `FromRegValue` for `u32` and `u64` ([#61](https://github.com/gentoo90/winreg-rs/issues/61))

## 0.14.0
* Breaking change: increase MSRV to 1.34
* Fix UB in `FromRegValue` for `u32` and `u64` ([#61](https://github.com/gentoo90/winreg-rs/issues/61))

## 0.13.0

* Breaking change: `.commit()` and `.rollback()` now consume the transaction ([#62](https://github.com/gentoo90/winreg-rs/issues/62))
* Add `RegKey::rename_subkey()` method ([#58](https://github.com/gentoo90/winreg-rs/issues/58))
* Make serialization modules public ([#59](https://github.com/gentoo90/winreg-rs/issues/59))

## 0.12.0, 0.51.0

* Breaking change: fix `std::fmt::Display` implementation for `RegValue` ([#52](https://github.com/gentoo90/winreg-rs/issues/52))
Expand Down
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "winreg"
edition = "2018"
version = "0.51.0"
version = "0.52.0"
authors = ["Igor Shaula <gentoo90@gmail.com>"]
license = "MIT"
description = "Rust bindings to MS Windows Registry API"
Expand All @@ -28,6 +28,8 @@ serde = { version = "1", optional = true }
rand = "0.3"
tempfile = "~3.0"
serde_derive = "1"
serde_json = "1"
serde-transcode = "1"
serde_bytes = "0.11"

[features]
Expand Down Expand Up @@ -56,6 +58,10 @@ required-features = ["serialization-serde"]
name = "transacted_serialization"
required-features = ["serialization-serde"]

[[example]]
name = "reg2json"
required-features = ["serialization-serde"]

[[example]]
name = "map_key_serialization"
required-features = ["serialization-serde"]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Rust bindings to MS Windows Registry API. Work in progress.

Current features:
* Basic registry operations:
* open/create/delete keys
* open/create/delete/rename keys
* load application hive from a file
* read and write values
* seamless conversion between `REG_*` types and rust primitives
Expand All @@ -36,7 +36,7 @@ Current features:
```toml
# Cargo.toml
[dependencies]
winreg = "0.51"
winreg = "0.52"
```

```rust
Expand Down Expand Up @@ -138,7 +138,7 @@ fn main() -> io::Result<()> {
```toml
# Cargo.toml
[dependencies]
winreg = { version = "0.51", features = ["transactions"] }
winreg = { version = "0.52", features = ["transactions"] }
```

```rust
Expand Down Expand Up @@ -179,7 +179,7 @@ fn main() -> io::Result<()> {
```toml
# Cargo.toml
[dependencies]
winreg = { version = "0.51", features = ["serialization-serde"] }
winreg = { version = "0.52", features = ["serialization-serde"] }
serde = "1"
serde_derive = "1"
```
Expand Down
20 changes: 20 additions & 0 deletions examples/reg2json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2023, Igor Shaula
// Licensed under the MIT License <LICENSE or
// http://opensource.org/licenses/MIT>. This file
// may not be copied, modified, or distributed
// except according to those terms.
extern crate serde_transcode;
extern crate winreg;
use std::error::Error;
use winreg::enums::*;
use winreg::RegKey;

fn main() -> Result<(), Box<dyn Error>> {
let key = RegKey::predef(HKEY_CLASSES_ROOT).open_subkey("Folder")?;

let mut deserializer = winreg::decoder::Decoder::from_key(&key)?;
let mut serializer = serde_json::Serializer::pretty(std::io::stdout());

serde_transcode::transcode(&mut deserializer, &mut serializer)?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Encoder<Transaction> {
}
}

pub fn commit(&mut self) -> EncodeResult<()> {
pub fn commit(self) -> EncodeResult<()> {
self.tr.commit().map_err(EncoderError::IoError)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//!```toml,ignore
//!# Cargo.toml
//![dependencies]
//!winreg = "0.51"
//!winreg = "0.52"
//!```
//!
//!```no_run
Expand Down Expand Up @@ -120,9 +120,9 @@ cfg_if::cfg_if! {

mod common;
#[cfg(feature = "serialization-serde")]
mod decoder;
pub mod decoder;
#[cfg(feature = "serialization-serde")]
mod encoder;
pub mod encoder;
pub mod enums;
pub mod reg_key;
pub mod reg_key_metadata;
Expand Down
27 changes: 27 additions & 0 deletions src/reg_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,33 @@ impl RegKey {
}
}

/// Rename a subkey
///
/// # Examples
/// ```no_run
/// # use std::error::Error;
/// # use winreg::RegKey;
/// # use winreg::enums::*;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let items = RegKey::predef(HKEY_CURRENT_USER).open_subkey(r"Software\MyProduct\Items")?;
/// items.rename_subkey("itemA", "itemB")?;
/// # Ok(())
/// # }
/// ```
pub fn rename_subkey<ON: AsRef<OsStr>, NN: AsRef<OsStr>>(
&self,
old_name: ON,
new_name: NN,
) -> io::Result<()> {
let c_old_name = to_utf16(old_name);
let c_new_name = to_utf16(new_name);
match unsafe { Registry::RegRenameKey(self.hkey, c_old_name.as_ptr(), c_new_name.as_ptr()) }
{
0 => Ok(()),
err => werr!(err),
}
}

/// Copy all the values and subkeys from `path` to `dest` key.
/// Will copy the content of `self` if `path` is an empty string.
///
Expand Down
4 changes: 2 additions & 2 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Transaction {
}
}

pub fn commit(&self) -> io::Result<()> {
pub fn commit(self) -> io::Result<()> {
unsafe {
match FileSystem::CommitTransaction(self.handle) {
0 => Err(io::Error::last_os_error()),
Expand All @@ -77,7 +77,7 @@ impl Transaction {
}
}

pub fn rollback(&self) -> io::Result<()> {
pub fn rollback(self) -> io::Result<()> {
unsafe {
match FileSystem::RollbackTransaction(self.handle) {
0 => Err(io::Error::last_os_error()),
Expand Down
18 changes: 14 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use crate::common::*;
use crate::enums::*;
use crate::RegValue;
use std::convert::TryInto;
use std::ffi::{OsStr, OsString};
use std::io;
use std::os::windows::ffi::OsStringExt;
Expand Down Expand Up @@ -107,11 +108,21 @@ impl FromRegValue for Vec<OsString> {
}
}

macro_rules! try_from_reg_value_int {
($val:expr, $map:expr) => {
$val.bytes
.as_slice()
.try_into()
.map($map)
.map_err(|_| io::Error::from_raw_os_error(Foundation::ERROR_INVALID_DATA as i32))
};
}

impl FromRegValue for u32 {
fn from_reg_value(val: &RegValue) -> io::Result<u32> {
match val.vtype {
#[allow(clippy::cast_ptr_alignment)]
REG_DWORD => Ok(unsafe { *(val.bytes.as_ptr() as *const u32) }),
REG_DWORD => try_from_reg_value_int!(val, u32::from_ne_bytes),
REG_DWORD_BIG_ENDIAN => try_from_reg_value_int!(val, u32::from_be_bytes),
_ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
}
}
Expand All @@ -120,8 +131,7 @@ impl FromRegValue for u32 {
impl FromRegValue for u64 {
fn from_reg_value(val: &RegValue) -> io::Result<u64> {
match val.vtype {
#[allow(clippy::cast_ptr_alignment)]
REG_QWORD => Ok(unsafe { *(val.bytes.as_ptr() as *const u64) }),
REG_QWORD => try_from_reg_value_int!(val, u64::from_ne_bytes),
_ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/reg_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ fn test_delete_subkey_with_flags() {
.is_ok());
}

#[test]
fn test_rename_subkey() {
with_key!(key, "RenameSubkey" => {
let old_name = "SubkeyA";
let new_name = "SubkeyB";
key.create_subkey(old_name).unwrap();
assert!(key.rename_subkey(old_name, new_name).is_ok());
assert!(key.open_subkey(new_name).is_ok());
});
}

#[test]
fn test_copy_tree() {
with_key!(key, "CopyTree" => {
Expand Down

0 comments on commit 1c56127

Please sign in to comment.