diff --git a/Cargo.lock b/Cargo.lock index c42a9f5..e88d352 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2871,7 +2871,7 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "surreal-id" -version = "0.1.1" +version = "0.2.0" dependencies = [ "pretty_assertions", "proptest", diff --git a/Cargo.toml b/Cargo.toml index a6b8d46..f215b3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "surreal-id" -version = "0.1.1" +version = "0.2.0" edition = "2021" license = "MIT OR Apache-2.0" description = "A package for easily creating ID types for usage with surrealdb" diff --git a/README.md b/README.md index be1e327..5ef0815 100644 --- a/README.md +++ b/README.md @@ -42,16 +42,17 @@ pub struct User { name: String, } -// The new function is automatically created for us by the blanket implementation from the NewId trait -let typesafe_custom_id = UserId::new("fa77edc3-56ed-4208-9e0b-c0b1c32e2d34")?, +// The new function is automatically created for us +// by the blanket implementation from the NewId trait +let typesafe_custom_id = UserId::new("fa77edc3-56ed-4208-9e0b-c0b1c32e2d34").unwrap(); let user_to_be_created = User { id: typesafe_custom_id, name: "John Doe".to_string(), }; -let db = Surreal::new::(()).await?; -db.use_ns("test").use_db("test").await?; +let db = Surreal::new::(()).await.unwrap(); +db.use_ns("test").use_db("test").await.unwrap(); let create_result = db.create(USERS_TABLE).content(&user_to_be_created).await; let retrieved_user: User = create_result.unwrap().remove(0); diff --git a/src/new_id.rs b/src/new_id.rs index c0dbcd1..f5c3755 100644 --- a/src/new_id.rs +++ b/src/new_id.rs @@ -58,4 +58,29 @@ pub trait NewId: Sized { /// /// The inner ID is expected to implement the conversion into `surrealdb::sql::Id`. fn from_inner_id>(inner_id: T) -> Self; + + /// Returns the table name associated with this ID type. + fn table(&self) -> &'static str { + Self::TABLE + } + + /// Returns the inner ID as a string without the table name or colon, but including the brackets. + fn id_with_brackets(&self) -> String { + format!("⟨{}⟩", &self.id_without_brackets()) + } + + /// Returns the inner ID as a string without the table name, colon and brackets. + fn id_without_brackets(&self) -> String { + let original_id = self.get_inner_string(); + original_id + .split(':') + .next() + .unwrap_or(&original_id) + .chars() + .filter(|&c| c != '⟨' && c != '⟩') + .collect() + } + + /// Provided by the implementer, returns the inner ID as a string. + fn get_inner_string(&self) -> String; } diff --git a/tests/user_id_integration.rs b/tests/user_id_integration.rs index f84d678..1716cad 100644 --- a/tests/user_id_integration.rs +++ b/tests/user_id_integration.rs @@ -16,6 +16,10 @@ impl NewId for UserId { id: inner_id.into(), }) } + + fn get_inner_string(&self) -> String { + self.0.id.to_string() + } } #[derive(Debug, Serialize, Deserialize, PartialEq)] @@ -33,6 +37,34 @@ mod tests { use super::*; use pretty_assertions::assert_eq; + #[test] + fn id_with_brackets() { + let id = "users:⟨fa77edc3-56ed-4208-9e0b-c0b1c32e2d34⟩"; + let user_id = UserId::new(id); + assert_eq!( + user_id.unwrap().id_with_brackets(), + "⟨fa77edc3-56ed-4208-9e0b-c0b1c32e2d34⟩", + ); + } + + #[test] + fn id_without_brackets() { + let id = "users:⟨fa77edc3-56ed-4208-9e0b-c0b1c32e2d34⟩"; + let user_id = UserId::new(id); + assert_eq!( + user_id.unwrap().id_without_brackets(), + "fa77edc3-56ed-4208-9e0b-c0b1c32e2d34", + ); + } + + #[test] + fn table_part_returns_correct_table() { + let id = "users:⟨fa77edc3-56ed-4208-9e0b-c0b1c32e2d34⟩"; + let user_id = UserId::new(id); + assert!(user_id.is_ok()); + assert_eq!(user_id.unwrap().table(), USERS_TABLE); + } + #[test] fn valid_id_and_table_creates_user_id_successfully() { let id = "users:⟨fa77edc3-56ed-4208-9e0b-c0b1c32e2d34⟩";