use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::PathBuf;
#[derive(Clone, Debug)]
pub struct ChainIdManager {
data_dir: String,
}
impl ChainIdManager {
pub fn new(data_dir: String) -> Self {
ChainIdManager { data_dir }
}
pub fn save_block_chain_id(&self, block_chain_id: &str) -> Result<(), io::Error> {
let mut path = PathBuf::new();
path.push(&self.data_dir);
path.push("block-chain-id");
let mut file = File::create(path)?;
file.write_all(block_chain_id.as_bytes())
}
pub fn get_block_chain_id(&self) -> Result<Option<String>, io::Error> {
let mut path = PathBuf::new();
path.push(&self.data_dir);
path.push("block-chain-id");
match File::open(path) {
Ok(mut file) => {
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(Some(contents))
}
Err(ref err) if err.kind() == io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(err),
}
}
}