what2watch/src/watches/mod.rs

88 lines
1.9 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::DbId;
pub mod handlers;
pub mod templates;
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, sqlx::Type,
)]
#[repr(i32)]
pub enum ShowKind {
Movie = 0,
Series = 1,
LimitedSeries = 2,
Short = 3,
Unknown = 4,
Other = 5,
}
impl std::fmt::Display for ShowKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let repr = match self {
Self::Movie => "movie",
Self::Series => "series",
Self::LimitedSeries => "limited series",
Self::Short => "short form",
Self::Other => "other",
Self::Unknown => "unknown",
};
write!(f, "{repr}")
}
}
impl Default for ShowKind {
fn default() -> Self {
Self::Unknown
}
}
impl From<i64> for ShowKind {
fn from(value: i64) -> Self {
match value {
0 => Self::Movie,
1 => Self::Series,
2 => Self::LimitedSeries,
3 => Self::Short,
4 => Self::Unknown,
5 => Self::Other,
_ => Self::Unknown,
}
}
}
#[derive(
Debug,
Default,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
sqlx::FromRow,
)]
pub struct Watch {
pub id: DbId,
pub title: String,
pub kind: ShowKind,
pub metadata_url: Option<String>,
pub length: Option<i64>,
pub release_date: Option<i64>,
pub added_by: DbId, // this shouldn't be exposed to randos in the application
}
impl Watch {
pub fn year(&self) -> Option<String> {
if let Some(year) = self.release_date {
let date = chrono::NaiveDateTime::from_timestamp_opt(year, 0)?;
let year = format!("{}", date.format("%Y"));
Some(year)
} else {
None
}
}
}