figment-winreg

demo.rs at tip
Login

demo.rs at tip

File examples/demo.rs from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
use figment::Figment;

use serde::Deserialize;

use figment_winreg::{FailStrategy, RegistryProvider};

use winreg::enums::HKEY_CURRENT_USER;


#[derive(Debug, PartialEq, Deserialize)]
struct AppConfig {
  #[serde(rename = "SubKeyInRoot")]
  subkey: Option<SubKeyInRoot>,

  #[serde(rename = "AnotherSubKey")]
  subkeytoo: Option<AnotherSubKey>,

  #[serde(rename = "RootString")]
  rootstring: String,

  #[serde(rename = "RootDWord")]
  rootu32: u32,

  #[serde(rename = "RootQWord")]
  rootu64: u64,

  #[serde(rename = "StrVec")]
  strvec: Vec<String>
}

#[derive(Debug, PartialEq, Deserialize)]
struct SubKeyInRoot {
  #[serde(rename = "ReqStr")]
  reqstr: String,

  #[serde(rename = "OptStr")]
  optstr: Option<String>,

  #[serde(rename = "ExpStr")]
  expstr: String,

  #[serde(rename = "SubSubKey")]
  subsubkey: Option<SubSubKey>
}

#[derive(Debug, PartialEq, Deserialize)]
struct AnotherSubKey {
  #[serde(rename = "OptStr")]
  optstr: Option<String>
}

#[derive(Debug, PartialEq, Deserialize)]
struct SubSubKey {
  #[serde(rename = "Greeting")]
  greeting: String,

  #[serde(rename = "OptStrVec")]
  optstrvec: Option<Vec<String>>
}


fn main() {
  let fig = Figment::new().merge(
    RegistryProvider::new(HKEY_CURRENT_USER, r"Temp\figment")
      .fail_strategy(FailStrategy::Skip)
  );

  let value: String = fig.extract_inner("RootString").unwrap();
  println!("rootstring: {value}");

  let value: String = fig.extract_inner(r"SubKeyInRoot.ExpStr").unwrap();
  println!("rootstring.expstr: {value}");

  let config: AppConfig = fig.extract().unwrap();
  println!("AppConfig: {config:?}");
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :