From c76d9d069db5c2c2cb0ab386e40130d759c856f2 Mon Sep 17 00:00:00 2001 From: bouzuya Date: Sat, 20 Jan 2024 11:54:27 +0900 Subject: [PATCH] BREAKING CHANGE: Add DatabaseName::into_collection and change collection to no longer consume self --- src/database_name.rs | 57 +++++++++++++++++++++++++++++++++++++--- tests/v0_7_0.rs | 62 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 113 insertions(+), 6 deletions(-) diff --git a/src/database_name.rs b/src/database_name.rs index e8db317..82fb474 100644 --- a/src/database_name.rs +++ b/src/database_name.rs @@ -91,19 +91,19 @@ impl DatabaseName { /// "projects/my-project/databases/my-database" /// )?; /// assert_eq!( - /// database_name.clone().collection("chatrooms")?, + /// database_name.collection("chatrooms")?, /// CollectionName::from_str( /// "projects/my-project/databases/my-database/documents/chatrooms" /// )? /// ); /// assert_eq!( - /// database_name.clone().collection("chatrooms/chatroom1/messages")?, + /// database_name.collection("chatrooms/chatroom1/messages")?, /// CollectionName::from_str( /// "projects/my-project/databases/my-database/documents/chatrooms/chatroom1/messages" /// )? /// ); /// assert_eq!( - /// database_name.clone().collection(CollectionId::from_str("chatrooms")?)?, + /// database_name.collection(CollectionId::from_str("chatrooms")?)?, /// CollectionName::from_str( /// "projects/my-project/databases/my-database/documents/chatrooms" /// )? @@ -119,7 +119,56 @@ impl DatabaseName { /// # } /// ``` /// - pub fn collection(self, collection_path: T) -> Result + pub fn collection(&self, collection_path: T) -> Result + where + E: std::fmt::Display, + T: TryInto, + { + self.clone().into_collection(collection_path) + } + + /// Creates a new `CollectionName` by consuming the `DatabaseName` with the provided `collection_path`. + /// + /// # Examples + /// + /// ```rust + /// # fn main() -> anyhow::Result<()> { + /// use firestore_path::{CollectionId,CollectionName,CollectionPath,DatabaseName}; + /// use std::str::FromStr; + /// + /// let database_name = DatabaseName::from_str( + /// "projects/my-project/databases/my-database" + /// )?; + /// assert_eq!( + /// database_name.clone().into_collection("chatrooms")?, + /// CollectionName::from_str( + /// "projects/my-project/databases/my-database/documents/chatrooms" + /// )? + /// ); + /// assert_eq!( + /// database_name.clone().into_collection("chatrooms/chatroom1/messages")?, + /// CollectionName::from_str( + /// "projects/my-project/databases/my-database/documents/chatrooms/chatroom1/messages" + /// )? + /// ); + /// assert_eq!( + /// database_name.clone().into_collection(CollectionId::from_str("chatrooms")?)?, + /// CollectionName::from_str( + /// "projects/my-project/databases/my-database/documents/chatrooms" + /// )? + /// ); + /// assert_eq!( + /// database_name.into_collection(CollectionPath::from_str("chatrooms/chatroom1/messages")?)?, + /// CollectionName::from_str( + /// "projects/my-project/databases/my-database/documents/chatrooms/chatroom1/messages" + /// )? + /// ); + /// + /// # Ok(()) + /// # } + /// ``` + /// + pub fn into_collection(self, collection_path: T) -> Result where E: std::fmt::Display, T: TryInto, diff --git a/tests/v0_7_0.rs b/tests/v0_7_0.rs index 07014dc..8f55c19 100644 --- a/tests/v0_7_0.rs +++ b/tests/v0_7_0.rs @@ -1,8 +1,8 @@ use std::str::FromStr; use firestore_path::{ - CollectionName, CollectionPath, DatabaseName, DocumentId, DocumentName, DocumentPath, - RootDocumentName, + CollectionId, CollectionName, CollectionPath, DatabaseName, DocumentId, DocumentName, + DocumentPath, RootDocumentName, }; #[test] @@ -80,6 +80,33 @@ fn test_collection_path_into_doc() -> anyhow::Result<()> { Ok(()) } +#[test] +fn test_database_name_collection() -> anyhow::Result<()> { + // BREAKING CHANGE: DatabaseName::collection doesn't consume self. + let database_name = DatabaseName::from_str("projects/my-project/databases/my-database")?; + assert_eq!( + database_name.collection("chatrooms")?, + CollectionName::from_str("projects/my-project/databases/my-database/documents/chatrooms")? + ); + assert_eq!( + database_name.collection("chatrooms/chatroom1/messages")?, + CollectionName::from_str( + "projects/my-project/databases/my-database/documents/chatrooms/chatroom1/messages" + )? + ); + assert_eq!( + database_name.collection(CollectionId::from_str("chatrooms")?)?, + CollectionName::from_str("projects/my-project/databases/my-database/documents/chatrooms")? + ); + assert_eq!( + database_name.collection(CollectionPath::from_str("chatrooms/chatroom1/messages")?)?, + CollectionName::from_str( + "projects/my-project/databases/my-database/documents/chatrooms/chatroom1/messages" + )? + ); + Ok(()) +} + #[test] fn test_database_name_doc() -> anyhow::Result<()> { // BREAKING CHANGE: DatabaseName::doc doesn't consume self. @@ -111,6 +138,37 @@ fn test_database_name_doc() -> anyhow::Result<()> { Ok(()) } +#[test] +fn test_database_name_into_collection() -> anyhow::Result<()> { + // Added: DatabaseName::into_collection + let database_name = DatabaseName::from_str("projects/my-project/databases/my-database")?; + assert_eq!( + database_name.clone().into_collection("chatrooms")?, + CollectionName::from_str("projects/my-project/databases/my-database/documents/chatrooms")? + ); + assert_eq!( + database_name + .clone() + .into_collection("chatrooms/chatroom1/messages")?, + CollectionName::from_str( + "projects/my-project/databases/my-database/documents/chatrooms/chatroom1/messages" + )? + ); + assert_eq!( + database_name + .clone() + .into_collection(CollectionId::from_str("chatrooms")?)?, + CollectionName::from_str("projects/my-project/databases/my-database/documents/chatrooms")? + ); + assert_eq!( + database_name.into_collection(CollectionPath::from_str("chatrooms/chatroom1/messages")?)?, + CollectionName::from_str( + "projects/my-project/databases/my-database/documents/chatrooms/chatroom1/messages" + )? + ); + Ok(()) +} + #[test] fn test_database_name_into_doc() -> anyhow::Result<()> { // Added: DatabaseName::into_doc