Skip to content

Commit 8f1d9eb

Browse files
committed
refactor: remove unused dependencies and integrate strum for error serialization in ReplicateStatusCause
1 parent 65769cf commit 8f1d9eb

File tree

3 files changed

+41
-182
lines changed

3 files changed

+41
-182
lines changed

Cargo.lock

Lines changed: 20 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

post-compute/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ serde = "1.0.219"
2222
serde_json = "1.0.140"
2323
sha256 = "1.6.0"
2424
sha3 = "0.10.8"
25+
strum = "0.27.2"
26+
strum_macros = "0.27.2"
2527
thiserror = "2.0.12"
2628
walkdir = "2.5.0"
2729
zip = "4.0.0"
@@ -30,7 +32,6 @@ zip = "4.0.0"
3032
logtest = "2.0.0"
3133
mockall = "0.13.1"
3234
once_cell = "1.21.3"
33-
rstest = "0.26.1"
3435
serial_test = "3.2.0"
3536
temp-env = "0.3.6"
3637
tempfile = "3.20.0"

post-compute/src/compute/errors.rs

Lines changed: 19 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use serde::{Deserialize, Serialize};
1+
use serde::{Serializer, ser::SerializeStruct};
2+
use strum_macros::EnumDiscriminants;
23
use thiserror::Error;
34

4-
#[derive(Debug, PartialEq, Clone, Error, Deserialize)]
5-
#[allow(clippy::enum_variant_names)]
5+
#[derive(Debug, PartialEq, Clone, Error, EnumDiscriminants)]
6+
#[strum_discriminants(derive(serde::Serialize))]
7+
#[strum_discriminants(serde(rename_all = "SCREAMING_SNAKE_CASE"))]
68
pub enum ReplicateStatusCause {
79
#[error("computed.json file missing")]
810
PostComputeComputedFileNotFound,
@@ -40,153 +42,31 @@ pub enum ReplicateStatusCause {
4042
PostComputeWorkerAddressMissing,
4143
}
4244

43-
impl ReplicateStatusCause {
44-
fn to_screaming_snake_case(&self) -> String {
45-
let debug_str = format!("{self:?}");
46-
let mut result = String::new();
47-
let mut prev_was_lowercase = false;
48-
49-
for c in debug_str.chars() {
50-
if c.is_uppercase() && !result.is_empty() && prev_was_lowercase {
51-
result.push('_');
52-
}
53-
result.push(c.to_ascii_uppercase());
54-
prev_was_lowercase = c.is_lowercase();
55-
}
56-
57-
result
58-
}
59-
}
60-
61-
#[derive(Debug, Serialize)]
62-
pub struct WorkflowError {
63-
pub cause: String,
64-
pub message: String,
65-
}
66-
67-
impl From<&ReplicateStatusCause> for WorkflowError {
68-
fn from(cause: &ReplicateStatusCause) -> Self {
69-
WorkflowError {
70-
cause: cause.to_screaming_snake_case(),
71-
message: cause.to_string(),
72-
}
73-
}
74-
}
75-
76-
impl Serialize for ReplicateStatusCause {
45+
impl serde::Serialize for ReplicateStatusCause {
7746
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
7847
where
79-
S: serde::Serializer,
48+
S: Serializer,
8049
{
81-
WorkflowError::from(self).serialize(serializer)
50+
let mut state = serializer.serialize_struct("ReplicateStatusCause", 2)?;
51+
state.serialize_field("cause", &ReplicateStatusCauseDiscriminants::from(self))?;
52+
state.serialize_field("message", &self.to_string())?;
53+
state.end()
8254
}
8355
}
8456

8557
#[cfg(test)]
8658
mod tests {
8759
use super::*;
88-
use rstest::rstest;
8960
use serde_json::{json, to_value};
9061

91-
#[rstest]
92-
#[case(
93-
ReplicateStatusCause::PostComputeComputedFileNotFound,
94-
"POST_COMPUTE_COMPUTED_FILE_NOT_FOUND",
95-
"computed.json file missing"
96-
)]
97-
#[case(
98-
ReplicateStatusCause::PostComputeDropboxUploadFailed,
99-
"POST_COMPUTE_DROPBOX_UPLOAD_FAILED",
100-
"Failed to upload to Dropbox"
101-
)]
102-
#[case(
103-
ReplicateStatusCause::PostComputeEncryptionFailed,
104-
"POST_COMPUTE_ENCRYPTION_FAILED",
105-
"Encryption stage failed"
106-
)]
107-
#[case(
108-
ReplicateStatusCause::PostComputeEncryptionPublicKeyMissing,
109-
"POST_COMPUTE_ENCRYPTION_PUBLIC_KEY_MISSING",
110-
"Encryption public key related environment variable is missing"
111-
)]
112-
#[case(
113-
ReplicateStatusCause::PostComputeFailedUnknownIssue,
114-
"POST_COMPUTE_FAILED_UNKNOWN_ISSUE",
115-
"Unexpected error occurred"
116-
)]
117-
#[case(
118-
ReplicateStatusCause::PostComputeInvalidTeeSignature,
119-
"POST_COMPUTE_INVALID_TEE_SIGNATURE",
120-
"Invalid TEE signature"
121-
)]
122-
#[case(
123-
ReplicateStatusCause::PostComputeIpfsUploadFailed,
124-
"POST_COMPUTE_IPFS_UPLOAD_FAILED",
125-
"Failed to upload to IPFS"
126-
)]
127-
#[case(
128-
ReplicateStatusCause::PostComputeMalformedEncryptionPublicKey,
129-
"POST_COMPUTE_MALFORMED_ENCRYPTION_PUBLIC_KEY",
130-
"Encryption public key is malformed"
131-
)]
132-
#[case(
133-
ReplicateStatusCause::PostComputeOutFolderZipFailed,
134-
"POST_COMPUTE_OUT_FOLDER_ZIP_FAILED",
135-
"Failed to zip result folder"
136-
)]
137-
#[case(
138-
ReplicateStatusCause::PostComputeResultDigestComputationFailed,
139-
"POST_COMPUTE_RESULT_DIGEST_COMPUTATION_FAILED",
140-
"Empty resultDigest"
141-
)]
142-
#[case(
143-
ReplicateStatusCause::PostComputeResultFileNotFound,
144-
"POST_COMPUTE_RESULT_FILE_NOT_FOUND",
145-
"Result file not found"
146-
)]
147-
#[case(
148-
ReplicateStatusCause::PostComputeSendComputedFileFailed,
149-
"POST_COMPUTE_SEND_COMPUTED_FILE_FAILED",
150-
"Failed to send computed file"
151-
)]
152-
#[case(
153-
ReplicateStatusCause::PostComputeStorageTokenMissing,
154-
"POST_COMPUTE_STORAGE_TOKEN_MISSING",
155-
"Storage token related environment variable is missing"
156-
)]
157-
#[case(
158-
ReplicateStatusCause::PostComputeTaskIdMissing,
159-
"POST_COMPUTE_TASK_ID_MISSING",
160-
"Task ID related environment variable is missing"
161-
)]
162-
#[case(
163-
ReplicateStatusCause::PostComputeTeeChallengePrivateKeyMissing,
164-
"POST_COMPUTE_TEE_CHALLENGE_PRIVATE_KEY_MISSING",
165-
"Tee challenge private key related environment variable is missing"
166-
)]
167-
#[case(
168-
ReplicateStatusCause::PostComputeTooLongResultFileName,
169-
"POST_COMPUTE_TOO_LONG_RESULT_FILE_NAME",
170-
"Result file name too long"
171-
)]
172-
#[case(
173-
ReplicateStatusCause::PostComputeWorkerAddressMissing,
174-
"POST_COMPUTE_WORKER_ADDRESS_MISSING",
175-
"Worker address related environment variable is missing"
176-
)]
177-
fn error_variant_serializes_with_correct_cause_and_message(
178-
#[case] error: ReplicateStatusCause,
179-
#[case] expected_cause: &str,
180-
#[case] expected_message: &str,
181-
) {
182-
let serialized = to_value(&error).unwrap();
183-
assert_eq!(
184-
serialized,
185-
json!({
186-
"cause": expected_cause,
187-
"message": expected_message
188-
})
189-
);
62+
#[test]
63+
fn error_variant_serialize_correctly() {
64+
let expected = json!({
65+
"cause": "POST_COMPUTE_TooLongResultFileName",
66+
"message": "Result file name too long"
67+
});
68+
let error_variant = ReplicateStatusCause::PostComputeTooLongResultFileName;
69+
assert_eq!(to_value(&error_variant).unwrap(), expected);
19070
}
19171

19272
#[test]

0 commit comments

Comments
 (0)