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