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/api_server.rs | 18 +++++++++--------- rust/src/database_models.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) (limited to 'rust') diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 3b052a6c..18a3ff32 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -96,7 +96,7 @@ impl Server { issn: rev.issn, publisher: rev.publisher, name: rev.name, - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -126,7 +126,7 @@ impl Server { issn: rev.issn, publisher: rev.publisher, name: rev.name, - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -153,7 +153,7 @@ impl Server { let entity = CreatorEntity { name: rev.name, orcid: rev.orcid, - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -181,7 +181,7 @@ impl Server { let entity = CreatorEntity { name: rev.name, orcid: rev.orcid, - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -209,7 +209,7 @@ impl Server { sha1: rev.sha1, size: rev.size.map(|v| v as isize), url: rev.url, - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -238,7 +238,7 @@ impl Server { sha1: rev.sha1, size: rev.size.map(|v| v as isize), url: rev.url, - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -264,7 +264,7 @@ impl Server { let entity = WorkEntity { work_type: rev.work_type, - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -298,7 +298,7 @@ impl Server { issue: rev.issue, container_id: None, // TODO work_id: rev.work_ident_id.to_string(), - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -333,7 +333,7 @@ impl Server { issue: rev.issue, container_id: None, // TODO work_id: rev.work_ident_id.to_string(), - state: None, // TODO: + state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), 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