Skip to content

Commit

Permalink
Add id_with_brackets, table and id_without_bracket methods
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwh committed Oct 2, 2023
1 parent 2d559ea commit b6ffe4c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Mem>(()).await?;
db.use_ns("test").use_db("test").await?;
let db = Surreal::new::<Mem>(()).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);
Expand Down
25 changes: 25 additions & 0 deletions src/new_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Into<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;
}
32 changes: 32 additions & 0 deletions tests/user_id_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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⟩";
Expand Down

0 comments on commit b6ffe4c

Please sign in to comment.