mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-28 16:27:50 +00:00
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:
parent
b0edfc75ff
commit
c34bdd06db
@ -11,7 +11,8 @@ extern crate serde_json;
|
|||||||
use libc::mode_t;
|
use libc::mode_t;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub mod serialize;
|
mod serialize;
|
||||||
|
pub use serialize::{to_string, to_writer, Result, SerializeError};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn is_false(b: bool) -> bool {
|
fn is_false(b: bool) -> bool {
|
||||||
@ -57,11 +58,11 @@ pub struct Spec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Spec {
|
impl Spec {
|
||||||
pub fn load(path: &str) -> Result<Spec, serialize::SerializeError> {
|
pub fn load(path: &str) -> Result<Spec> {
|
||||||
serialize::deserialize(path)
|
serialize::deserialize(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&self, path: &str) -> Result<(), serialize::SerializeError> {
|
pub fn save(&self, path: &str) -> Result<()> {
|
||||||
serialize::serialize(self, path)
|
serialize::serialize(self, path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,23 +3,24 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
//
|
//
|
||||||
|
|
||||||
use serde;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt::{self, Formatter};
|
use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, SerializeError>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum SerializeError {
|
pub enum SerializeError {
|
||||||
Io(io::Error),
|
Io(io::Error),
|
||||||
Json(serde_json::Error),
|
Json(serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for SerializeError {
|
impl Display for SerializeError {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
||||||
match *self {
|
match *self {
|
||||||
SerializeError::Io(ref e) => e.fmt(f),
|
SerializeError::Io(ref e) => e.fmt(f),
|
||||||
SerializeError::Json(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
|
where
|
||||||
W: io::Write,
|
W: io::Write,
|
||||||
T: Serialize,
|
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
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
{
|
{
|
||||||
let mut f = File::create(path)?;
|
let f = File::create(path)?;
|
||||||
Ok(serde_json::to_writer(&mut f, &o)?)
|
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
|
where
|
||||||
T: Serialize,
|
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
|
where
|
||||||
for<'a> T: Deserialize<'a>,
|
for<'a> T: Deserialize<'a>,
|
||||||
{
|
{
|
||||||
let f = File::open(path)?;
|
let f = File::open(path)?;
|
||||||
Ok(serde_json::from_reader(&f)?)
|
Ok(serde_json::from_reader(f)?)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user