agent: simplify implementation of oci/serialize

Simplify implementation of oci/serialize:
1) explicitly export pub members.
2) avoid unnecessary & and mut operators.
3) define Result to avoid duplicated code.

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
Liu Jiang 2019-11-22 14:52:26 +08:00
parent b0edfc75ff
commit c34bdd06db
2 changed files with 18 additions and 16 deletions

View File

@ -11,7 +11,8 @@ extern crate serde_json;
use libc::mode_t;
use std::collections::HashMap;
pub mod serialize;
mod serialize;
pub use serialize::{to_string, to_writer, Result, SerializeError};
#[allow(dead_code)]
fn is_false(b: bool) -> bool {
@ -57,11 +58,11 @@ pub struct Spec {
}
impl Spec {
pub fn load(path: &str) -> Result<Spec, serialize::SerializeError> {
pub fn load(path: &str) -> Result<Spec> {
serialize::deserialize(path)
}
pub fn save(&self, path: &str) -> Result<(), serialize::SerializeError> {
pub fn save(&self, path: &str) -> Result<()> {
serialize::serialize(self, path)
}
}

View File

@ -3,23 +3,24 @@
// SPDX-License-Identifier: Apache-2.0
//
use serde;
use serde::{Deserialize, Serialize};
use serde_json;
use std::error::Error;
use std::fmt::{self, Formatter};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::fs::File;
use std::io;
pub type Result<T> = std::result::Result<T, SerializeError>;
#[derive(Debug)]
pub enum SerializeError {
Io(io::Error),
Json(serde_json::Error),
}
impl fmt::Display for SerializeError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
impl Display for SerializeError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match *self {
SerializeError::Io(ref e) => e.fmt(f),
SerializeError::Json(ref e) => e.fmt(f),
@ -55,33 +56,33 @@ impl From<serde_json::Error> for SerializeError {
}
}
pub fn to_writer<W, T>(o: &T, mut w: W) -> Result<(), SerializeError>
pub fn to_writer<W, T>(o: &T, w: W) -> Result<()>
where
W: io::Write,
T: Serialize,
{
Ok(serde_json::to_writer(&mut w, &o)?)
Ok(serde_json::to_writer(w, o)?)
}
pub fn serialize<T>(o: &T, path: &str) -> Result<(), SerializeError>
pub fn serialize<T>(o: &T, path: &str) -> Result<()>
where
T: Serialize,
{
let mut f = File::create(path)?;
Ok(serde_json::to_writer(&mut f, &o)?)
let f = File::create(path)?;
Ok(serde_json::to_writer(f, o)?)
}
pub fn to_string<T>(o: &T) -> Result<String, SerializeError>
pub fn to_string<T>(o: &T) -> Result<String>
where
T: Serialize,
{
Ok(serde_json::to_string(&o)?)
Ok(serde_json::to_string(o)?)
}
pub fn deserialize<T>(path: &str) -> Result<T, SerializeError>
pub fn deserialize<T>(path: &str) -> Result<T>
where
for<'a> T: Deserialize<'a>,
{
let f = File::open(path)?;
Ok(serde_json::from_reader(&f)?)
Ok(serde_json::from_reader(f)?)
}