1use std::fmt;
10use std::str::FromStr;
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14#[non_exhaustive]
15pub enum AudioFormat {
16 Aac,
18 Opus,
20 Flac,
22 Mp3,
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
34#[non_exhaustive]
35pub enum VideoFormat {
36 Avc1,
38 H264,
40 Hvc1,
42 H265,
44 Av01,
46 Vp8,
48 Vp9,
50}
51
52#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
59#[non_exhaustive]
60pub enum ContainerFormat {
61 Fmp4,
63 Mkv,
65 Ts,
67 Flv,
69}
70
71#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
73#[non_exhaustive]
74pub enum Format {
75 Audio(AudioFormat),
77 Video(VideoFormat),
79 Container(ContainerFormat),
81}
82
83#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
85#[non_exhaustive]
86pub enum Kind {
87 Audio,
89 Video,
91 Container,
93}
94
95impl Kind {
96 pub fn name(self) -> &'static str {
98 match self {
99 Kind::Audio => "audio",
100 Kind::Video => "video",
101 Kind::Container => "container",
102 }
103 }
104}
105
106impl Format {
107 pub fn kind(self) -> Kind {
109 match self {
110 Format::Audio(_) => Kind::Audio,
111 Format::Video(_) => Kind::Video,
112 Format::Container(_) => Kind::Container,
113 }
114 }
115}
116
117impl FromStr for Format {
118 type Err = crate::Error;
119
120 fn from_str(s: &str) -> Result<Self, Self::Err> {
125 Ok(match s {
126 "aac" => Format::Audio(AudioFormat::Aac),
127 "opus" => Format::Audio(AudioFormat::Opus),
128 "flac" => Format::Audio(AudioFormat::Flac),
129 "mp3" => Format::Audio(AudioFormat::Mp3),
130
131 "avc1" | "avcc" => Format::Video(VideoFormat::Avc1),
132 "h264" => Format::Video(VideoFormat::H264),
133 "hvc1" | "hvcc" => Format::Video(VideoFormat::Hvc1),
134 "h265" | "hevc" => Format::Video(VideoFormat::H265),
135 "av01" | "av1" | "av1c" | "av1C" => Format::Video(VideoFormat::Av01),
136 "vp8" | "vp08" => Format::Video(VideoFormat::Vp8),
137 "vp9" | "vp09" => Format::Video(VideoFormat::Vp9),
138
139 "fmp4" | "cmaf" => Format::Container(ContainerFormat::Fmp4),
140 "mkv" | "webm" | "matroska" => Format::Container(ContainerFormat::Mkv),
141 "ts" | "mpegts" | "mpeg2ts" => Format::Container(ContainerFormat::Ts),
143 "flv" => Format::Container(ContainerFormat::Flv),
144
145 _ => return Err(crate::Error::UnknownFormat(s.to_string())),
146 })
147 }
148}
149
150impl fmt::Display for Format {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 match self {
153 Format::Audio(format) => format.fmt(f),
154 Format::Video(format) => format.fmt(f),
155 Format::Container(format) => format.fmt(f),
156 }
157 }
158}
159
160fn parse_kind(s: &str, wanted: Kind) -> Result<Format, crate::Error> {
165 let format: Format = s.parse()?;
166 if format.kind() != wanted {
167 return Err(crate::Error::WrongKind {
168 format: s.to_string(),
169 actual: format.kind().name(),
170 wanted: wanted.name(),
171 });
172 }
173 Ok(format)
174}
175
176macro_rules! kind_format {
177 ($ty:ident, $kind:ident, $($variant:ident => $name:literal),+ $(,)?) => {
178 impl fmt::Display for $ty {
179 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 f.write_str(match self {
181 $($ty::$variant => $name,)+
182 })
183 }
184 }
185
186 impl FromStr for $ty {
187 type Err = crate::Error;
188
189 fn from_str(s: &str) -> Result<Self, Self::Err> {
192 match parse_kind(s, Kind::$kind)? {
193 Format::$kind(format) => Ok(format),
194 _ => unreachable!(),
196 }
197 }
198 }
199 };
200}
201
202kind_format!(AudioFormat, Audio,
203 Aac => "aac", Opus => "opus", Flac => "flac", Mp3 => "mp3");
204
205kind_format!(VideoFormat, Video,
206 Avc1 => "avc1", H264 => "h264", Hvc1 => "hvc1", H265 => "h265",
207 Av01 => "av01", Vp8 => "vp8", Vp9 => "vp9");
208
209kind_format!(ContainerFormat, Container,
210 Fmp4 => "fmp4", Mkv => "mkv", Ts => "ts", Flv => "flv");
211
212impl AudioFormat {
213 pub fn track_suffix(self) -> &'static str {
215 match self {
216 AudioFormat::Aac => ".aac",
217 AudioFormat::Opus => ".opus",
218 AudioFormat::Flac => ".flac",
219 AudioFormat::Mp3 => ".mp3",
220 }
221 }
222}
223
224impl VideoFormat {
225 pub fn track_suffix(self) -> &'static str {
230 match self {
231 VideoFormat::Avc1 => ".avc1",
232 VideoFormat::H264 => ".avc3",
233 VideoFormat::Hvc1 => ".hvc1",
234 VideoFormat::H265 => ".hev1",
235 VideoFormat::Av01 => ".av01",
236 VideoFormat::Vp8 => ".vp8",
237 VideoFormat::Vp9 => ".vp9",
238 }
239 }
240}
241
242#[cfg(test)]
243mod tests {
244 use super::*;
245
246 #[test]
249 fn aliases_resolve_to_one_canonical_name() {
250 for (aliases, canonical) in [
251 (vec!["h264"], "h264"),
252 (vec!["h265", "hevc"], "h265"),
253 (vec!["avc1", "avcc"], "avc1"),
254 (vec!["hvc1", "hvcc"], "hvc1"),
255 (vec!["av01", "av1", "av1c", "av1C"], "av01"),
256 (vec!["vp8", "vp08"], "vp8"),
257 (vec!["vp9", "vp09"], "vp9"),
258 (vec!["fmp4", "cmaf"], "fmp4"),
259 (vec!["mkv", "webm", "matroska"], "mkv"),
260 (vec!["ts", "mpegts", "mpeg2ts"], "ts"),
261 (vec!["opus"], "opus"),
262 ] {
263 for alias in aliases {
264 let format: Format = alias.parse().unwrap_or_else(|err| panic!("{alias}: {err}"));
265 assert_eq!(format.to_string(), canonical, "{alias} should canonicalize");
266 }
267 }
268 }
269
270 #[test]
273 fn a_format_of_another_kind_names_the_kind_that_takes_it() {
274 let err = "h264".parse::<AudioFormat>().unwrap_err();
275 assert!(
276 matches!(
277 &err,
278 crate::Error::WrongKind {
279 actual: "video",
280 wanted: "audio",
281 ..
282 }
283 ),
284 "got {err:?}"
285 );
286
287 let err = "fmp4".parse::<VideoFormat>().unwrap_err();
288 assert!(
289 matches!(
290 &err,
291 crate::Error::WrongKind {
292 actual: "container",
293 wanted: "video",
294 ..
295 }
296 ),
297 "got {err:?}"
298 );
299
300 let err = "opus".parse::<ContainerFormat>().unwrap_err();
301 assert!(
302 matches!(
303 &err,
304 crate::Error::WrongKind {
305 actual: "audio",
306 wanted: "container",
307 ..
308 }
309 ),
310 "got {err:?}"
311 );
312 }
313
314 #[test]
315 fn an_unrecognized_name_is_unknown_rather_than_wrong_kind() {
316 let err = "prores".parse::<VideoFormat>().unwrap_err();
317 assert!(
318 matches!(&err, crate::Error::UnknownFormat(f) if f == "prores"),
319 "got {err:?}"
320 );
321 }
322}