Skip to content

Commit

Permalink
BREAKING CHANGE: Add DatabaseName::into_collection and change collect…
Browse files Browse the repository at this point in the history
…ion to no longer consume self
  • Loading branch information
bouzuya committed Jan 20, 2024
1 parent 6a03e72 commit c76d9d0
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 6 deletions.
57 changes: 53 additions & 4 deletions src/database_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/// )?
Expand All @@ -119,7 +119,56 @@ impl DatabaseName {
/// # }
/// ```
///
pub fn collection<E, T>(self, collection_path: T) -> Result<CollectionName, Error>
pub fn collection<E, T>(&self, collection_path: T) -> Result<CollectionName, Error>
where
E: std::fmt::Display,
T: TryInto<CollectionPath, Error = E>,
{
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<E, T>(self, collection_path: T) -> Result<CollectionName, Error>
where
E: std::fmt::Display,
T: TryInto<CollectionPath, Error = E>,
Expand Down
62 changes: 60 additions & 2 deletions tests/v0_7_0.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c76d9d0

Please sign in to comment.