use crate::error::CSDError; use crate::exec::call_ceph; use std::fmt::Debug; use std::fs::File; use std::io::prelude::*; use serde::de::DeserializeOwned; // Generic trait to read file to serializable struct pub trait FromFile { fn from_file(path: &str) -> Result; } impl FromFile for T { fn from_file(path: &str) -> Result { let mut file = File::open(path)?; let mut buffer = String::new(); file.read_to_string(&mut buffer)?; Ok(serde_json::from_str(&buffer)?) } } // Generic trait to create structs from ceph JSON output. Since most if not all // of of ceph's commands can be formatted to JSON. For example: // let pgmap = PGMap::from_ceph("pg dump").unwrap() pub trait FromCeph { fn from_ceph(cmd: &str) -> Result; } impl FromCeph for T { fn from_ceph(cmd: &str) -> Result { let ceph_output = call_ceph(cmd)?; let serde_res: Result = serde_json::from_str(&ceph_output); trace!("deserialize ceph: {:?}", serde_res); Ok(serde_res?) } }