summaryrefslogtreecommitdiffstats
path: root/rust/src/api_helpers.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-07-25 12:18:34 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-07-25 12:18:34 -0700
commitc4618dd2fb070eefcdb38bca7b92fe32da766702 (patch)
treec874372bb6574e92db1e7bb98029ee0ee186326f /rust/src/api_helpers.rs
parenta4e45f77b5431e0211337afc67ee586c553590d3 (diff)
downloadfatcat-c4618dd2fb070eefcdb38bca7b92fe32da766702.tar.gz
fatcat-c4618dd2fb070eefcdb38bca7b92fe32da766702.zip
more external ident handling
Diffstat (limited to 'rust/src/api_helpers.rs')
-rw-r--r--rust/src/api_helpers.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs
index f0f56a6b..ef07ee55 100644
--- a/rust/src/api_helpers.rs
+++ b/rust/src/api_helpers.rs
@@ -5,6 +5,8 @@ use diesel;
use diesel::prelude::*;
use errors::*;
use uuid::Uuid;
+use regex::Regex;
+
pub fn get_or_create_editgroup(editor_id: Uuid, conn: &PgConnection) -> Result<Uuid> {
// check for current active
@@ -109,3 +111,85 @@ pub fn uuid2fcid(id: &Uuid) -> String {
let raw = id.as_bytes();
BASE32_NOPAD.encode(raw).to_lowercase()
}
+
+pub fn check_pmcid(raw: &str) -> Result<()> {
+ lazy_static! {
+ static ref RE: Regex = Regex::new(r"^PMC\d+$").unwrap();
+ }
+ if RE.is_match(raw) {
+ Ok(())
+ } else {
+ Err(ErrorKind::MalformedExternalId(
+ format!("not a valid PubMed Central ID (PMCID): '{}' (expected, eg, 'PMC12345')", raw)
+ ).into())
+ }
+}
+
+pub fn check_pmid(raw: &str) -> Result<()> {
+ lazy_static! {
+ static ref RE: Regex = Regex::new(r"^\d+$").unwrap();
+ }
+ if RE.is_match(raw) {
+ Ok(())
+ } else {
+ Err(ErrorKind::MalformedExternalId(
+ format!("not a valid PubMed ID (PMID): '{}' (expected, eg, '1234')", raw)
+ ).into())
+ }
+}
+
+pub fn check_wikidata_qid(raw: &str) -> Result<()> {
+ lazy_static! {
+ static ref RE: Regex = Regex::new(r"^Q\d+$").unwrap();
+ }
+ if RE.is_match(raw) {
+ Ok(())
+ } else {
+ Err(ErrorKind::MalformedExternalId(
+ format!("not a valid Wikidata QID: '{}' (expected, eg, 'Q1234')", raw)
+ ).into())
+ }
+}
+
+pub fn check_doi(raw: &str) -> Result<()> {
+ lazy_static! {
+ static ref RE: Regex = Regex::new(r"^10.\d{3,6}/.+$").unwrap();
+ }
+ if RE.is_match(raw) {
+ Ok(())
+ } else {
+ Err(ErrorKind::MalformedExternalId(
+ format!("not a valid DOI: '{}' (expected, eg, '10.1234/aksjdfh')", raw)
+ ).into())
+ }
+}
+
+pub fn check_issn(raw: &str) -> Result<()> {
+ lazy_static! {
+ static ref RE: Regex = Regex::new(r"^\d{4}-\d{3}[0-9X]$").unwrap();
+ }
+ if RE.is_match(raw) {
+ Ok(())
+ } else {
+ Err(ErrorKind::MalformedExternalId(
+ format!("not a valid ISSN: '{}' (expected, eg, '1234-5678')", raw)
+ ).into())
+ }
+}
+
+pub fn check_orcid(raw: &str) -> Result<()> {
+ lazy_static! {
+ static ref RE: Regex = Regex::new(r"^\d{4}-\d{4}-\d{4}-\d{4}$").unwrap();
+ }
+ if RE.is_match(raw) {
+ Ok(())
+ } else {
+ Err(ErrorKind::MalformedExternalId(
+ format!("not a valid ORCID: '{}' (expected, eg, '0123-4567-3456-6789')", raw)
+ ).into())
+ }
+}
+
+// TODO: make the above checks "more correct"
+// TODO: check ISBN-13
+// TODO: check hashes (SHA-1, etc)