Struct rspotify_model::idtypes::TrackId
source · #[repr(transparent)]pub struct TrackId<'a>(_);
Expand description
ID of type Type::Track
. The validity of its characters is defined by the closure |id| id.chars().all(|ch| ch.is_ascii_alphanumeric())
.
Refer to the module-level docs for more information.
Implementations§
source§impl<'a> TrackId<'a>
impl<'a> TrackId<'a>
sourcepub fn id_is_valid(id: &str) -> bool
pub fn id_is_valid(id: &str) -> bool
Only returns true
in case the given string is valid
according to that specific ID (e.g., some may require
alphanumeric characters only).
sourcepub unsafe fn from_id_unchecked<S>(id: S) -> Selfwhere
S: Into<Cow<'a, str>>,
pub unsafe fn from_id_unchecked<S>(id: S) -> Selfwhere S: Into<Cow<'a, str>>,
Initialize the ID without checking its validity.
Safety
The string passed to this method must be made out of valid characters only; otherwise undefined behaviour may occur.
sourcepub fn from_id<S>(id: S) -> Result<Self, IdError>where
S: Into<Cow<'a, str>>,
pub fn from_id<S>(id: S) -> Result<Self, IdError>where S: Into<Cow<'a, str>>,
Parse Spotify ID from string slice.
A valid Spotify object id must be a non-empty string with valid characters.
Errors
IdError::InvalidId
- ifid
contains invalid characters.
sourcepub fn from_uri(uri: &'a str) -> Result<Self, IdError>
pub fn from_uri(uri: &'a str) -> Result<Self, IdError>
Parse Spotify URI from string slice
Spotify URI must be in one of the following formats:
spotify:{type}:{id}
or spotify/{type}/{id}
.
Where {type}
is one of artist
, album
, track
,
playlist
, user
, show
, or episode
, and {id}
is a
non-empty valid string.
Examples: spotify:album:6IcGNaXFRf5Y1jc7QsE9O2
,
spotify/track/4y4VO05kYgUTo2bzbox1an
.
Errors
IdError::InvalidPrefix
- ifuri
is not started withspotify:
orspotify/
,IdError::InvalidType
- if type part of anuri
is not a valid Spotify typeT
,IdError::InvalidId
- if id part of anuri
is not a valid id,IdError::InvalidFormat
- if it can’t be splitted into type and id parts.
Implementation details
Unlike Self::from_id
, this method takes a &str
rather
than an Into<Cow<str>>
. This is because the inner Cow
in
the ID would reference a slice from the given &str
(i.e.,
taking the ID out of the URI). The parameter wouldn’t live
long enough when using Into<Cow<str>>
, so the only
sensible choice is to just use a &str
.
sourcepub fn from_id_or_uri(id_or_uri: &'a str) -> Result<Self, IdError>
pub fn from_id_or_uri(id_or_uri: &'a str) -> Result<Self, IdError>
Parse Spotify ID or URI from string slice
Spotify URI must be in one of the following formats:
spotify:{type}:{id}
or spotify/{type}/{id}
.
Where {type}
is one of artist
, album
, track
,
playlist
, user
, show
, or episode
, and {id}
is a
non-empty valid string. The URI must be match with the ID’s
type (Id::TYPE
), otherwise IdError::InvalidType
error is
returned.
Examples: spotify:album:6IcGNaXFRf5Y1jc7QsE9O2
,
spotify/track/4y4VO05kYgUTo2bzbox1an
.
If input string is not a valid Spotify URI (it’s not started
with spotify:
or spotify/
), it must be a valid Spotify
object ID, i.e. a non-empty valid string.
Errors
IdError::InvalidType
- ifid_or_uri
is an URI, and it’s type part is not equal toT
,IdError::InvalidId
- either ifid_or_uri
is an URI with invalid id part, or it’s an invalid id (id is invalid if it contains valid characters),IdError::InvalidFormat
- ifid_or_uri
is an URI, and it can’t be split into type and id parts.
Implementation details
Unlike Self::from_id
, this method takes a &str
rather
than an Into<Cow<str>>
. This is because the inner Cow
in
the ID would reference a slice from the given &str
(i.e.,
taking the ID out of the URI). The parameter wouldn’t live
long enough when using Into<Cow<str>>
, so the only
sensible choice is to just use a &str
.
sourcepub fn as_ref(&'a self) -> Self
pub fn as_ref(&'a self) -> Self
This creates an ID with the underlying &str
variant from a
reference. Useful to use an ID multiple times without having
to clone it.
sourcepub fn into_static(self) -> TrackId<'static>
pub fn into_static(self) -> TrackId<'static>
An ID is a Cow
after all, so this will switch to the its
owned version, which has a 'static
lifetime.
sourcepub fn clone_static(&self) -> TrackId<'static>
pub fn clone_static(&self) -> TrackId<'static>
Similar to Self::into_static
, but without consuming the
original ID.
Trait Implementations§
source§impl Borrow<str> for TrackId<'_>
impl Borrow<str> for TrackId<'_>
Id
s may be borrowed as str
the same way Box<T>
may be
borrowed as T
or String
as str