Skip to content

Commit

Permalink
Change Database::collection
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 20, 2023
1 parent a553df0 commit 87de87c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
74 changes: 34 additions & 40 deletions src/database_name.rs
@@ -1,8 +1,6 @@
use std::str::FromStr;

use crate::{
error::ErrorKind, CollectionId, CollectionName, CollectionPath, DatabaseId, Error, ProjectId,
};
use crate::{error::ErrorKind, CollectionName, CollectionPath, DatabaseId, Error, ProjectId};

/// A database name.
///
Expand Down Expand Up @@ -54,35 +52,56 @@ impl DatabaseName {
}
}

/// Creates a new `CollectionName` from this `DatabaseName` and `collection_id`.
/// Creates a new `CollectionName` from this `DatabaseName` and `collection_path`.
///
/// # Examples
///
/// ```rust
/// # fn main() -> anyhow::Result<()> {
/// use firestore_path::{CollectionName,DatabaseName};
/// use firestore_path::{CollectionId,CollectionName,CollectionPath,DatabaseName};
/// use std::str::FromStr;
///
/// let database_name = DatabaseName::from_str("projects/my-project/databases/my-database/documents")?;
/// let database_name = DatabaseName::from_str(
/// "projects/my-project/databases/my-database/documents"
/// )?;
/// assert_eq!(
/// database_name.collection("chatrooms")?,
/// CollectionName::from_str("projects/my-project/databases/my-database/documents/chatrooms")?
/// database_name.clone().collection("chatrooms")?,
/// CollectionName::from_str(
/// "projects/my-project/databases/my-database/documents/chatrooms"
/// )?
/// );
/// assert_eq!(
/// database_name.clone().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")?)?,
/// 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(())
/// # }
/// ```
///
pub fn collection<E, T>(self, collection_id: T) -> Result<CollectionName, Error>
pub fn collection<E, T>(self, collection_path: T) -> Result<CollectionName, Error>
where
E: std::fmt::Display,
T: TryInto<CollectionId, Error = E>,
T: TryInto<CollectionPath, Error = E>,
{
let collection_id = collection_id
let collection_path = collection_path
.try_into()
.map_err(|e| Error::from(ErrorKind::CollectionIdConversion(e.to_string())))?;
let collection_path = CollectionPath::new(None, collection_id);
let collection_name = CollectionName::new(self, collection_path);
Ok(collection_name)
.map_err(|e| Error::from(ErrorKind::CollectionPathConversion(e.to_string())))?;
Ok(CollectionName::new(self, collection_path))
}
}

Expand Down Expand Up @@ -151,31 +170,6 @@ mod tests {
Ok(())
}

#[test]
fn test_collection() -> anyhow::Result<()> {
let database_name =
DatabaseName::from_str("projects/my-project/databases/my-database/documents")?;
let collection_name = database_name.collection("chatrooms")?;
assert_eq!(
collection_name,
CollectionName::from_str(
"projects/my-project/databases/my-database/documents/chatrooms"
)?
);

let database_name =
DatabaseName::from_str("projects/my-project/databases/my-database/documents")?;
let collection_id = CollectionId::from_str("chatrooms")?;
let collection_name = database_name.collection(collection_id)?;
assert_eq!(
collection_name,
CollectionName::from_str(
"projects/my-project/databases/my-database/documents/chatrooms"
)?
);
Ok(())
}

#[test]
fn test_impl_from_str_and_impl_try_from_string() -> anyhow::Result<()> {
for (s, expected) in [
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Expand Up @@ -7,6 +7,8 @@ pub struct Error(#[from] ErrorKind);
pub(crate) enum ErrorKind {
#[error("collection id conversion {0}")]
CollectionIdConversion(String),
#[error("collection path conversion {0}")]
CollectionPathConversion(String),
#[error("contains invalid charactor")]
ContainsInvalidCharacter,
#[error("contains slash")]
Expand Down

0 comments on commit 87de87c

Please sign in to comment.