From 7ee9e001bc908aa6a934751bdef5081977d034db Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 23 May 2018 19:02:53 -0700 Subject: add state param generation --- rust/src/database_models.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'rust/src/database_models.rs') diff --git a/rust/src/database_models.rs b/rust/src/database_models.rs index 9aebb0f9..0e138439 100644 --- a/rust/src/database_models.rs +++ b/rust/src/database_models.rs @@ -2,10 +2,33 @@ use chrono; //use serde_json; use database_schema::*; use uuid::Uuid; +use errors::*; // Ugh. I thought the whole point was to *not* do this, but: // https://github.com/diesel-rs/diesel/issues/1589 +pub enum EntityState { + WorkInProgress, + Active(i64), + Redirect(Uuid, i64), + Deleted, +} + +impl EntityState { + pub fn shortname(&self) -> String { + match self { + EntityState::WorkInProgress => "wip", + EntityState::Active(_) => "active", + EntityState::Redirect(_,_) => "redirect", + EntityState::Deleted => "deleted", + }.to_string() + } +} + +pub trait EntityIdentRow { + fn state(&self) -> Result; +} + // Helper for constructing tables macro_rules! entity_structs { ($edit_table:expr, $edit_struct:ident, $ident_table:expr, $ident_struct:ident) => { @@ -28,6 +51,20 @@ macro_rules! entity_structs { pub rev_id: Option, pub redirect_id: Option, } + + impl EntityIdentRow for $ident_struct { + fn state(&self) -> Result { + if !self.is_live { + return Ok(EntityState::WorkInProgress); + } + match (self.redirect_id, self.rev_id) { + (None, None) => Ok(EntityState::Deleted), + (Some(redir), Some(rev)) => Ok(EntityState::Redirect(redir, rev)), + (None, Some(rev)) => Ok(EntityState::Active(rev)), + _ => bail!("Invalid EntityIdentRow state") + } + } + } }; } -- cgit v1.2.3