You've already forked flix
Update dependencies and lints
This commit is contained in:
+63
-24
@@ -1,4 +1,6 @@
|
||||
//! Typed TMDB IDs
|
||||
//! Typed TMDB IDs.
|
||||
|
||||
#![expect(clippy::module_name_repetitions, reason = "ID types end with Id")]
|
||||
|
||||
use core::cmp::Ordering;
|
||||
use core::fmt;
|
||||
@@ -10,21 +12,25 @@ use sea_orm::sea_query::{ArrayType, Nullable, ValueType, ValueTypeErr};
|
||||
#[cfg(feature = "sea-orm")]
|
||||
use sea_orm::{ColIdx, ColumnType, DbErr, QueryResult, TryFromU64, TryGetError, TryGetable, Value};
|
||||
|
||||
/// The internal representation used by TMDB
|
||||
/// The internal representation used by TMDB.
|
||||
pub type TmdbRepr = u32;
|
||||
|
||||
/// An opaque type representing a TMDB ID
|
||||
/// An opaque type representing a TMDB ID.
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[serde(transparent)]
|
||||
#[repr(transparent)]
|
||||
pub struct Id<T> {
|
||||
/// Internal ID representation.
|
||||
id: TmdbRepr,
|
||||
/// Marker to indicate an invariant T.
|
||||
#[serde(skip_serializing, default)]
|
||||
_phantom: PhantomData<T>,
|
||||
_phantom: PhantomData<fn(T) -> T>,
|
||||
}
|
||||
|
||||
// Manual implementation since `T: Clone` is not required
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> Clone for Id<T> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
@@ -34,31 +40,40 @@ impl<T> Clone for Id<T> {
|
||||
impl<T> Copy for Id<T> {}
|
||||
|
||||
// Manual implementation since `T: PartialEq` is not required
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> PartialEq for Id<T> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
// Manual implementation since `T: Eq` is not required
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> Eq for Id<T> {}
|
||||
|
||||
// Manual implementation since `T: PartialOrd` is not required
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> PartialOrd for Id<T> {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
// Manual implementation since `T: Ord` is not required
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> Ord for Id<T> {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.id.cmp(&other.id)
|
||||
}
|
||||
}
|
||||
|
||||
// Manual implementation since `T: Hash` is not required
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> Hash for Id<T> {
|
||||
#[inline]
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.id.hash(state);
|
||||
}
|
||||
@@ -66,6 +81,8 @@ impl<T> Hash for Id<T> {
|
||||
|
||||
impl<T> Id<T> {
|
||||
/// Allows the conversion from a raw value to [Id], though the use is discouraged.
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn from_raw(raw: TmdbRepr) -> Self {
|
||||
Self {
|
||||
id: raw,
|
||||
@@ -74,13 +91,16 @@ impl<T> Id<T> {
|
||||
}
|
||||
|
||||
/// Allows extracting the raw value, though the use is discouraged.
|
||||
pub fn into_raw(self) -> TmdbRepr {
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn into_raw(self) -> TmdbRepr {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for Id<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Id")
|
||||
.field("T", &core::any::type_name::<T>())
|
||||
.field("id", &self.id)
|
||||
@@ -89,7 +109,9 @@ impl<T> fmt::Debug for Id<T> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "sea-orm")]
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> ValueType for Id<T> {
|
||||
#[inline]
|
||||
fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
|
||||
<TmdbRepr as ValueType>::try_from(v).map(|id| Self {
|
||||
id,
|
||||
@@ -97,14 +119,17 @@ impl<T> ValueType for Id<T> {
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn type_name() -> String {
|
||||
format!("Id<{}>", &core::any::type_name::<T>())
|
||||
format!("Id<{}>", core::any::type_name::<T>())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn array_type() -> ArrayType {
|
||||
TmdbRepr::array_type()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn column_type() -> ColumnType {
|
||||
TmdbRepr::column_type()
|
||||
}
|
||||
@@ -112,13 +137,16 @@ impl<T> ValueType for Id<T> {
|
||||
|
||||
#[cfg(feature = "sea-orm")]
|
||||
impl<T> From<Id<T>> for Value {
|
||||
#[inline]
|
||||
fn from(value: Id<T>) -> Self {
|
||||
value.id.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "sea-orm")]
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl<T> TryGetable for Id<T> {
|
||||
#[inline]
|
||||
fn try_get_by<I: ColIdx>(res: &QueryResult, index: I) -> Result<Self, TryGetError> {
|
||||
TmdbRepr::try_get_by(res, index).map(|id| Self {
|
||||
id,
|
||||
@@ -129,6 +157,7 @@ impl<T> TryGetable for Id<T> {
|
||||
|
||||
#[cfg(feature = "sea-orm")]
|
||||
impl<T> TryFromU64 for Id<T> {
|
||||
#[inline]
|
||||
fn try_from_u64(n: u64) -> Result<Self, DbErr> {
|
||||
TmdbRepr::try_from_u64(n).map(|id| Self {
|
||||
id,
|
||||
@@ -139,6 +168,7 @@ impl<T> TryFromU64 for Id<T> {
|
||||
|
||||
#[cfg(feature = "sea-orm")]
|
||||
impl<T> Nullable for Id<T> {
|
||||
#[inline]
|
||||
fn null() -> Value {
|
||||
TmdbRepr::null()
|
||||
}
|
||||
@@ -146,34 +176,34 @@ impl<T> Nullable for Id<T> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
#[cfg(feature = "sea-orm")]
|
||||
fn test_sea_orm() {
|
||||
#[expect(dead_code, reason = "structs test derive macros")]
|
||||
mod test_seaorm {
|
||||
use sea_orm::{
|
||||
ActiveModelBehavior, DeriveEntityModel, DerivePrimaryKey, DeriveRelation, EntityTrait,
|
||||
EnumIter, PrimaryKeyTrait,
|
||||
};
|
||||
|
||||
use super::Id;
|
||||
use super::super::Id;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "ids")]
|
||||
pub struct Model {
|
||||
#[expect(clippy::use_self, reason = "derive macros cause Self to be invalid")]
|
||||
struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
id: Id<Model>,
|
||||
nullable: Option<Id<Model>>,
|
||||
}
|
||||
|
||||
#[expect(clippy::missing_trait_methods, reason = "accept default methods")]
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
enum Relation {}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serde() {
|
||||
fn serde() {
|
||||
use super::Id;
|
||||
|
||||
let id: Id<()> = Id::from_raw(1234);
|
||||
@@ -181,61 +211,70 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Type alias for the raw ID representation
|
||||
pub use self::TmdbRepr as RawId;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[non_exhaustive]
|
||||
pub enum Collection {}
|
||||
/// Type alias for a collection ID
|
||||
/// Type alias for a collection ID.
|
||||
pub type CollectionId = Id<Collection>;
|
||||
|
||||
impl From<CollectionId> for flix_model::id::CollectionId {
|
||||
#[inline]
|
||||
fn from(value: CollectionId) -> Self {
|
||||
Self::from_raw(value.into_raw().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<flix_model::id::CollectionId> for CollectionId {
|
||||
type Error = <RawId as TryFrom<flix_model::id::RawId>>::Error;
|
||||
type Error = <TmdbRepr as TryFrom<flix_model::id::RawId>>::Error;
|
||||
|
||||
#[inline]
|
||||
fn try_from(value: flix_model::id::CollectionId) -> Result<Self, Self::Error> {
|
||||
value.into_raw().try_into().map(Self::from_raw)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[non_exhaustive]
|
||||
pub enum Movie {}
|
||||
/// Type alias for a movie ID
|
||||
/// Type alias for a movie ID.
|
||||
pub type MovieId = Id<Movie>;
|
||||
|
||||
impl From<MovieId> for flix_model::id::MovieId {
|
||||
#[inline]
|
||||
fn from(value: MovieId) -> Self {
|
||||
Self::from_raw(value.into_raw().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<flix_model::id::MovieId> for MovieId {
|
||||
type Error = <RawId as TryFrom<flix_model::id::RawId>>::Error;
|
||||
type Error = <TmdbRepr as TryFrom<flix_model::id::RawId>>::Error;
|
||||
|
||||
#[inline]
|
||||
fn try_from(value: flix_model::id::MovieId) -> Result<Self, Self::Error> {
|
||||
value.into_raw().try_into().map(Self::from_raw)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[non_exhaustive]
|
||||
pub enum Show {}
|
||||
/// Type alias for a show ID
|
||||
/// Type alias for a show ID.
|
||||
pub type ShowId = Id<Show>;
|
||||
|
||||
impl From<ShowId> for flix_model::id::ShowId {
|
||||
#[inline]
|
||||
fn from(value: ShowId) -> Self {
|
||||
Self::from_raw(value.into_raw().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<flix_model::id::ShowId> for ShowId {
|
||||
type Error = <RawId as TryFrom<flix_model::id::RawId>>::Error;
|
||||
type Error = <TmdbRepr as TryFrom<flix_model::id::RawId>>::Error;
|
||||
|
||||
#[inline]
|
||||
fn try_from(value: flix_model::id::ShowId) -> Result<Self, Self::Error> {
|
||||
value.into_raw().try_into().map(Self::from_raw)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user