From 4a948dac4a83576b5924c00f846db5ba144a6fa1 Mon Sep 17 00:00:00 2001 From: nils-mathieu Date: Sat, 16 Mar 2024 16:52:50 +0100 Subject: [PATCH] change the external_id field to be an enum instead of a regular string --- src/jobs/da_job/mod.rs | 4 +-- src/jobs/types.rs | 63 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/jobs/da_job/mod.rs b/src/jobs/da_job/mod.rs index 77b217ff..54b7856f 100644 --- a/src/jobs/da_job/mod.rs +++ b/src/jobs/da_job/mod.rs @@ -20,7 +20,7 @@ impl Job for DaJob { internal_id, job_type: JobType::DataSubmission, status: JobStatus::Created, - external_id: String::new(), + external_id: String::new().into(), metadata: HashMap::new(), version: 0, }) @@ -49,7 +49,7 @@ impl Job for DaJob { } async fn verify_job(&self, config: &Config, job: &JobItem) -> Result { - Ok(config.da_client().verify_inclusion(job.external_id.as_str()).await?) + Ok(config.da_client().verify_inclusion(job.external_id.unwrap_string()?).await?) } fn max_process_attempts(&self) -> u64 { diff --git a/src/jobs/types.rs b/src/jobs/types.rs index f48b2372..b8950604 100644 --- a/src/jobs/types.rs +++ b/src/jobs/types.rs @@ -1,8 +1,69 @@ +use color_eyre::{eyre::eyre, Result}; use mongodb::bson::serde_helpers::uuid_1_as_binary; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use uuid::Uuid; +/// An external id. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum ExternalId { + /// A string. + String(Box), + /// A number. + Number(usize), +} + +impl From for ExternalId { + #[inline] + fn from(value: String) -> Self { + ExternalId::String(value.into_boxed_str()) + } +} + +impl From for ExternalId { + #[inline] + fn from(value: usize) -> Self { + ExternalId::Number(value) + } +} + +impl ExternalId { + /// Unwraps the external id as a string. + /// + /// # Panics + /// + /// This function panics if the provided external id not a string. + #[track_caller] + #[inline] + pub fn unwrap_string(&self) -> Result<&str> { + match self { + ExternalId::String(s) => Ok(s), + _ => Err(unwrap_external_id_failed("string", self)), + } + } + + /// Unwraps the external id as a number. + /// + /// # Panics + /// + /// This function panics if the provided external id is not a number. + #[track_caller] + #[inline] + #[allow(dead_code)] // temporarily unused (until the other pull request uses it) + pub fn unwrap_number(&self) -> Result { + match self { + ExternalId::Number(n) => Ok(*n), + _ => Err(unwrap_external_id_failed("number", self)), + } + } +} + +/// Returns an error indicating that the provided external id coulnd't be unwrapped. +fn unwrap_external_id_failed(expected: &str, got: &ExternalId) -> color_eyre::eyre::Error { + eyre!("wrong ExternalId type: expected {}, got {:?}", expected, got) +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub enum JobType { /// Submitting DA data to the DA layer @@ -46,7 +107,7 @@ pub struct JobItem { pub status: JobStatus, /// external id to track the status of the job. for ex, txn hash for blob inclusion /// or job_id from SHARP - pub external_id: String, + pub external_id: ExternalId, /// additional field to store values related to the job pub metadata: HashMap, /// helps to keep track of the version of the item for optimistic locking