Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: use enum for patch action variants
Lars Wirzenius committed 2 years ago
commit a59b2dba825b6786e74b502d03734de0d9f3adb1
parent d12e4206e8eaa057e1d4ce2e557132646ab88363
1 file changed +39 -5
modified src/msg.rs
@@ -184,7 +184,7 @@ impl<'a> RequestBuilder<'a> {
                "created"
            };
            patch_info = Some(PatchEvent {
-
                action: patch_action.to_string(),
+
                action: PatchAction::try_from(patch_action)?,
                patch: Patch {
                    id: patch_id,
                    author,
@@ -400,14 +400,44 @@ pub struct PushEvent {
/// An event related to a Radicle patch object.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatchEvent {
-
    /// What action has happened to the patch. Should be one of
-
    /// `created` or `updated`.
-
    pub action: String,
+
    /// What action has happened to the patch.
+
    pub action: PatchAction,

    /// Metadata about the patch.
    pub patch: Patch,
}

+
/// What action has happened to the patch?
+
#[derive(Debug, Clone, Serialize, Deserialize)]
+
pub enum PatchAction {
+
    /// Patch has been created.
+
    Created,
+

+
    /// Patch has been updated.
+
    Updated,
+
}
+

+
#[cfg(test)]
+
impl PatchAction {
+
    fn as_str(&self) -> &str {
+
        match self {
+
            Self::Created => "created",
+
            Self::Updated => "updated",
+
        }
+
    }
+
}
+

+
impl TryFrom<&str> for PatchAction {
+
    type Error = MessageError;
+
    fn try_from(value: &str) -> Result<Self, Self::Error> {
+
        match value {
+
            "created" => Ok(Self::Created),
+
            "updated" => Ok(Self::Updated),
+
            _ => Err(Self::Error::UnknownPatchAction(value.into())),
+
        }
+
    }
+
}
+

/// Fields in a [`Request`] message describing the repository
/// concerned.
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -670,6 +700,10 @@ pub enum MessageError {
    /// Error from `radicle-surf` crate.
    #[error(transparent)]
    RadicleSurfError(#[from] radicle_surf::Error),
+

+
    /// Trying to create a PatchAction from an invalid value.
+
    #[error("invalid patch action {0:?}")]
+
    UnknownPatchAction(String),
}

#[cfg(test)]
@@ -793,7 +827,7 @@ pub mod tests {
        assert_eq!(common.repository.name, project.repo.project()?.name());

        let patch = patch.unwrap();
-
        assert_eq!(patch.action, "created");
+
        assert_eq!(patch.action.as_str(), "created");
        assert_eq!(patch.patch.id.to_string(), patch_cob.id.to_string());
        assert_eq!(patch.patch.title, patch_cob.title());
        assert_eq!(patch.patch.state.status, patch_cob.state().to_string());