pub struct FirestoreDocumentId(/* private fields */);Expand description
A Firestore document ID that has been checked against Firestore’s ID rules.
Construct one with FirestoreDocumentId::new at the point an ID enters your system, then
pass it (by value or by reference) anywhere this crate accepts a document ID, or store it in
your own structs.
use firestore::FirestoreDocumentId;
let id = FirestoreDocumentId::new("user-42").expect("valid document id");
assert_eq!(id.as_str(), "user-42");
let err = FirestoreDocumentId::new("a/b").unwrap_err();
assert!(err.to_string().contains("document_id"));
let err = FirestoreDocumentId::new("").unwrap_err();
assert!(err.to_string().contains("document_id"));It implements AsRef<str> (and &FirestoreDocumentId does too, via the standard library’s
blanket impl), so it drops into every document-ID parameter in this crate’s fluent API without
a signature change:
use firestore::{FirestoreDb, FirestoreDocumentId, FirestoreResult};
let db = FirestoreDb::new("my-project").await?;
let id = FirestoreDocumentId::new("user-42")?;
let doc = db.fluent().select().by_id_in("users").one(&id).await?;A field typed as FirestoreDocumentId carries the same proof, and validates the same way
when deserialized — the usual arrival point for an untrusted ID:
use firestore::FirestoreDocumentId;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
struct CreateSession {
user_id: FirestoreDocumentId,
}
let session = CreateSession {
user_id: FirestoreDocumentId::new("user-42")?,
};
assert_eq!(session.user_id.as_str(), "user-42");This departs from FirestoreReference, which derives
Deserialize unconditionally and only validates lazily, on parse(). If a field must
round-trip an ID without validating it (for example, one already known to be safe, or logged
verbatim for diagnostics), keep it typed as String — that is the documented escape hatch,
not a gap in this type.
Neither this type nor FirestoreCollectionId implements rvstruct::ValueStruct. That
trait requires an into_value(self) -> String alongside value(), and a consuming conversion
back to a bare String is exactly what these types exist to discourage: it launders a
validated ID back into a value that can be mutated or concatenated while still looking like it
came from a checked source. For the same reason there is no into_string() and no
From<FirestoreDocumentId> for String — only the read-only value() and
as_str() accessors. An owned String is still reachable, deliberately
explicitly, via id.as_str().to_string():
use firestore::FirestoreDocumentId;
let id = FirestoreDocumentId::new("user-42").unwrap();
let _owned: String = id.into();Firestore also exposes Datastore-mode entities with integer IDs as documents literally named
__id7__, and such documents are read today through the same path-building code these types
back. Validation here therefore does not reject the reserved __*__ namespace: doing so would
turn an existing, working read into a client-side error for a case the server only rejects on
write. "__id7__" and "__name__" are accepted by FirestoreDocumentId::new and
FirestoreCollectionId::new on purpose.
Implementations§
Source§impl FirestoreDocumentId
impl FirestoreDocumentId
Sourcepub fn new<S: Into<String>>(id: S) -> FirestoreResult<Self>
pub fn new<S: Into<String>>(id: S) -> FirestoreResult<Self>
Validates id and wraps it.
§Errors
Returns FirestoreError::InvalidParametersError if id is empty, longer than 1500
bytes, contains /, or is exactly "." or "..".
Sourcepub fn validate_str(id: &str) -> FirestoreResult<()>
pub fn validate_str(id: &str) -> FirestoreResult<()>
Validates id without allocating or constructing a value.
Useful to check a borrowed &str before deciding whether to take ownership of it.
§Errors
Same conditions as FirestoreDocumentId::new.
Sourcepub fn validate(&self) -> FirestoreResult<()>
pub fn validate(&self) -> FirestoreResult<()>
Re-validates an already-constructed ID.
A valid FirestoreDocumentId cannot become invalid after construction, so this always
succeeds; it exists to mirror FirestoreListenerTarget::validate
for callers that validate every ID-like value the same way.
§Errors
Never fails; returns FirestoreResult<()> for symmetry with FirestoreDocumentId::validate_str.
Sourcepub const fn from_static(id: &'static str) -> Self
pub const fn from_static(id: &'static str) -> Self
Validates id at compile time and wraps it without allocating, for a name known up front.
Declare validated names the way you would declare const NAME: &str = "..." today:
use firestore::FirestoreDocumentId;
const WELCOME_DOC: FirestoreDocumentId = FirestoreDocumentId::from_static("welcome");
assert_eq!(WELCOME_DOC.as_str(), "welcome");Used in a const or static item, an invalid literal is a compile error: evaluating the
item panics, which rustc reports at the definition site rather than letting the invalid
value reach a binary. Called from inside a function body, where the result is only a
runtime value, the same invalid literal panics instead, exactly like an out-of-bounds
const fn array index would.
use firestore::FirestoreDocumentId;
const BAD: FirestoreDocumentId = FirestoreDocumentId::from_static("a/b");§Panics
Panics if id is empty, longer than 1500 bytes, contains /, or is exactly "." or
"..", and the call is not evaluated at compile time.
Sourcepub fn value(&self) -> &str
pub fn value(&self) -> &str
Returns the wrapped ID.
Equivalent to as_str; kept as a separate method so a validated ID reads
the same as rvstruct::ValueStruct::value elsewhere in this crate’s API. There is no
consuming equivalent that returns an owned String (see the FirestoreDocumentId type
docs for why). Use id.as_str().to_string() when an owned copy is genuinely needed.
Trait Implementations§
Source§impl AsRef<str> for FirestoreDocumentId
impl AsRef<str> for FirestoreDocumentId
Source§impl Borrow<str> for FirestoreDocumentId
impl Borrow<str> for FirestoreDocumentId
Source§impl Clone for FirestoreDocumentId
impl Clone for FirestoreDocumentId
Source§impl Debug for FirestoreDocumentId
impl Debug for FirestoreDocumentId
Source§impl<'de> Deserialize<'de> for FirestoreDocumentId
impl<'de> Deserialize<'de> for FirestoreDocumentId
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for FirestoreDocumentId
impl Display for FirestoreDocumentId
impl Eq for FirestoreDocumentId
Source§impl FromStr for FirestoreDocumentId
impl FromStr for FirestoreDocumentId
Source§impl Hash for FirestoreDocumentId
impl Hash for FirestoreDocumentId
Source§impl Ord for FirestoreDocumentId
impl Ord for FirestoreDocumentId
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for FirestoreDocumentId
impl PartialEq for FirestoreDocumentId
Source§impl PartialEq<&str> for FirestoreDocumentId
impl PartialEq<&str> for FirestoreDocumentId
Source§impl PartialEq<FirestoreDocumentId> for str
impl PartialEq<FirestoreDocumentId> for str
Source§impl PartialEq<FirestoreDocumentId> for &str
impl PartialEq<FirestoreDocumentId> for &str
Source§impl PartialEq<FirestoreDocumentId> for String
impl PartialEq<FirestoreDocumentId> for String
Source§impl PartialEq<String> for FirestoreDocumentId
impl PartialEq<String> for FirestoreDocumentId
Source§impl PartialEq<str> for FirestoreDocumentId
impl PartialEq<str> for FirestoreDocumentId
Source§impl PartialOrd for FirestoreDocumentId
impl PartialOrd for FirestoreDocumentId
Source§impl Serialize for FirestoreDocumentId
impl Serialize for FirestoreDocumentId
impl StructuralPartialEq for FirestoreDocumentId
Source§impl TryFrom<&str> for FirestoreDocumentId
impl TryFrom<&str> for FirestoreDocumentId
Auto Trait Implementations§
impl Freeze for FirestoreDocumentId
impl RefUnwindSafe for FirestoreDocumentId
impl Send for FirestoreDocumentId
impl Sync for FirestoreDocumentId
impl Unpin for FirestoreDocumentId
impl UnsafeUnpin for FirestoreDocumentId
impl UnwindSafe for FirestoreDocumentId
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request