summaryrefslogtreecommitdiffstats
path: root/rust/src/lib.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/lib.rs
parente4c1514294443b9e6f6ed716dcad5ebec64c3af8 (diff)
downloadfatcat-b4eb110bd880f78c5da578fe897ae97d4c734984.tar.gz
fatcat-b4eb110bd880f78c5da578fe897ae97d4c734984.zip
rust: base32 encoded idents
Diffstat (limited to 'rust/src/lib.rs')
-rw-r--r--rust/src/lib.rs26
1 files changed, 6 insertions, 20 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 9de94d86..86e367e4 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -33,7 +33,12 @@ mod errors {
Uuid(::uuid::ParseError);
Io(::std::io::Error) #[cfg(unix)];
Serde(::serde_json::Error);
- Base32(::data_encoding::DecodeError);
+ }
+ errors {
+ InvalidFatcatId(id: String) {
+ description("invalid fatcat identifier syntax")
+ display("invalid fatcat identifier (expect 26-char base32 encoded): {}", id)
+ }
}
}
}
@@ -49,7 +54,6 @@ 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/");
@@ -103,21 +107,3 @@ 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()
-}