aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src/api_helpers.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-07-20 14:33:09 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-07-20 14:33:09 -0700
commitb4eb110bd880f78c5da578fe897ae97d4c734984 (patch)
tree2eef3aeef360e548680c431abb2b5547f4242632 /rust/src/api_helpers.rs
parente4c1514294443b9e6f6ed716dcad5ebec64c3af8 (diff)
downloadfatcat-b4eb110bd880f78c5da578fe897ae97d4c734984.tar.gz
fatcat-b4eb110bd880f78c5da578fe897ae97d4c734984.zip
rust: base32 encoded idents
Diffstat (limited to 'rust/src/api_helpers.rs')
-rw-r--r--rust/src/api_helpers.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs
index 62fc4569..1ee08c76 100644
--- a/rust/src/api_helpers.rs
+++ b/rust/src/api_helpers.rs
@@ -1,8 +1,10 @@
+use data_encoding::BASE32_NOPAD;
use database_models::*;
use database_schema::*;
use diesel;
use diesel::prelude::*;
use errors::*;
+use uuid::Uuid;
pub fn get_or_create_editgroup(editor_id: i64, conn: &PgConnection) -> Result<i64> {
// check for current active
@@ -85,3 +87,22 @@ pub fn accept_editgroup(editgroup_id: i64, conn: &PgConnection) -> Result<Change
Ok(entry)
})
}
+
+/// Convert fatcat IDs (base32 strings) to UUID
+pub fn fcid2uuid(fcid: &str) -> Result<Uuid> {
+ if fcid.len() != 26 {
+ return Err(ErrorKind::InvalidFatcatId(fcid.to_string()).into());
+ }
+ let mut raw = vec![0; 16];
+ BASE32_NOPAD
+ .decode_mut(fcid.to_uppercase().as_bytes(), &mut raw)
+ .map_err(|_dp| ErrorKind::InvalidFatcatId(fcid.to_string()))?;
+ // unwrap() is safe here, because we know raw is always 16 bytes
+ Ok(Uuid::from_bytes(&raw).unwrap())
+}
+
+/// Convert UUID to fatcat ID string (base32 encoded)
+pub fn uuid2fcid(id: &Uuid) -> String {
+ let raw = id.as_bytes();
+ BASE32_NOPAD.encode(raw).to_lowercase()
+}