aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src/database_models.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-05-23 19:02:53 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-05-24 15:21:32 -0700
commit7ee9e001bc908aa6a934751bdef5081977d034db (patch)
tree0c27b9ca069896d840e502882a318f14a2d41bb6 /rust/src/database_models.rs
parent4f7db09a5367170c2b06d6067e840c841e15eed6 (diff)
downloadfatcat-7ee9e001bc908aa6a934751bdef5081977d034db.tar.gz
fatcat-7ee9e001bc908aa6a934751bdef5081977d034db.zip
add state param generation
Diffstat (limited to 'rust/src/database_models.rs')
-rw-r--r--rust/src/database_models.rs37
1 files changed, 37 insertions, 0 deletions
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<EntityState>;
+}
+
// 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<i64>,
pub redirect_id: Option<Uuid>,
}
+
+ impl EntityIdentRow for $ident_struct {
+ fn state(&self) -> Result<EntityState> {
+ 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")
+ }
+ }
+ }
};
}