From 2c9e3796a74d1b5edb66952e8ea19cb118e41ad3 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 29 Dec 2019 22:16:22 +0800 Subject: [PATCH] add DingTalk::from_json --- Cargo.toml | 2 +- src/lib.rs | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bc62b20..79fb55d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dingtalk" -version = "0.2.0" +version = "0.2.1" authors = ["Hatter Jiang "] edition = "2018" description = "DingTalk Util" diff --git a/src/lib.rs b/src/lib.rs index d34051a..0059edd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,14 +149,7 @@ impl <'a> DingTalk<'a> { /// Create `DingTalk` from file /// - /// Format: - /// ```json - /// { - /// "default_webhook_url": "", // option - /// "access_token": "", - /// "sec_token": "" // option - /// } - /// ``` + /// Format see `DingTalk::from_json(json: &str)` pub fn from_file(f: &str) -> XResult { let f_path_buf = if f.starts_with("~/") { let home = PathBuf::from(env::var("HOME")?); @@ -165,9 +158,23 @@ impl <'a> DingTalk<'a> { PathBuf::from(f) }; let f_content = fs::read_to_string(f_path_buf)?; - let f_json_value = json::parse(&f_content)?; + Self::from_json(&f_content) + } + + /// Create `DingTalk` from JSON string + /// + /// Format: + /// ```json + /// { + /// "default_webhook_url": "", // option + /// "access_token": "", + /// "sec_token": "" // option + /// } + /// ``` + pub fn from_json(json: &str) -> XResult { + let f_json_value = json::parse(json)?; if !f_json_value.is_object() { - return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", f_content)))); + return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", json)))); } let default_webhook_url: &'a str = Self::string_to_a_str(f_json_value["default_webhook_url"].as_str().unwrap_or(DEFAULT_DINGTALK_ROBOT_URL).to_owned());