figment-winreg

Artifact [da44ca2d9f]
Login

Artifact [da44ca2d9f]

Artifact da44ca2d9f00e694930b1d9e6876d9126345b947185848f4f74256a67343e5b3:


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 :