Skip to content

Commit

Permalink
change the external_id field to be an enum instead of a regular string
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-mathieu committed Mar 16, 2024
1 parent eea4f83 commit 4a948da
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/jobs/da_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Job for DaJob {
}

async fn verify_job(&self, config: &Config, job: &JobItem) -> Result<JobVerificationStatus> {
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 {
Expand Down
63 changes: 62 additions & 1 deletion src/jobs/types.rs
Original file line number Diff line number Diff line change
@@ -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<str>),
/// A number.
Number(usize),
}

impl From<String> for ExternalId {
#[inline]
fn from(value: String) -> Self {
ExternalId::String(value.into_boxed_str())
}
}

impl From<usize> 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<usize> {
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
Expand Down Expand Up @@ -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<String, String>,
/// helps to keep track of the version of the item for optimistic locking
Expand Down

0 comments on commit 4a948da

Please sign in to comment.