diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-09 18:17:20 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2019-01-09 18:22:43 -0800 |
commit | c8a8cb4ec532cbf1a6821fba4b2084b4f0e7b4c0 (patch) | |
tree | fa3fe500bc76aa24ed7810ce96d4fd4aab42ffb7 /rust | |
parent | 181d80073a88ccce6925dd5663bb2c5c716d6e54 (diff) | |
download | fatcat-c8a8cb4ec532cbf1a6821fba4b2084b4f0e7b4c0.tar.gz fatcat-c8a8cb4ec532cbf1a6821fba4b2084b4f0e7b4c0.zip |
impl AsRef, From, Into for FatCatId
Diffstat (limited to 'rust')
-rw-r--r-- | rust/src/identifiers.rs | 22 | ||||
-rw-r--r-- | rust/tests/test_fcid.rs | 27 |
2 files changed, 42 insertions, 7 deletions
diff --git a/rust/src/identifiers.rs b/rust/src/identifiers.rs index 5cfa2fb6..e107d16f 100644 --- a/rust/src/identifiers.rs +++ b/rust/src/identifiers.rs @@ -4,7 +4,7 @@ use regex::Regex; use serde_json; use std::str::FromStr; use uuid::Uuid; -use std::fmt; +use std::{fmt, convert}; #[derive(Clone, Copy, PartialEq, Debug)] @@ -23,11 +23,29 @@ impl FromStr for FatCatId { } } +impl convert::AsRef<Uuid> for FatCatId { + fn as_ref(&self) -> &Uuid { + &self.0 + } +} + +impl convert::Into<Uuid> for FatCatId { + fn into(self) -> Uuid { + self.0 + } +} + +impl convert::From<Uuid> for FatCatId { + fn from(u: Uuid) -> FatCatId { + FatCatId(u) + } +} + impl FatCatId { pub fn to_uuid(&self) -> Uuid { self.0 } - // TODO: just make it u: Uuid and clone (not by ref) + // TODO: make it possible to just pass 'Uuid' in addition to '&Uuid' pub fn from_uuid(u: &Uuid) -> FatCatId { FatCatId(*u) } diff --git a/rust/tests/test_fcid.rs b/rust/tests/test_fcid.rs index aac27129..31f7030a 100644 --- a/rust/tests/test_fcid.rs +++ b/rust/tests/test_fcid.rs @@ -1,17 +1,19 @@ extern crate fatcat; extern crate uuid; -use fatcat::identifiers::{fcid2uuid, uuid2fcid}; +// TODO: these should just be in-line in identifiers.rs + +use fatcat::identifiers::{fcid2uuid, uuid2fcid, FatCatId}; use uuid::Uuid; #[test] fn test_fcid_conversions() { let test_uuid = Uuid::parse_str("86daea5b-1b6b-432a-bb67-ea97795f80fe").unwrap(); - let test_fcid = "q3nouwy3nnbsvo3h5klxsx4a7y"; + let test_str = "q3nouwy3nnbsvo3h5klxsx4a7y"; - assert_eq!(test_fcid, uuid2fcid(&test_uuid)); - assert_eq!(test_uuid, fcid2uuid(test_fcid).unwrap()); - assert_eq!(test_uuid, fcid2uuid(&test_fcid.to_uppercase()).unwrap()); + assert_eq!(test_str, uuid2fcid(&test_uuid)); + assert_eq!(test_uuid, fcid2uuid(test_str).unwrap()); + assert_eq!(test_uuid, fcid2uuid(&test_str.to_uppercase()).unwrap()); assert_eq!(test_uuid, fcid2uuid(&uuid2fcid(&test_uuid)).unwrap()); assert_eq!( @@ -24,3 +26,18 @@ fn test_fcid_conversions() { assert_eq!(false, fcid2uuid("10Oouwy3nnbsvo3h5klxsx4a7y").is_ok()); assert_eq!(false, fcid2uuid("cccccccccccccccccccccccccc").is_ok()); } + +#[test] +fn test_fcid_struct() { + let test_uuid = Uuid::parse_str("86daea5b-1b6b-432a-bb67-ea97795f80fe").unwrap(); + let test_str = "q3nouwy3nnbsvo3h5klxsx4a7y"; + let test_fcid = FatCatId::from_uuid(&test_uuid); + + assert_eq!(test_str, test_fcid.to_string()); + assert_eq!(test_str, format!("{}", test_fcid)); + assert_eq!(test_uuid, test_fcid.to_uuid()); + + // Inner UUID isn't public, so this doesn't work + //let test_fcid2 = FatCatId(test_uuid); + //assert_eq!(test_fcid, test_fcid2); +} |