aboutsummaryrefslogtreecommitdiffstats
path: root/rust
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
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')
-rw-r--r--rust/Cargo.lock9
-rw-r--r--rust/Cargo.toml1
-rw-r--r--rust/src/lib.rs21
3 files changed, 30 insertions, 1 deletions
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index c8dc516f..8528ed90 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -166,6 +166,11 @@ dependencies = [
]
[[package]]
+name = "data-encoding"
+version = "2.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "diesel"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -292,6 +297,7 @@ version = "0.1.0"
dependencies = [
"chrono 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "data-encoding 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_migrations 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -302,7 +308,7 @@ dependencies = [
"iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"iron-slog 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"iron-test 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
"slog 2.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1264,6 +1270,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum chunked_transfer 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "498d20a7aaf62625b9bf26e637cf7736417cde1d0c99f1d04d1170229a85cf87"
"checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536"
"checksum conduit-mime-types 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "95ca30253581af809925ef68c2641cc140d6183f43e12e0af4992d53768bd7b8"
+"checksum data-encoding 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "67df0571a74bf0d97fb8b2ed22abdd9a48475c96bd327db968b7d9cace99655e"
"checksum diesel 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9db28a518f3e0c61bdb0044d9a8b2b369373c090c19fec07e49e3b8511dcffa"
"checksum diesel_derives 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "03bcaf77491f53e400d5ee3bdd57142ea4e1c47fe9217b3361ff9a76ca0e3d37"
"checksum diesel_migrations 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b42c35d1ce9e8d57a3e7001b4127f2bc1b073a89708bb7019f5be27c991c28"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 1a584d84..07020e67 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -15,6 +15,7 @@ clap = "*"
error-chain = "0.11"
uuid = "0.5"
log = "*"
+data-encoding = "2.1"
# API server
chrono = { version = "0.4", features = ["serde"] }
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()
+}