summaryrefslogtreecommitdiffstats
path: root/rust/src/lib.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-07-20 13:34:33 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-07-20 13:34:37 -0700
commit4144debb6c0317efcde3c97fc6d8b369c6530b6d (patch)
treee8582f3daa70e954a297572bd25301e54e185967 /rust/src/lib.rs
parent5e41d3946541b160ff9329c39357038e7776846c (diff)
downloadfatcat-4144debb6c0317efcde3c97fc6d8b369c6530b6d.tar.gz
fatcat-4144debb6c0317efcde3c97fc6d8b369c6530b6d.zip
entity ident (fcid) UUID helpers
Could perhaps implement these as impl methods on the uuid::Uuid struct instead?
Diffstat (limited to 'rust/src/lib.rs')
-rw-r--r--rust/src/lib.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index d9193f2f..9de94d86 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -16,6 +16,7 @@ extern crate iron;
extern crate serde_json;
#[macro_use]
extern crate log;
+extern crate data_encoding;
pub mod api_helpers;
pub mod api_server;
@@ -32,6 +33,7 @@ mod errors {
Uuid(::uuid::ParseError);
Io(::std::io::Error) #[cfg(unix)];
Serde(::serde_json::Error);
+ Base32(::data_encoding::DecodeError);
}
}
}
@@ -47,6 +49,7 @@ use dotenv::dotenv;
use iron::middleware::AfterMiddleware;
use iron::{Request, Response};
use std::env;
+use data_encoding::BASE32_NOPAD;
#[cfg(feature = "postgres")]
embed_migrations!("../migrations/");
@@ -100,3 +103,21 @@ impl AfterMiddleware for XClacksOverheadMiddleware {
Ok(res)
}
}
+
+/// Convert fatcat IDs (base32 strings) to UUID
+pub fn fcid2uuid(fcid: &str) -> Result<uuid::Uuid> {
+ if fcid.len() != 20 {
+ bail!("invalid fatcat ID (expecting 20-chars of base32");
+ }
+ let mut raw = vec![0; 16];
+ BASE32_NOPAD.decode_mut(fcid.to_uppercase().as_bytes(), &mut raw)
+ .map_err(|dp| dp.error)?;
+ // unwrap() is safe here, because we know raw is always 16 bytes
+ Ok(uuid::Uuid::from_bytes(&raw).unwrap())
+}
+
+/// Convert UUID to fatcat ID string (base32 encoded)
+pub fn uuid2fcid(id: &uuid::Uuid) -> String {
+ let raw = id.as_bytes();
+ BASE32_NOPAD.encode(raw).to_lowercase()
+}