extern crate winreg;
use winreg::enums::*;
use winreg::RegKey;
extern crate serde_json;
use serde_json::Value;
use std::fs::File;
fn main() {
jugment_os();
//chrome_native_config();
//firefox_native_config();
}
/* utils 函数 */
#[allow(dead_code)]
fn option_to_native(){
println!("
请通过输入序号
1. Chrome
2. Firefox
");
}
/* utils 函数 */
#[allow(dead_code)]
#[cfg(target_os="windows")]
fn chrome_native_config(){
let hklm = RegKey::predef(HKEY_CURRENT_USER);
let (key,disp) = hklm.create_subkey("SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\chrome_nativeMessaging").unwrap();
match disp {
REG_CREATED_NEW_KEY => println!("A new key has been created"),
REG_OPENED_EXISTING_KEY => println!("An existing key has been opened"),
}
key.set_value("", &"D:\\threeday\\chrome_nativeMessaging.json").unwrap();
let value:String = key.get_value("").unwrap();
if value != "D:\\threeday\\chrome_nativeMessaging.json".to_string() {
key.set_value("", &"D:\\threeday\\chrome_nativeMessaging.json").unwrap();
}
let f = File::create("D:\\threeday\\chrome_nativeMessaging.json").unwrap();
let config_str=r#"
{
"name":"com.chrome.nativemessaging",
"description":"Chrome native messageing api example",
"path":"D:\\threeday\\TestWeb.exe",
"type":"stdio",
"allowed_origins":[
"chrome-extension://hgibimofjkchfnpmfhnigfhkkkahlmah/"
]
}"#;
let v:Value =serde_json::from_str(config_str).unwrap();
serde_json::to_writer(&f, &v).expect("write json failed");
}
#[allow(dead_code)]
#[cfg(target_os="linux")]
fn chrome_native_config(){
let f = File::create("~/.config/google-chrome/NativeMessingHosts/chrome_nativeMessaging.json").unwrap();
let config_str=r#"
{
"name":"chrome_nativeMessaging",
"description":"Chrome native messageing api example",
"path":"~/.config/google-chrome/NativeMessingHosts/TestWeb",
"type":"stdio",
"allowed_origins":[
"chrome-extension://fkdghlklbgmkokmgnoanmkedekgkckkp/"
]
}"#;
let v:Value =serde_json::from_str(config_str).unwrap();
serde_json::to_writer(&f, &v).expect("write json failed");
}
/* utils 函数 */
#[allow(dead_code)]
#[cfg(target_os="windows")]
fn firefox_native_config(){
let hklm = RegKey::predef(HKEY_CURRENT_USER);
let (key,disp) = hklm.create_subkey("SOFTWARE\\Mozilla\\NativeMessagingHosts\\com.wingdust.threeday").unwrap();
match disp {
REG_CREATED_NEW_KEY => println!("A new key has been created"),
REG_OPENED_EXISTING_KEY => println!("An existing key has been opened"),
}
key.set_value("", &"D:\\threeday\\firefox_nativeMessaging.json").unwrap();
let value:String = key.get_value("").unwrap();
if value != "D:\\threeday\\firefox_nativeMessaging.json".to_string() {
key.set_value("", &"D:\\threeday\\firefox_nativeMessaging.json").unwrap();
}
let hklm2 = RegKey::predef(HKEY_LOCAL_MACHINE);
let (key2,disp2) = hklm2.create_subkey("SOFTWARE\\Mozilla\\NativeMessagingHosts\\com.wingdust.threeday").unwrap();
match disp2 {
REG_CREATED_NEW_KEY => println!("A new key has been created"),
REG_OPENED_EXISTING_KEY => println!("An existing key has been opened"),
}
key2.set_value("", &"D:\\threeday\\firefox_nativeMessaging.json").unwrap();
let value2:String = key2.get_value("").unwrap();
if value2 != "D:\\threeday\\firefox_nativeMessaging.json".to_string() {
key2.set_value("", &"D:\\threeday\\firefox_nativeMessaging.json").unwrap();
}
let f = File::create("D:\\threeday\\firefox_nativeMessaging.json").unwrap();
let config_str=r#"
{
"name":"com.wingdust.threeday",
"description":"Firefox native messageing api example",
"path":"D:\\threeday\\TestWeb.exe",
"type":"stdio",
"allowed_extensions":["threeday@wingdust.com"]
}"#;
let v:Value =serde_json::from_str(config_str).unwrap();
serde_json::to_writer(&f, &v).expect("write json failed");
}
#[allow(dead_code)]
#[cfg(target_os="linux")]
fn firefox_native_config(){
let f = File::create("~/.mozilla/native-messaging-hosts/firefox_nativeMessaging.json").unwrap();
let config_str=r#"
{
"name":"firefox_nativeMessaging",
"description":"Firefox native messageing api example",
"path":"~/.mozilla/native-messaging-hosts/TestWeb",
"type":"stdio",
"allowed_extensions":["threeday@wingdust.com"]
}"#;
let v:Value =serde_json::from_str(config_str).unwrap();
serde_json::to_writer(&f, &v).expect("write json failed");
}
/* utils 函数 */
#[allow(dead_code)]
fn write_json(message:&str,path:&str){
let f =File::create(format!("./{}/backup-1.json",path)).expect("create backup-1 failed");
let v:Value=serde_json::from_str(message).expect("trans json error");
serde_json::to_writer(&f, &v).expect("write json failed");
}
/// 判断当前运行环境的操作系统
#[allow(dead_code)]
fn jugment_os()->&'static str{
if cfg!(target_os = "windows"){
"windows"
}
else if cfg!(target_os = "linux"){
println!("linux");
"linux"
}
else{
"non-windows or linux"
}
}
Cargo.toml
[package]
name = "saveFile"
version = "0.1.0"
authors = ["wingdust <995315823@qq.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde_json = "~1.0.2"
[target.'cfg(windows)'.dependencies]
winreg = "0.8"
评论区
写评论最上面引入的部分也要加条件:
--
👇
WingDust:
--
👇
uno:
贴报错信息,或者做一个最小化的demo吧
👇
WingDust:
[target.'cfg(windows)'.dependencies] winreg = "0.8"
我这样写不通过
Cargo.toml
贴报错信息,或者做一个最小化的demo吧
👇
WingDust:
[target.'cfg(windows)'.dependencies] winreg = "0.8"
我这样写不通过
--
👇
uno: crate 也要条件引入
[dependencies] serde_json = "~1.0.2"
[target.'cfg(windows)'.dependencies] winreg = "0.8"
我这样写不通过
https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies
crate 也要条件引入