Migrate to Zed and add proper newtypes

This commit is contained in:
2026-01-04 20:00:13 -07:00
parent dd2cf5c942
commit 994a80c45c
18 changed files with 828 additions and 449 deletions
+10 -14
View File
@@ -1,29 +1,25 @@
[package]
name = "flix-model"
version = "0.0.15"
categories = []
version = "0.0.16"
edition.workspace = true
rust-version.workspace = true
description = "Core types for flix data"
repository = "https://github.com/QuantumShade/flix"
authors.workspace = true
edition.workspace = true
license-file.workspace = true
rust-version.workspace = true
categories = []
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[lints]
workspace = true
[dependencies]
seamantic = { workspace = true }
serde = { workspace = true, features = ["derive", "std"], optional = true }
thiserror = { workspace = true }
[features]
default = []
serde = ["dep:serde"]
[dependencies]
seamantic = { workspace = true }
serde = { workspace = true, optional = true, features = ["std", "derive"] }
thiserror = { workspace = true }
[lints]
workspace = true
+70 -6
View File
@@ -1,12 +1,76 @@
//! This module contains season and episode numbers and related errors
use core::fmt;
use core::ops::RangeInclusive;
use core::str::FromStr;
use std::collections::HashSet;
/// Type alias for representing season numbers
pub type SeasonNumber = u32;
/// Type alias for representing episode numbers
pub type EpisodeNumber = u32;
use seamantic::sea_orm;
/// Newtype for representing season numbers
#[derive(
Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, sea_orm::DeriveValueType,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[repr(transparent)]
pub struct SeasonNumber(u32);
impl SeasonNumber {
/// Create a `SeasonNumber` from an integer
pub fn new(value: u32) -> Self {
Self(value)
}
}
impl fmt::Display for SeasonNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for SeasonNumber {
type Err = <u32 as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
u32::from_str(s).map(Self)
}
}
/// Newtype for representing episode numbers
#[derive(
Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, sea_orm::DeriveValueType,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[repr(transparent)]
pub struct EpisodeNumber(u32);
impl EpisodeNumber {
/// Create an `EpisodeNumber` from an integer
pub fn new(value: u32) -> Self {
Self(value)
}
/// Get the underlying value
pub fn into_inner(self) -> u32 {
self.0
}
}
impl fmt::Display for EpisodeNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for EpisodeNumber {
type Err = <u32 as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
u32::from_str(s).map(Self)
}
}
/// Potential errors when building EpisodeNumbers
#[derive(Debug, thiserror::Error)]
@@ -37,7 +101,7 @@ impl TryFrom<&[EpisodeNumber]> for EpisodeNumbers {
let max = value.iter().copied().max().unwrap_or_default();
let len = value.len();
if usize::try_from(max.saturating_sub(min).saturating_add(1)) != Ok(len) {
if usize::try_from(max.0.saturating_sub(min.0).saturating_add(1)) != Ok(len) {
return Err(Error::Noncontiguous);
}
@@ -56,7 +120,7 @@ impl EpisodeNumbers {
/// Create an [EpisodeNumbers] from a starting number and a count.
/// `count` should be zero for single episodes.
pub fn new(start: EpisodeNumber, count: u8) -> Self {
Self(start..=start.saturating_add(count.into()))
Self(start..=EpisodeNumber(start.0.saturating_add(count.into())))
}
/// Get the range of episodes