1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
//! A UUID wrapper that has a base64 display and serialization //! //! # What? //! //! A newtype around UUIDs that: //! //! * Displays and Serializes as Base64 //! * Specifically it is the url-safe base64 variant, *with no padding* //! //! ```rust //! # extern crate uuid; //! # extern crate uuid_b64; //! # use uuid::Uuid; //! # use uuid_b64::UuidB64; //! # fn main() { //! let known_id = Uuid::parse_str("b0c1ee86-6f46-4f1b-8d8b-7849e75dbcee").unwrap(); //! let as_b64 = UuidB64::from(known_id); //! assert_eq!(as_b64.to_string(), "sMHuhm9GTxuNi3hJ51287g"); //! //! let parsed_b64: UuidB64 = "sMHuhm9GTxuNi3hJ51287g".parse().unwrap(); //! assert_eq!(parsed_b64, as_b64); //! //! let raw_id = Uuid::new_v4(); //! assert_eq!(raw_id.to_string().len(), 36); //! let uuidb64 = UuidB64::from(raw_id); //! assert_eq!(uuidb64.to_string().len(), 22); //! # } //! ``` //! //! `UuidB64::new` creates v4 UUIDs, because... that's what I use. I'm open to //! hearing arguments about why this is a ridiculous decision and I should have //! made `new` be `new_v4`. //! //! # Why? //! //! UUIDs are great: //! //! * They have a known size and representation, meaning that they are //! well-supported with an efficient representation in a wide variety of //! systems. Things like programming languages and databases. //! * V4 (almost completely random) UUIDs have nice sharding properties, you //! can give out UUIDs willy-nilly without coordination and still be //! guaranteed to not have a conflict of IDs between two items across //! systems. //! //! That said, the standard *representation* for UUIDs is kind of annoying: //! //! * It's a *long*: 36 characters to represent 16 bytes of data! //! * It's hard to read: it is only hexadecimal characters. The human eye needs //! to pay a lot of attention to be certain if any two UUIDs are the same. //! //! I guess that's it. Base64 is a more human-friendly representation of UUIDs: //! //! * It's slightly shorter: 1.375 times the size of the raw data (22 //! characters), vs 2.25 times the size characters. //! * Since it is case-sensitive, the *shape* of the IDs helps to distinguish //! between different IDs. There is also more entropy per character, so //! scanning to find a difference is faster. //! //! That said, there are drawbacks to something like this: //! //! * If you store it as a UUID column in a database IDs won't show up in the //! same way as it does in your application code, meaning you'll (A) maybe //! want to define a new database type, or B just expect to only ever //! interact with the DB via you application. //! //! Conversion functions are pretty trivial, this works in postgres //! (inefficiently, but it's only for interactive queries so whatever): //! //! ```sql //! CREATE FUNCTION b64uuid(encoded TEXT) RETURNS UUID //! AS $$ //! BEGIN //! RETURN ENCODE(DECODE(REPLACE(REPLACE( //! encoded, '-', '+'), '_', '/') || '==', 'base64'), 'hex')::UUID; //! END //! $$ LANGUAGE plpgsql; //! ``` //! //! # Usage //! //! Just use `UuidB64` everywhere you would use `Uuid`, and use `UuidB64::from` //! to create one from an existing UUID. //! //! ## Features //! //! * `serde` enables serialization/deserialization via Serde. //! * `diesel-uuid` enables integration with Diesel's UUID support, this is //! only tested on postgres, PRs welcome for other DBs. extern crate base64; #[cfg(feature = "diesel")] #[macro_use] extern crate diesel_derive_newtype; #[macro_use] extern crate error_chain; extern crate inlinable_string; #[macro_use] extern crate lazy_static; extern crate uuid; #[cfg(all(test, feature = "diesel-uuid"))] #[macro_use] extern crate diesel; #[cfg(all(test, feature = "diesel-uuid"))] #[cfg(all(test, feature = "serde"))] #[macro_use] extern crate serde_derive; #[cfg(all(test, feature = "serde"))] #[macro_use] extern crate serde_json; use std::convert::From; use std::str::FromStr; use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; use uuid::Uuid; use base64::{CharacterSet, Config, LineWrap}; use base64::display::Base64Display; use inlinable_string::inline_string::InlineString; use errors::{ErrorKind, ResultExt}; mod errors; #[cfg(feature = "serde")] mod serde_impl; lazy_static! { static ref B64_CONFIG: Config = Config::new( CharacterSet::UrlSafe, false, // pad? false, // trim whitespace? LineWrap::NoWrap, ); } /// It's a Uuid that displays as Base 64 #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[cfg_attr(feature = "diesel", derive(DieselNewType))] pub struct UuidB64(uuid::Uuid); impl UuidB64 { /// Generate a new v4 Uuid pub fn new() -> UuidB64 { UuidB64(Uuid::new_v4()) } /// Copy the raw UUID out pub fn uuid(&self) -> Uuid { self.0 } /// Convert this to a new [`InlineString`][] /// /// `InlineString`s are stack-allocated and therefore faster than /// heap-allocated Strings. How much faster? Somewhat faster: /// /// ```text /// test uuidb64_to_inline_string ... bench: 49 ns/iter (+/- 20) /// test uuidb64_to_inline_string_new_id_per_loop ... bench: 146 ns/iter (+/- 23) /// test uuidb64_to_string ... bench: 151 ns/iter (+/- 28) /// test uuidb64_to_string_new_id_per_loop ... bench: 268 ns/iter (+/- 144) /// ``` /// /// Honestly this is unlikely to matter for your use case, but since B64 /// UUIDs have a serialization that *does* fit within the InlineString /// limit (where the regular UUID representation does not) it felt like a /// waste to not do this. Also this is what is used for Serde, so we're /// zero-allocation for that. /// /// [`InlineString`]: https://docs.rs/inlinable_string/0.1.9/inlinable_string/inline_string/index.html pub fn to_istring(&self) -> InlineString { let mut buf = InlineString::from("0000000000000000000000"); // not actually zeroes unsafe { let raw_buf = buf.as_mut_slice(); base64::encode_config_slice(self.0.as_bytes(), *B64_CONFIG, &mut raw_buf[0..22]); } buf } /// Write the Base64-encoded UUID into the provided buffer /// /// ``` /// # extern crate uuid; /// # extern crate uuid_b64; /// # use uuid::Uuid; /// # use uuid_b64::UuidB64; /// # fn main() { /// let known_id = Uuid::parse_str("b0c1ee86-6f46-4f1b-8d8b-7849e75dbcee").unwrap(); /// let as_b64 = UuidB64::from(known_id); /// let mut buf = String::new(); /// as_b64.to_buf(&mut buf); /// assert_eq!(&buf, "sMHuhm9GTxuNi3hJ51287g"); /// # } /// ``` pub fn to_buf(&self, buffer: &mut String) { base64::encode_config_buf(self.0.as_bytes(), *B64_CONFIG, buffer); } } /// Parse a B64 encoded string into a UuidB64 /// /// ```rust /// # use uuid_b64::UuidB64; /// let parsed_b64: UuidB64 = "sMHuhm9GTxuNi3hJ51287g".parse().unwrap(); /// assert_eq!(format!("{:?}", parsed_b64), "UuidB64(sMHuhm9GTxuNi3hJ51287g)"); /// ``` impl FromStr for UuidB64 { type Err = errors::ErrorKind; fn from_str(s: &str) -> Result<Self, Self::Err> { let bytes = base64::decode_config(s, *B64_CONFIG).chain_err(|| ErrorKind::ParseError(s.into()))?; let id = Uuid::from_bytes(&bytes).chain_err(|| ErrorKind::ParseError(s.into()))?; Ok(UuidB64(id)) } } /// Right now this is just `Uuid`, but anything Uuid is comfortable with, we are impl<T> From<T> for UuidB64 where T: Into<Uuid>, { fn from(item: T) -> Self { UuidB64(item.into()) } } impl Debug for UuidB64 { /// Same as the display formatter, but includes `UuidB64()` around it /// /// ```rust /// # extern crate uuid; /// # extern crate uuid_b64; /// # use uuid::Uuid; /// # use uuid_b64::UuidB64; /// # fn main() { /// let known_id = Uuid::parse_str("b0c1ee86-6f46-4f1b-8d8b-7849e75dbcee").unwrap(); /// let as_b64 = UuidB64::from(known_id); /// assert_eq!(format!("{:?}", as_b64), "UuidB64(sMHuhm9GTxuNi3hJ51287g)"); /// # } /// ``` fn fmt(&self, f: &mut Formatter) -> FmtResult { write!(f, "UuidB64({})", self) } } impl Display for UuidB64 { /// Write Base64 encoding of this UUID /// /// ```rust /// # extern crate uuid; /// # extern crate uuid_b64; /// # use uuid::Uuid; /// # use uuid_b64::UuidB64; /// # fn main() { /// let known_id = Uuid::parse_str("b0c1ee86-6f46-4f1b-8d8b-7849e75dbcee").unwrap(); /// let as_b64 = UuidB64::from(known_id); /// assert_eq!(format!("{}", as_b64), "sMHuhm9GTxuNi3hJ51287g"); /// # } /// ``` fn fmt(&self, f: &mut Formatter) -> FmtResult { // can only hit this error if we use an invalid line length let wrapper = Base64Display::with_config(self.0.as_bytes(), *B64_CONFIG).unwrap(); write!(f, "{}", wrapper) } } #[cfg(test)] mod tests { use super::*; #[test] fn display_is_b64() { let id = UuidB64::new(); let fmted = format!("{}", id); assert_eq!(fmted.len(), 22); assert_eq!(format!("UuidB64({})", fmted), format!("{:?}", id)); } #[test] fn parse_roundtrips() { let original = UuidB64::new(); let encoded = format!("{}", original); let parsed: UuidB64 = encoded.parse().unwrap(); assert_eq!(parsed, original); } #[test] fn from_uuid_works() { let _ = UuidB64::from(Uuid::new_v4()); } #[test] fn to_istring_works() { let b64 = UuidB64::from(Uuid::parse_str("b0c1ee86-6f46-4f1b-8d8b-7849e75dbcee").unwrap()); assert_eq!(b64.to_istring(), "sMHuhm9GTxuNi3hJ51287g"); for _ in 0..10 { let b64 = UuidB64::new(); b64.to_istring(); } } } #[cfg(all(test, feature = "diesel-uuid"))] mod diesel_tests { use diesel; use diesel::prelude::*; use diesel::dsl::sql; use diesel::pg::PgConnection; use std::env; use super::UuidB64; #[derive(Debug, Clone, PartialEq, Identifiable, Insertable, Queryable)] #[table_name = "my_entities"] pub struct MyEntity { id: UuidB64, val: i32, } table! { my_entities { id -> Uuid, val -> Integer, } } #[cfg(test)] fn setup() -> PgConnection { let db_url = env::var("PG_DATABASE_URL").expect("PG_DB_URL must be in the environment"); let conn = PgConnection::establish(&db_url).unwrap(); #[allow(deprecated)] // not present in diesel 1.0 let setup = sql::<diesel::types::Bool>( "CREATE TABLE IF NOT EXISTS my_entities ( id UUID PRIMARY KEY, val Int )", ); setup.execute(&conn).expect("Can't create table"); conn } #[test] fn does_roundtrip() { use self::my_entities::dsl::*; let conn = setup(); let obj = MyEntity { id: UuidB64::new(), val: 1, }; diesel::insert_into(my_entities) .values(&obj) .execute(&conn) .expect("Couldn't insert struct into my_entities"); let found: Vec<MyEntity> = my_entities.load(&conn).unwrap(); assert_eq!(found[0], obj); diesel::delete(my_entities.filter(id.eq(&obj.id))) .execute(&conn) .expect("Couldn't delete existing object"); } }