summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2024-03-05 18:13:16 +0200
committerLars Wirzenius <liw@liw.fi>2024-03-05 18:13:16 +0200
commit68b589ce376cdb50730a94191e2179281083665f (patch)
tree3b87cd40044b03e4c5f0d6f0d7aad2fb68c3f05a
parentfee8700dc8d1845c7c8c55e0fd2dd99b29d3efe6 (diff)
downloadradicle-ci-broker-68b589ce376cdb50730a94191e2179281083665f.tar.gz
feat: use an enum for event types
Stringly typing isn't checked by the compiler. Signed-off-by: Lars Wirzenius <liw@liw.fi>
-rw-r--r--src/msg.rs24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/msg.rs b/src/msg.rs
index 067faa2..10e3fc6 100644
--- a/src/msg.rs
+++ b/src/msg.rs
@@ -135,9 +135,9 @@ impl<'a> RequestBuilder<'a> {
let author = extract_author(profile, event)?;
let push_info: Option<PushEvent>;
let patch_info: Option<PatchEvent>;
- let event_type: String;
+ let event_type: EventType;
if is_patch {
- event_type = "patch".to_string();
+ event_type = EventType::Patch;
let patch_id = event.patch_id().ok_or(MessageError::Trigger)?;
let patch = patch::Patches::open(&repository)?
.get(&patch_id.into())?
@@ -202,7 +202,7 @@ impl<'a> RequestBuilder<'a> {
},
});
} else {
- event_type = "push".to_string();
+ event_type = EventType::Push;
let before_oid: Oid = old.unwrap_or(*oid);
let push_commits: Vec<Oid> = repo
.history(oid)?
@@ -342,11 +342,21 @@ impl fmt::Display for Request {
}
}
+/// Type of event.
+#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
+pub enum EventType {
+ /// A push event to a branch.
+ Push,
+
+ /// A new or changed patch.
+ Patch,
+}
+
/// Common fields in all variations of a [`Request`] message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventCommonFields {
/// The type of the event.
- pub event_type: String,
+ pub event_type: EventType,
/// The repository the event is related to.
pub repository: Repository,
@@ -638,7 +648,7 @@ pub enum MessageError {
#[cfg(test)]
pub mod tests {
use crate::event::BrokerEvent;
- use crate::msg::{Request, RequestBuilder};
+ use crate::msg::{EventType, Request, RequestBuilder};
use radicle::git::raw::Oid;
use radicle::git::RefString;
use radicle::patch::{MergeTarget, Patches};
@@ -682,7 +692,7 @@ pub mod tests {
assert!(patch.is_none());
assert!(push.is_some());
- assert_eq!(common.event_type, "push");
+ assert_eq!(common.event_type, EventType::Push);
assert_eq!(common.repository.id, project.id);
assert_eq!(common.repository.name, project.repo.project()?.name());
@@ -751,7 +761,7 @@ pub mod tests {
assert!(patch.is_some());
assert!(push.is_none());
- assert_eq!(common.event_type, "patch");
+ assert_eq!(common.event_type, EventType::Patch);
assert_eq!(common.repository.id, project.id);
assert_eq!(common.repository.name, project.repo.project()?.name());