#[allow(unused_imports)]
use serde::{Serialize, Deserialize};
#[allow(unused_imports)]
use bincode;
#[allow(unused_imports)]
use serde_json::Value as JsonValue;
#[derive(Clone)]
#[derive(Serialize, Deserialize)]
pub struct BufferObject {
pub path: String,
pub result: String,
string: String,
json: JsonValue,
typ: String,
}
impl BufferObject {
pub fn new(path: String) -> Self {
let content = std::fs::read_to_string(&path).unwrap();
BufferObject {
path: path.clone(),
result: String::new(),
string: String::from(content),
json: JsonValue::default(), typ: "base".to_string(),
}
}
pub fn from_json(path: String) -> Self {
let content = std::fs::read_to_string(&path).unwrap();
let json: JsonValue = serde_json::from_str(&content).unwrap();
BufferObject {
path: path.clone(),
result: String::new(),
string: String::from(content),
json: json, typ: "json".to_string(),
}
}
pub fn to_json(self) -> JsonValue {
let json = if self.typ == "json" {
self.json.clone()
} else {
let _string = self.string.clone();
let _json = serde_json::from_str(&_string).unwrap();
_json
};
json
}
pub fn to_string(self) -> String {
let string = if self.typ == "base" {
self.string.clone()
} else {
let _json = self.json.clone();
let _string = serde_json::to_string(&_json).unwrap();
_string
};
string
}
pub fn to_cstring(self) -> std::ffi::CString {
let string = self.to_string();
let cstring = std::ffi::CString::new(string).unwrap();
cstring
}
pub fn get_serialized(self) -> Vec<u8> {
let seri = bincode::serialize(&self.to_string()).unwrap();
seri
}
}