@@ -9,7 +9,7 @@ use crate::error::RegistryError;
99
1010rust_i18n:: i18n!( "locales" , fallback = "en-us" ) ;
1111
12- mod error;
12+ pub mod error;
1313pub mod config;
1414
1515pub struct RegistryHelper {
@@ -20,7 +20,7 @@ pub struct RegistryHelper {
2020}
2121
2222impl RegistryHelper {
23- /// Create a new `RegistryHelper`.
23+ /// Create a new `RegistryHelper` from json .
2424 ///
2525 /// # Arguments
2626 ///
@@ -29,7 +29,7 @@ impl RegistryHelper {
2929 /// # Errors
3030 ///
3131 /// * `RegistryError` - The error that occurred.
32- pub fn new ( config : & str ) -> Result < Self , RegistryError > {
32+ pub fn new_from_json ( config : & str ) -> Result < Self , RegistryError > {
3333 let registry: Registry = match serde_json:: from_str ( config) {
3434 Ok ( config) => config,
3535 Err ( e) => return Err ( RegistryError :: Json ( e) ) ,
@@ -47,6 +47,34 @@ impl RegistryHelper {
4747 )
4848 }
4949
50+ /// Create a new `RegistryHelper`.
51+ ///
52+ /// # Arguments
53+ ///
54+ /// * `config` - The string with registry configuration information.
55+ ///
56+ /// # Errors
57+ ///
58+ /// * `RegistryError` - The error that occurred.
59+ pub fn new ( key_path : & str , value_name : Option < String > , value_data : Option < RegistryValueData > ) -> Result < Self , RegistryError > {
60+ let ( hive, subkey) = get_hive_from_path ( key_path) ?;
61+ let config = Registry {
62+ key_path : key_path. to_string ( ) ,
63+ value_name,
64+ value_data,
65+ metadata : None ,
66+ exist : None ,
67+ } ;
68+ Ok (
69+ Self {
70+ config,
71+ hive,
72+ subkey : subkey. to_string ( ) ,
73+ what_if : false
74+ }
75+ )
76+ }
77+
5078 pub fn enable_what_if ( & mut self ) {
5179 self . what_if = true ;
5280 }
@@ -386,7 +414,7 @@ fn convert_reg_value(value: &Data) -> Result<Option<RegistryValueData>, Registry
386414
387415#[ test]
388416fn get_hklm_key ( ) {
389- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKEY_LOCAL_MACHINE"}"# ) . unwrap ( ) ;
417+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKEY_LOCAL_MACHINE"}"# ) . unwrap ( ) ;
390418 let reg_config = reg_helper. get ( ) . unwrap ( ) ;
391419 assert_eq ! ( reg_config. key_path, r#"HKEY_LOCAL_MACHINE"# ) ;
392420 assert_eq ! ( reg_config. value_name, None ) ;
@@ -395,7 +423,7 @@ fn get_hklm_key() {
395423
396424#[ test]
397425fn get_product_name ( ) {
398- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion","valueName":"ProductName"}"# ) . unwrap ( ) ;
426+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion","valueName":"ProductName"}"# ) . unwrap ( ) ;
399427 let reg_config = reg_helper. get ( ) . unwrap ( ) ;
400428 assert_eq ! ( reg_config. key_path, r#"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"# ) ;
401429 assert_eq ! ( reg_config. value_name, Some ( "ProductName" . to_string( ) ) ) ;
@@ -404,7 +432,7 @@ fn get_product_name() {
404432
405433#[ test]
406434fn get_nonexisting_key ( ) {
407- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKCU\\DoesNotExist"}"# ) . unwrap ( ) ;
435+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKCU\\DoesNotExist"}"# ) . unwrap ( ) ;
408436 let reg_config = reg_helper. get ( ) . unwrap ( ) ;
409437 assert_eq ! ( reg_config. key_path, r#"HKCU\DoesNotExist"# ) ;
410438 assert_eq ! ( reg_config. value_name, None ) ;
@@ -414,7 +442,7 @@ fn get_nonexisting_key() {
414442
415443#[ test]
416444fn get_nonexisting_value ( ) {
417- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKCU\\Software","valueName":"DoesNotExist"}"# ) . unwrap ( ) ;
445+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKCU\\Software","valueName":"DoesNotExist"}"# ) . unwrap ( ) ;
418446 let reg_config = reg_helper. get ( ) . unwrap ( ) ;
419447 assert_eq ! ( reg_config. key_path, r#"HKCU\Software"# ) ;
420448 assert_eq ! ( reg_config. value_name, Some ( "DoesNotExist" . to_string( ) ) ) ;
@@ -424,7 +452,7 @@ fn get_nonexisting_value() {
424452
425453#[ test]
426454fn set_and_remove_test_value ( ) {
427- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKCU\\DSCTest\\DSCSubKey","valueName":"TestValue","valueData": { "String": "Hello"} }"# ) . unwrap ( ) ;
455+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKCU\\DSCTest\\DSCSubKey","valueName":"TestValue","valueData": { "String": "Hello"} }"# ) . unwrap ( ) ;
428456 reg_helper. set ( ) . unwrap ( ) ;
429457 let result = reg_helper. get ( ) . unwrap ( ) ;
430458 assert_eq ! ( result. key_path, r#"HKCU\DSCTest\DSCSubKey"# ) ;
@@ -436,7 +464,7 @@ fn set_and_remove_test_value() {
436464 assert_eq ! ( result. value_name, Some ( "TestValue" . to_string( ) ) ) ;
437465 assert_eq ! ( result. value_data, None ) ;
438466 assert_eq ! ( result. exist, Some ( false ) ) ;
439- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKCU\\DSCTest"}"# ) . unwrap ( ) ;
467+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKCU\\DSCTest"}"# ) . unwrap ( ) ;
440468 let result = reg_helper. get ( ) . unwrap ( ) ;
441469 assert_eq ! ( result. key_path, r#"HKCU\DSCTest"# ) ;
442470 assert_eq ! ( result. value_name, None ) ;
@@ -451,13 +479,13 @@ fn set_and_remove_test_value() {
451479
452480#[ test]
453481fn delete_tree ( ) {
454- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKCU\\DSCTest2\\DSCSubKey","valueName":"TestValue","valueData": { "String": "Hello"} }"# ) . unwrap ( ) ;
482+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKCU\\DSCTest2\\DSCSubKey","valueName":"TestValue","valueData": { "String": "Hello"} }"# ) . unwrap ( ) ;
455483 reg_helper. set ( ) . unwrap ( ) ;
456484 let result = reg_helper. get ( ) . unwrap ( ) ;
457485 assert_eq ! ( result. key_path, r#"HKCU\DSCTest2\DSCSubKey"# ) ;
458486 assert_eq ! ( result. value_name, Some ( "TestValue" . to_string( ) ) ) ;
459487 assert_eq ! ( result. value_data, Some ( RegistryValueData :: String ( "Hello" . to_string( ) ) ) ) ;
460- let reg_helper = RegistryHelper :: new ( r#"{"keyPath":"HKCU\\DSCTest2"}"# ) . unwrap ( ) ;
488+ let reg_helper = RegistryHelper :: new_from_json ( r#"{"keyPath":"HKCU\\DSCTest2"}"# ) . unwrap ( ) ;
461489 reg_helper. remove ( ) . unwrap ( ) ;
462490 let result = reg_helper. get ( ) . unwrap ( ) ;
463491 assert_eq ! ( result. key_path, r#"HKCU\DSCTest2"# ) ;
0 commit comments