From eb1be1caf274670748d89723f605122d1ce2e6b3 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 1 Jan 2021 23:31:31 +0800 Subject: [PATCH] style: code style --- __crypto/simple_contract/src/credit_util.rs | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 __crypto/simple_contract/src/credit_util.rs diff --git a/__crypto/simple_contract/src/credit_util.rs b/__crypto/simple_contract/src/credit_util.rs new file mode 100644 index 0000000..7fe6752 --- /dev/null +++ b/__crypto/simple_contract/src/credit_util.rs @@ -0,0 +1,22 @@ +use std::fs; +use std::fs::File; +use rust_util::XResult; +use crate::credit::CreditContract; + +pub fn load_credit_contract(name: &str) -> XResult { + let json = fs::read_to_string(name)?; + serde_json::from_str(&json).map_err(|e| e.into()) +} + +pub fn save_credit_contract(c: &CreditContract, overwrite: bool) -> XResult<()> { + let name = &c.name; + if !overwrite { + if let Ok(_) = File::open(name) { + return simple_error!("File exists: {}", name); + } + } + information!("Write file: {}", name); + fs::write(name, serde_json::to_string(c)?.as_bytes())?; + Ok(()) +} +