1use std::fmt;
6use std::io;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
13pub enum Slot {
14 TwoChannel,
16 Multichannel,
18}
19
20impl Slot {
21 pub const ALL: [Slot; 2] = [Slot::TwoChannel, Slot::Multichannel];
23
24 pub fn name(self) -> &'static str {
26 match self {
27 Slot::TwoChannel => "2ch",
28 Slot::Multichannel => "multichannel",
29 }
30 }
31}
32
33impl fmt::Display for Slot {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 f.write_str(self.name())
36 }
37}
38
39#[derive(Debug, thiserror::Error)]
44#[non_exhaustive]
45pub enum Error {
46 #[error("not a Scarletbook image: no SACDMTOC at LSN 510")]
48 NotScarletbook,
49 #[error("master TOC truncated: the image ends before LSN 519")]
51 MasterTocTruncated,
52 #[error("master TOC version {major}.{minor} is not supported")]
54 #[non_exhaustive]
55 MasterTocVersionUnsupported {
56 major: u8,
58 minor: u8,
60 },
61 #[error("no readable audio area")]
63 NoAudioAreas,
64 #[error("track refused: the area has {channels} channels, only 1 to 6 are supported")]
66 #[non_exhaustive]
67 ChannelCountUnsupported {
68 channels: u8,
70 },
71 #[error("track skipped: its duration is 0 frames")]
73 TrackEmpty,
74 #[error("refused: it ends at frame {end_frame}, beyond the area capacity of {capacity} frames")]
77 #[non_exhaustive]
78 TrackExceedsCapacity {
79 end_frame: u32,
81 capacity: u64,
83 },
84 #[error("no area in slot {0}")]
86 NoSuchArea(Slot),
87 #[error("no track {track} in area {slot}")]
89 #[non_exhaustive]
90 NoSuchTrack {
91 slot: Slot,
93 track: u32,
95 },
96 #[error("frame range [{first}, {end}) is not within the area's timeline [0, {playtime})")]
100 #[non_exhaustive]
101 FrameRangeInvalid {
102 first: u32,
104 end: u32,
106 playtime: u32,
108 },
109 #[error("{count} sectors at LSN {lsn} are not all in the image's {sectors} sectors")]
112 #[non_exhaustive]
113 SectorRangeInvalid {
114 lsn: u64,
116 count: u64,
118 sectors: u64,
120 },
121 #[error("{lanes} DST lanes: must be from 1 to {max}", max = crate::dst::MAX_LANES)]
124 #[non_exhaustive]
125 DstLanesInvalid {
126 lanes: usize,
128 },
129 #[error("invalid argument: {0}")]
131 InvalidArgument(&'static str),
132 #[error("I/O error: {0}")]
134 Io(#[from] io::Error),
135 #[error("cancelled")]
139 Cancelled,
140 #[error("output refused: {0}")]
144 OutputRefused(OutputRefusal),
145 #[error("sector {lsn} is unreadable")]
150 #[non_exhaustive]
151 SectorUnreadable {
152 lsn: u64,
154 },
155}
156
157#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
159#[non_exhaustive]
160pub enum OutputRefusal {
161 #[error(
164 "the area's layout ({channels} channels, configuration {config:#04x}) is unspecified, and the options give no channel type for it"
165 )]
166 #[non_exhaustive]
167 UnspecifiedLayout {
168 channels: u8,
170 config: u8,
172 },
173 #[error("not an edited master: {0}")]
175 NotEditedMaster(EditedMasterCheck),
176}
177
178#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
181#[non_exhaustive]
182pub enum EditedMasterCheck {
183 #[error("it holds only the play range of an area")]
185 Range,
186 #[error("the area's loudspeaker layout is unspecified")]
188 Layout,
189 #[error("the area must have 1 to 255 tracks")]
191 TrackCount,
192 #[error("track {track} is shorter than 75 frames")]
194 #[non_exhaustive]
195 TrackTooShort {
196 track: u8,
198 },
199 #[error("the tracks are out of order or overlap")]
201 TracksNotInOrder,
202 #[error("the program is longer than 255:59:74")]
204 ProgramTooLong,
205}
206
207#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
209#[non_exhaustive]
210pub enum ErrorClass {
211 Rejected,
213 TrackRefused,
215 TrackSkipped,
217 Usage,
219 Io,
221 Cancelled,
223}
224
225impl Error {
226 pub fn code(&self) -> Option<&'static str> {
228 Some(match self {
229 Error::NotScarletbook => "not-scarletbook",
230 Error::MasterTocTruncated => "master-toc-truncated",
231 Error::MasterTocVersionUnsupported { .. } => "master-toc-version-unsupported",
232 Error::NoAudioAreas => "no-audio-areas",
233 Error::ChannelCountUnsupported { .. } => "channel-count-unsupported",
234 Error::TrackEmpty => "track-empty",
235 Error::TrackExceedsCapacity { .. } => "track-exceeds-capacity",
236 Error::NoSuchArea(_)
237 | Error::NoSuchTrack { .. }
238 | Error::FrameRangeInvalid { .. }
239 | Error::SectorRangeInvalid { .. }
240 | Error::DstLanesInvalid { .. }
241 | Error::InvalidArgument(_)
242 | Error::Io(_)
243 | Error::Cancelled
244 | Error::OutputRefused(_)
245 | Error::SectorUnreadable { .. } => {
246 return None;
247 }
248 })
249 }
250
251 pub fn class(&self) -> ErrorClass {
253 match self {
254 Error::NotScarletbook
255 | Error::MasterTocTruncated
256 | Error::MasterTocVersionUnsupported { .. }
257 | Error::NoAudioAreas => ErrorClass::Rejected,
258 Error::ChannelCountUnsupported { .. } | Error::TrackExceedsCapacity { .. } => {
259 ErrorClass::TrackRefused
260 }
261 Error::TrackEmpty => ErrorClass::TrackSkipped,
262 Error::NoSuchArea(_)
263 | Error::NoSuchTrack { .. }
264 | Error::FrameRangeInvalid { .. }
265 | Error::SectorRangeInvalid { .. }
266 | Error::DstLanesInvalid { .. }
267 | Error::InvalidArgument(_)
268 | Error::OutputRefused(_) => ErrorClass::Usage,
269 Error::Io(_) | Error::SectorUnreadable { .. } => ErrorClass::Io,
270 Error::Cancelled => ErrorClass::Cancelled,
271 }
272 }
273}
274
275#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
277#[non_exhaustive]
278pub enum WarningCode {
279 MasterTextMissing,
281 ManufacturerMissing,
283 AreaTocTooLarge,
285 AreaToc1Unreadable,
287 AreaToc2Unreadable,
289 AreaTocCopiesDiffer,
291 AreaTocUnreadable,
293 AreaSignatureInvalid,
295 AreaTrackListMissing,
297 AreaTocMalformed,
299 AreaAudioRangeReversed,
301 TrackTextOffsetInvalid,
303 TrackTextTypeUnknown,
305 TrackPastPlaytime,
307 TrackLsnOutsideArea,
309 AudioSectorMalformed,
311 MasterTocCopiesDiffer,
314 SampleFrequencyUnknown,
316}
317
318impl WarningCode {
319 pub fn code(self) -> &'static str {
321 match self {
322 WarningCode::MasterTextMissing => "master-text-missing",
323 WarningCode::ManufacturerMissing => "manufacturer-missing",
324 WarningCode::AreaTocTooLarge => "area-toc-too-large",
325 WarningCode::AreaToc1Unreadable => "area-toc1-unreadable",
326 WarningCode::AreaToc2Unreadable => "area-toc2-unreadable",
327 WarningCode::AreaTocCopiesDiffer => "area-toc-copies-differ",
328 WarningCode::AreaTocUnreadable => "area-toc-unreadable",
329 WarningCode::AreaSignatureInvalid => "area-signature-invalid",
330 WarningCode::AreaTrackListMissing => "area-track-list-missing",
331 WarningCode::AreaTocMalformed => "area-toc-malformed",
332 WarningCode::AreaAudioRangeReversed => "area-audio-range-reversed",
333 WarningCode::TrackTextOffsetInvalid => "track-text-offset-invalid",
334 WarningCode::TrackTextTypeUnknown => "track-text-type-unknown",
335 WarningCode::TrackPastPlaytime => "track-past-playtime",
336 WarningCode::TrackLsnOutsideArea => "track-lsn-outside-area",
337 WarningCode::AudioSectorMalformed => "audio-sector-malformed",
338 WarningCode::MasterTocCopiesDiffer => "master-toc-copies-differ",
339 WarningCode::SampleFrequencyUnknown => "sample-frequency-unknown",
340 }
341 }
342
343 pub fn skips_area(self) -> bool {
345 matches!(
346 self,
347 WarningCode::AreaTocTooLarge
348 | WarningCode::AreaTocUnreadable
349 | WarningCode::AreaSignatureInvalid
350 | WarningCode::AreaTrackListMissing
351 | WarningCode::AreaTocMalformed
352 | WarningCode::AreaAudioRangeReversed
353 )
354 }
355}
356
357impl fmt::Display for WarningCode {
358 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
359 f.write_str(self.code())
360 }
361}
362
363#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
365#[non_exhaustive]
366pub struct Warning {
367 pub code: WarningCode,
369 pub slot: Option<Slot>,
371 pub track: Option<u8>,
373 pub lsn: Option<u32>,
375}
376
377impl Warning {
378 pub(crate) fn disc(code: WarningCode) -> Self {
379 Warning { code, slot: None, track: None, lsn: None }
380 }
381
382 pub(crate) fn area(code: WarningCode, slot: Slot) -> Self {
383 Warning { code, slot: Some(slot), track: None, lsn: None }
384 }
385
386 pub(crate) fn track(code: WarningCode, slot: Slot, track: u8) -> Self {
387 Warning { code, slot: Some(slot), track: Some(track), lsn: None }
388 }
389}
390
391impl fmt::Display for Warning {
392 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
393 f.write_str(self.code.code())?;
394 if let Some(slot) = self.slot {
395 write!(f, " (area {slot}")?;
396 if let Some(track) = self.track {
397 write!(f, ", track {track}")?;
398 }
399 if let Some(lsn) = self.lsn {
400 write!(f, ", LSN {lsn}")?;
401 }
402 f.write_str(")")?;
403 }
404 Ok(())
405 }
406}