Skip to content

Commit 3574149

Browse files
authored
Implement Serialize/Deserialize for ActionStage (TraceMachina#1186)
Implements Serialize/Deserialize for ActionStage to allow for storage and retrieval from stores such as Redis. towards: TraceMachina#1182
1 parent d37bd90 commit 3574149

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

nativelink-error/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,17 @@ impl From<Code> for std::io::ErrorKind {
441441
}
442442
}
443443
}
444+
445+
// Allows for mapping this type into a generic serialization error.
446+
impl serde::ser::Error for Error {
447+
fn custom<T: std::fmt::Display>(msg: T) -> Self {
448+
Self::new(Code::InvalidArgument, msg.to_string())
449+
}
450+
}
451+
452+
// Allows for mapping this type into a generic deserialization error.
453+
impl serde::de::Error for Error {
454+
fn custom<T: std::fmt::Display>(msg: T) -> Self {
455+
Self::new(Code::InvalidArgument, msg.to_string())
456+
}
457+
}

nativelink-util/src/action_messages.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use nativelink_proto::google::rpc::Status;
2929
use prost::bytes::Bytes;
3030
use prost::Message;
3131
use prost_types::Any;
32+
use serde::ser::Error as SerdeError;
3233
use serde::{Deserialize, Serialize};
3334
use uuid::Uuid;
3435

@@ -746,7 +747,7 @@ impl Default for ActionResult {
746747
// TODO(allada) Remove the need for clippy argument by making the ActionResult and ProtoActionResult
747748
// a Box.
748749
/// The execution status/stage. This should match `ExecutionStage::Value` in `remote_execution.proto`.
749-
#[derive(PartialEq, Debug, Clone)]
750+
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
750751
#[allow(clippy::large_enum_variant)]
751752
pub enum ActionStage {
752753
/// Stage is unknown.
@@ -762,9 +763,22 @@ pub enum ActionStage {
762763
/// Worker completed the work with result.
763764
Completed(ActionResult),
764765
/// Result was found from cache, don't decode the proto just to re-encode it.
766+
#[serde(serialize_with = "serialize_proto_result", skip_deserializing)]
767+
// The serialization step decodes this to an ActionResult which is serializable.
768+
// Since it will always be serialized as an ActionResult, we do not need to support
769+
// deserialization on this type at all.
770+
// In theory, serializing this should never happen so performance shouldn't be affected.
765771
CompletedFromCache(ProtoActionResult),
766772
}
767773

774+
fn serialize_proto_result<S>(v: &ProtoActionResult, serializer: S) -> Result<S::Ok, S::Error>
775+
where
776+
S: serde::Serializer,
777+
{
778+
let s = ActionResult::try_from(v.clone()).map_err(S::Error::custom)?;
779+
s.serialize(serializer)
780+
}
781+
768782
impl ActionStage {
769783
pub const fn has_action_result(&self) -> bool {
770784
match self {
@@ -1075,7 +1089,7 @@ where
10751089

10761090
/// Current state of the action.
10771091
/// This must be 100% compatible with `Operation` in `google/longrunning/operations.proto`.
1078-
#[derive(PartialEq, Debug, Clone)]
1092+
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
10791093
pub struct ActionState {
10801094
pub stage: ActionStage,
10811095
pub id: OperationId,

0 commit comments

Comments
 (0)