1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use crate::{
Node, NodeID, NodeIDOrSlug, ObjectIds, PeopleGroup, SourceTrack, SourceTrackKind, TmdbKind,
TraktKind,
};
use hex::FromHexError;
use serde::{Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};
impl SourceTrackKind {
pub fn letter(&self) -> char {
match self {
SourceTrackKind::Video { .. } => 'v',
SourceTrackKind::Audio { .. } => 'a',
SourceTrackKind::Subtitles => 's',
}
}
}
impl Display for SourceTrack {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let kspec = match &self.kind {
SourceTrackKind::Video {
width, height, fps, ..
} => {
format!("Video: {width}x{height} {}fps ", fps.unwrap_or(0.))
}
SourceTrackKind::Audio {
channels,
sample_rate,
bit_depth,
} => format!(
"Audio: {channels}ch {sample_rate}Hz {}bits ",
bit_depth.unwrap_or(0)
),
SourceTrackKind::Subtitles => "Subtitles: ".to_string(),
};
f.write_fmt(format_args!(
"{} {:?} {} ({})",
kspec, self.name, self.language, self.codec
))
}
}
impl Display for TmdbKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
TmdbKind::Tv => "tv",
TmdbKind::Movie => "movie",
})
}
}
impl TraktKind {
pub fn singular(self) -> &'static str {
match self {
TraktKind::Movie => "movie",
TraktKind::Show => "show",
TraktKind::Season => "season",
TraktKind::Episode => "episode",
TraktKind::Person => "person",
TraktKind::User => "user",
}
}
pub fn plural(self) -> &'static str {
match self {
TraktKind::Movie => "movies",
TraktKind::Show => "shows",
TraktKind::Season => "seasons",
TraktKind::Episode => "episodes",
TraktKind::Person => "people",
TraktKind::User => "users", // //! not used in API
}
}
}
impl Display for TraktKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
TraktKind::Movie => "Movie",
TraktKind::Show => "Show",
TraktKind::Season => "Season",
TraktKind::Episode => "Episode",
TraktKind::Person => "Person",
TraktKind::User => "User",
})
}
}
impl Display for ObjectIds {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(id) = self.trakt {
f.write_fmt(format_args!("trakt={}", id))?;
}
if let Some(_id) = &self.slug {
f.write_str(",slug")?;
}
if let Some(id) = self.tmdb {
f.write_fmt(format_args!(",tmdb={}", id))?;
}
if let Some(_id) = &self.imdb {
f.write_str(",imdb")?;
}
if let Some(_id) = &self.tvdb {
f.write_str(",tvdb")?;
}
if let Some(_id) = &self.omdb {
f.write_str(",omdb")?;
}
Ok(())
}
}
impl Display for PeopleGroup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
PeopleGroup::Cast => "Cast",
PeopleGroup::Writing => "Writing",
PeopleGroup::Directing => "Directing",
PeopleGroup::Art => "Art",
PeopleGroup::Sound => "Sound",
PeopleGroup::Camera => "Camera",
PeopleGroup::Lighting => "Lighting",
PeopleGroup::Crew => "Crew",
PeopleGroup::Editing => "Editing",
PeopleGroup::Production => "Production",
PeopleGroup::Vfx => "Visual Effects",
PeopleGroup::CostumeMakeup => "Costume & Makeup",
PeopleGroup::CreatedBy => "Created by:",
})
}
}
impl FromStr for PeopleGroup {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"Cast" => PeopleGroup::Cast,
"Writing" => PeopleGroup::Writing,
"Directing" => PeopleGroup::Directing,
"Art" => PeopleGroup::Art,
"Sound" => PeopleGroup::Sound,
"Camera" => PeopleGroup::Camera,
"Lighting" => PeopleGroup::Lighting,
"Crew" => PeopleGroup::Crew,
"Editing" => PeopleGroup::Editing,
"Production" => PeopleGroup::Production,
"Visual Effects" => PeopleGroup::Vfx,
"Costume & Makeup" => PeopleGroup::CostumeMakeup,
"Created by:" => PeopleGroup::CreatedBy,
_ => return Err(()),
})
}
}
impl NodeID {
pub fn from_slug(slug: &str) -> Self {
let mut h = blake3::Hasher::new();
h.update(slug.as_bytes());
Self(*h.finalize().as_bytes())
}
#[inline]
pub fn from_node(node: &Node) -> Self {
Self::from_slug(&node.slug)
}
}
impl Node {
#[inline]
pub fn id(&self) -> NodeID {
NodeID::from_node(self)
}
}
impl NodeID {
pub const MIN: NodeID = NodeID([0; 32]);
pub const MAX: NodeID = NodeID([255; 32]);
}
#[cfg(feature = "rocket")]
impl<'a> rocket::request::FromParam<'a> for NodeID {
type Error = FromHexError;
fn from_param(param: &'a str) -> Result<Self, Self::Error> {
if let Some(id) = param.strip_prefix("+") {
let mut k = [0; 32];
hex::decode_to_slice(id, &mut k)?;
Ok(NodeID(k))
} else {
Ok(NodeID::from_slug(param))
}
}
}
#[cfg(feature = "rocket")]
impl rocket::http::uri::fmt::FromUriParam<rocket::http::uri::fmt::Path, NodeID> for NodeID {
type Target = NodeID;
fn from_uri_param(param: NodeID) -> Self::Target {
param
}
}
#[cfg(feature = "rocket")]
impl<'a> rocket::http::uri::fmt::FromUriParam<rocket::http::uri::fmt::Path, &'a String> for NodeID {
type Target = &'a str;
fn from_uri_param(param: &'a String) -> Self::Target {
param.as_str()
}
}
#[cfg(feature = "rocket")]
impl<'a> rocket::http::uri::fmt::FromUriParam<rocket::http::uri::fmt::Path, &'a str> for NodeID {
type Target = &'a str;
fn from_uri_param(param: &'a str) -> Self::Target {
param
}
}
#[cfg(feature = "rocket")]
impl rocket::http::uri::fmt::UriDisplay<rocket::http::uri::fmt::Path> for NodeID {
fn fmt(
&self,
f: &mut rocket::http::uri::fmt::Formatter<'_, rocket::http::uri::fmt::Path>,
) -> std::fmt::Result {
f.write_value(format!("+{}", hex::encode(self.0)))?;
Ok(())
}
}
impl Display for NodeID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("+")?;
f.write_str(&hex::encode(self.0))?;
Ok(())
}
}
impl Display for NodeIDOrSlug {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NodeIDOrSlug::ID(x) => x.fmt(f),
NodeIDOrSlug::Slug(x) => x.fmt(f),
}
}
}
impl From<NodeID> for NodeIDOrSlug {
fn from(value: NodeID) -> Self {
Self::ID(value)
}
}
impl From<String> for NodeIDOrSlug {
fn from(value: String) -> Self {
Self::Slug(value)
}
}
impl Serialize for NodeID {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&hex::encode(self.0))
}
}
impl<'de> Deserialize<'de> for NodeID {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let mut k = [0; 32];
hex::decode_to_slice(String::deserialize(deserializer)?, &mut k).map_err(|_| {
<D::Error as serde::de::Error>::custom(format_args!("nodeid hex invalid"))
})?;
Ok(NodeID(k))
}
}
|