提交 84568469 authored 作者: Serhij S's avatar Serhij S

state functions moved to features

上级 9e65ff31
......@@ -54,8 +54,8 @@ once_cell = { version = "1.19.0", optional = true }
parking_lot = { version = "0.12.3", optional = true }
parking_lot_rt = { version = "0.12.1", optional = true }
quanta = { version = "=0.12.3", optional = true }
serde_json = "1.0.134"
rmp-serde = "1.3.0"
serde_json = { version = "1.0.134", optional = true }
rmp-serde = { version = "1.3.0", optional = true }
[target.'cfg(windows)'.dependencies]
parking_lot_rt = { version = "0.12.1" }
......@@ -69,12 +69,15 @@ modbus = ["rmodbus"]
openssl-vendored = ["busrt/openssl-vendored", "eva-common/openssl-vendored"]
metrics = ["dep:metrics", "metrics-exporter-prometheus", "metrics-exporter-scope", "tokio", "quanta"]
async = ["dep:parking_lot_rt"]
full = ["eapi", "modbus", "metrics", "pipe", "rvideo", "rflow", "async"]
full = ["eapi", "modbus", "metrics", "pipe", "rvideo", "rflow", "async", "json", "msgpack"]
locking-default = ["dep:parking_lot", "rtsc/parking_lot", "rvideo?/locking-default", "rflow?/locking-default"]
locking-rt = ["dep:parking_lot_rt", "rvideo?/locking-rt", "rflow?/locking-rt"]
locking-rt-safe = ["rvideo?/locking-rt-safe", "rflow?/locking-rt-safe"]
json = ["serde_json"]
msgpack = ["rmp-serde"]
default = ["locking-default"]
[dev-dependencies]
......
......@@ -108,6 +108,7 @@ pub mod supervisor;
pub mod thread_rt;
/// State helper functions
#[cfg(any(feature = "json", feature = "msgpack"))]
pub mod state;
/// The crate result type
......
......@@ -5,42 +5,55 @@ use serde::{de::DeserializeOwned, Serialize};
use crate::{Error, Result};
enum Format {
#[cfg(feature = "json")]
Json,
#[cfg(feature = "msgpack")]
Msgpack,
}
impl Format {
fn from_path<P: AsRef<Path>>(path: P) -> Self {
fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
match path
.as_ref()
.extension()
.map_or("", |ext| ext.to_str().unwrap())
{
"json" => Self::Json,
_ => Self::Msgpack,
#[cfg(feature = "json")]
"json" => Ok(Self::Json),
#[cfg(not(feature = "json"))]
"json" => Err(Error::Unimplemented),
#[cfg(feature = "msgpack")]
_ => Ok(Self::Msgpack),
#[cfg(not(feature = "msgpack"))]
_ => Err(Error::Unimplemented),
}
}
}
/// Load the state from a file. If "json" extension is specified, the state is loaded in JSON
/// format. All errors, including missing state file, must be handled by the caller.
/// Load the state from a file. If "json" extension is specified, the state is loaded from JSON
/// format (requires crate 'json' feature), otherwise from MessagePack (requires crate 'msgpack'
/// feature). All errors, including missing state file, must be handled by the caller.
pub fn load<S: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<S> {
let format = Format::from_path(&path);
let format = Format::from_path(&path)?;
let file = File::open(&path)?;
let data = match format {
#[cfg(feature = "json")]
Format::Json => serde_json::from_reader(file).map_err(Error::failed)?,
#[cfg(feature = "msgpack")]
Format::Msgpack => rmp_serde::from_read(file).map_err(Error::failed)?,
};
Ok(data)
}
/// Save the state to a file. If "json" extension is specified, the state is saved in JSON
/// format. Otherwise it is saved in MessagePack format.
/// Save the state to a file. If "json" extension is specified, the state is saved in JSON format
/// (requires crate 'json' feature), otherwise in MessagePack (requires crate 'msgpack' feature).
pub fn save<S: Serialize, P: AsRef<Path>>(path: P, state: &S) -> Result<()> {
let format = Format::from_path(&path);
let format = Format::from_path(&path)?;
let mut file = File::create(&path)?;
let data = match format {
#[cfg(feature = "json")]
Format::Json => serde_json::to_vec(state).map_err(Error::failed)?,
#[cfg(feature = "msgpack")]
Format::Msgpack => rmp_serde::to_vec_named(state).map_err(Error::failed)?,
};
file.write_all(&data)?;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论