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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
/*!
# CDTOC
[](https://docs.rs/cdtoc/)
[](https://github.com/Blobfolio/cdtoc/blob/master/CHANGELOG.md)<br>
[](https://crates.io/crates/cdtoc)
[](https://github.com/Blobfolio/cdtoc/actions)
[](https://deps.rs/repo/github/blobfolio/cdtoc)<br>
[](https://en.wikipedia.org/wiki/WTFPL)
[](https://github.com/Blobfolio/cdtoc/issues)
CDTOC is a simple Rust library for parsing and working with audio CD tables of contents, namely in the form of [CDTOC-style](https://forum.dbpoweramp.com/showthread.php?16705-FLAC-amp-Ogg-Vorbis-Storage-of-CDTOC&s=3ca0c65ee58fc45489103bb1c39bfac0&p=76686&viewfull=1#post76686) metadata values.
By default it can also generate disc IDs for services like [AccurateRip](http://accuraterip.com/), [CDDB](https://en.wikipedia.org/wiki/CDDB), [CUETools Database](http://cue.tools/wiki/CUETools_Database), and [MusicBrainz](https://musicbrainz.org/), but you can disable the corresponding crate feature(s) — `accuraterip`, `cddb`, `ctdb`, and `musicbrainz` respectively — to shrink the dependency tree if you don't need that functionality.
## Examples
```
use cdtoc::Toc;
// From a CDTOC string.
let toc1 = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
// From the raw parts.
let toc2 = Toc::from_parts(
vec![150, 11563, 25174, 45863],
None,
55370,
).unwrap();
// Either way gets you to the same place.
assert_eq!(toc1, toc2);
// You can also get a CDTOC-style string back at any time:
assert_eq!(toc1.to_string(), "4+96+2D2B+6256+B327+D84A");
```
## De/Serialization
The optional `serde` crate feature can be enabled to expose de/serialization implementations for this library's types:
| Type | Format | Notes |
| ---- | ------ | ----- |
| [`AccurateRip`] | `String` | |
| [`Cddb`] | `String` | |
| [`Duration`] | `u64` | |
| [`ShaB64`] | `String` | MusicBrainz and CTDB IDs. |
| [`Toc`] | `String` | |
| [`Track`] | `Map` | |
| [`TrackPosition`] | `String` | |
## Installation
Add `cdtoc` to your `dependencies` in `Cargo.toml`, like:
```ignore,toml
[dependencies]
cdtoc = "0.3.*"
```
The disc ID helpers require additional dependencies, so if you aren't using them, be sure to disable the default features (adding back any you _do_ want) to skip the overhead.
```ignore,toml
[dependencies.cdtoc]
version = "0.3.*"
default-features = false
```
*/
#![deny(unsafe_code)]
#![warn(
clippy::filetype_is_file,
clippy::integer_division,
clippy::needless_borrow,
clippy::nursery,
clippy::pedantic,
clippy::perf,
clippy::suboptimal_flops,
clippy::unneeded_field_pattern,
macro_use_extern_crate,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
non_ascii_idents,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unused_crate_dependencies,
unused_extern_crates,
unused_import_braces,
)]
#![allow(
clippy::doc_markdown,
clippy::module_name_repetitions,
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
mod error;
mod time;
mod track;
#[cfg(feature = "accuraterip")] mod accuraterip;
#[cfg(feature = "cddb")] mod cddb;
#[cfg(feature = "ctdb")] mod ctdb;
#[cfg(feature = "musicbrainz")] mod musicbrainz;
#[cfg(feature = "serde")] mod serde;
#[cfg(feature = "sha1")] mod shab64;
pub use error::TocError;
pub use time::Duration;
pub use track::{
Track,
Tracks,
TrackPosition,
};
#[cfg(feature = "accuraterip")] pub use accuraterip::AccurateRip;
#[cfg(feature = "cddb")] pub use cddb::Cddb;
#[cfg(feature = "sha1")] pub use shab64::ShaB64;
use dactyl::traits::HexToUnsigned;
use std::fmt;
use trimothy::TrimSlice;
#[cfg(any(feature = "musicbrainz", feature = "ctdb"))]
/// # Lotsa Zeroes.
///
/// MusicBrainz and CTDB take a sha1 hash of 100 hex-encoded tracks, most of
/// which, most of the time, are just zero-padding. Slicing what we need out of
/// a prebuilt static is much faster than pushing zeroes on-the-fly.
static ZEROES: [u8; 792] = [b'0'; 792];
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
/// # CDTOC.
///
/// This struct holds a CD's parsed table of contents.
///
/// You can initialize it using a [CDTOC-style](https://forum.dbpoweramp.com/showthread.php?16705-FLAC-amp-Ogg-Vorbis-Storage-of-CDTOC&s=3ca0c65ee58fc45489103bb1c39bfac0&p=76686&viewfull=1#post76686) metadata value
/// via [`Toc::from_cdtoc`] or manually with [`Toc::from_parts`].
///
/// Once parsed, you can obtain things like the [number of audio tracks](Toc::audio_len),
/// their [sector positions](Toc::audio_sectors), information about the [session(s)](Toc::kind)
/// and so on.
///
/// Many online databases derive their unique disc IDs using tables of content
/// too. [`Toc`] can give you the following, provided the corresponding crate
/// feature(s) are enabled:
///
/// | Service | Feature | Method |
/// | ------- | ------- | ------ |
/// | [AccurateRip](http://accuraterip.com/) | `accuraterip` | [`Toc::accuraterip_id`] |
/// | [CDDB](https://en.wikipedia.org/wiki/CDDB) | `cddb` | [`Toc::cddb_id`] |
/// | [CUETools Database](http://cue.tools/wiki/CUETools_Database) | `ctdb` | [`Toc::ctdb_id`] |
/// | [MusicBrainz](https://musicbrainz.org/) | `musicbrainz` | [`Toc::musicbrainz_id`] |
///
/// If you don't care about any of those, import this crate with
/// `default-features = false` to skip the overhead.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// // From a CDTOC string.
/// let toc1 = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
///
/// // From the raw parts.
/// let toc2 = Toc::from_parts(
/// vec![150, 11563, 25174, 45863],
/// None,
/// 55370,
/// ).unwrap();
///
/// // Either way gets you to the same place.
/// assert_eq!(toc1, toc2);
///
/// // You can also get a CDTOC-style string back at any time:
/// assert_eq!(toc1.to_string(), "4+96+2D2B+6256+B327+D84A");
/// ```
pub struct Toc {
kind: TocKind,
audio: Vec<u32>,
data: u32,
leadout: u32,
}
impl fmt::Display for Toc {
#[allow(clippy::cast_possible_truncation)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use trimothy::TrimSliceMatches;
let mut out = Vec::with_capacity(128);
let mut buf = [b'0'; 8];
// Audio track count.
let audio_len = self.audio.len() as u8;
faster_hex::hex_encode_fallback(&[audio_len], &mut buf[..2]);
if 16 <= audio_len { out.push(buf[0]); }
out.push(buf[1]);
macro_rules! push {
($v:expr) => (
faster_hex::hex_encode_fallback($v.to_be_bytes().as_slice(), &mut buf);
out.push(b'+');
out.extend_from_slice(buf.trim_start_matches(|b| b == b'0'));
);
}
// The sectors.
for v in &self.audio { push!(v); }
// And finally some combination of data and leadout.
match self.kind {
TocKind::Audio => { push!(self.leadout); },
TocKind::CDExtra => {
push!(self.data);
push!(self.leadout);
},
TocKind::DataFirst => {
push!(self.leadout);
// Handle this manually since there's the weird X marker.
faster_hex::hex_encode_fallback(self.data.to_be_bytes().as_slice(), &mut buf);
out.push(b'+');
out.push(b'X');
out.extend_from_slice(buf.trim_start_matches(|b| b == b'0'));
},
}
out.make_ascii_uppercase();
std::str::from_utf8(&out)
.map_err(|_| fmt::Error)
.and_then(|s| f.write_str(s))
}
}
impl Toc {
/// # From CDTOC Metadata Tag.
///
/// Instantiate a new [`Toc`] from a CDTOC metadata tag value, of the
/// format described [here](https://forum.dbpoweramp.com/showthread.php?16705-FLAC-amp-Ogg-Vorbis-Storage-of-CDTOC&s=3ca0c65ee58fc45489103bb1c39bfac0&p=76686&viewfull=1#post76686).
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// ```
///
/// ## Errors
///
/// This will return an error if the tag value is improperly formatted, the
/// audio track count is outside `1..=99`, there are too many or too few
/// sectors, the leadin is less than `150`, or the sectors are ordered
/// incorrectly.
pub fn from_cdtoc<S>(src: S) -> Result<Self, TocError>
where S: AsRef<str> {
let (audio, data, leadout) = parse_cdtoc_metadata(src.as_ref().as_bytes())?;
Self::from_parts(audio, data, leadout)
}
/// # From Durations.
///
/// This will attempt to create an audio-only [`Toc`] from the track
/// durations. (Needless to say, this will only work if all tracks are
/// present and in the right order!)
///
/// If you happen to know the disc's true leadin offset you can specify it,
/// otherwise the "industry default" value of `150` will be assumed.
///
/// To create a mixed-mode [`Toc`] from scratch, use [`Toc::from_parts`]
/// instead so you can specify the location of the data session.
///
/// ## Examples
///
/// ```
/// use cdtoc::{Toc, Duration};
///
/// let toc = Toc::from_durations(
/// [
/// Duration::from(46650_u64),
/// Duration::from(41702_u64),
/// Duration::from(30295_u64),
/// Duration::from(37700_u64),
/// Duration::from(40050_u64),
/// Duration::from(53985_u64),
/// Duration::from(37163_u64),
/// Duration::from(59902_u64),
/// ],
/// None,
/// ).unwrap();
/// assert_eq!(
/// toc.to_string(),
/// "8+96+B6D0+159B6+1D00D+26351+2FFC3+3D2A4+463CF+54DCD",
/// );
/// ```
///
/// ## Errors
///
/// This will return an error if the track count is outside `1..=99`, the
/// leadin is less than 150, or the sectors overflow `u32`.
pub fn from_durations<I>(src: I, leadin: Option<u32>) -> Result<Self, TocError>
where I: IntoIterator<Item=Duration> {
let mut last: u32 = leadin.unwrap_or(150);
let mut audio: Vec<u32> = vec![last];
for d in src {
let next = u32::try_from(d.sectors())
.ok()
.and_then(|n| last.checked_add(n))
.ok_or(TocError::SectorSize)?;
audio.push(next);
last = next;
}
let leadout = audio.remove(audio.len() - 1);
Self::from_parts(audio, None, leadout)
}
/// # From Parts.
///
/// Instantiate a new [`Toc`] by manually specifying the (starting) sectors
/// for each audio track, data track (if any), and the leadout.
///
/// If a data track is supplied, it must fall between the last audio track
/// and leadout, or come before either.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_parts(
/// vec![150, 11563, 25174, 45863],
/// None,
/// 55370,
/// ).unwrap();
///
/// assert_eq!(toc.to_string(), "4+96+2D2B+6256+B327+D84A");
///
/// // Sanity matters; the leadin, for example, can't be less than 150.
/// assert!(Toc::from_parts(
/// vec![0, 10525],
/// None,
/// 15000,
/// ).is_err());
/// ```
///
/// ## Errors
///
/// This will return an error if the audio track count is outside `1..=99`,
/// the leadin is less than `150`, or the sectors are in the wrong order.
pub fn from_parts(audio: Vec<u32>, data: Option<u32>, leadout: u32)
-> Result<Self, TocError> {
// Check length.
let audio_len = audio.len();
if 0 == audio_len { return Err(TocError::NoAudio); }
if 99 < audio_len { return Err(TocError::TrackCount); }
// Audio leadin must be at least 150.
if audio[0] < 150 { return Err(TocError::LeadinSize); }
// Audio is out of order?
if
(1 < audio_len && audio.windows(2).any(|pair| pair[1] <= pair[0])) ||
leadout <= audio[audio_len - 1]
{
return Err(TocError::SectorOrder);
}
// Figure out the kind and validate the data sector.
let kind =
if let Some(d) = data {
if d < audio[0] { TocKind::DataFirst }
else if audio[audio_len - 1] < d && d < leadout {
TocKind::CDExtra
}
else { return Err(TocError::SectorOrder); }
}
else { TocKind::Audio };
Ok(Self { kind, audio, data: data.unwrap_or_default(), leadout })
}
/// # Set Audio Leadin.
///
/// Set the audio leadin, nudging all entries up or down accordingly (
/// including data and leadout).
///
/// Note: this method cannot be used for data-first mixed-mode CDs.
///
/// ## Examples
///
/// ```
/// use cdtoc::{Toc, TocKind};
///
/// let mut toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.audio_leadin(), 150);
///
/// // Bump it up to 182.
/// assert!(toc.set_audio_leadin(182).is_ok());
/// assert_eq!(toc.audio_leadin(), 182);
/// assert_eq!(
/// toc.to_string(),
/// "4+B6+2D4B+6276+B347+D86A",
/// );
///
/// // Back down to 150.
/// assert!(toc.set_audio_leadin(150).is_ok());
/// assert_eq!(toc.audio_leadin(), 150);
/// assert_eq!(
/// toc.to_string(),
/// "4+96+2D2B+6256+B327+D84A",
/// );
///
/// // For CD-Extra, the data track will get nudged too.
/// toc = Toc::from_cdtoc("3+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.kind(), TocKind::CDExtra);
/// assert_eq!(toc.audio_leadin(), 150);
/// assert_eq!(toc.data_sector(), Some(45863));
///
/// assert!(toc.set_audio_leadin(182).is_ok());
/// assert_eq!(toc.audio_leadin(), 182);
/// assert_eq!(toc.data_sector(), Some(45895));
///
/// // And back again.
/// assert!(toc.set_audio_leadin(150).is_ok());
/// assert_eq!(toc.audio_leadin(), 150);
/// assert_eq!(toc.data_sector(), Some(45863));
/// ```
///
/// ## Errors
///
/// This will return an error if the leadin is less than `150`, the CD
/// format is data-first, or the nudging causes the sectors to overflow
/// `u32`.
pub fn set_audio_leadin(&mut self, leadin: u32) -> Result<(), TocError> {
use std::cmp::Ordering;
if leadin < 150 { Err(TocError::LeadinSize) }
else if matches!(self.kind, TocKind::DataFirst) {
Err(TocError::Format(TocKind::DataFirst))
}
else {
let current = self.audio_leadin();
match leadin.cmp(¤t) {
// Nudge downward.
Ordering::Less => {
let diff = current - leadin;
for v in &mut self.audio { *v -= diff; }
if self.has_data() { self.data -= diff; }
self.leadout -= diff;
},
// Nudge upward.
Ordering::Greater => {
let diff = leadin - current;
for v in &mut self.audio {
*v = v.checked_add(diff).ok_or(TocError::SectorSize)?;
}
if self.has_data() {
self.data = self.data.checked_add(diff)
.ok_or(TocError::SectorSize)?;
}
self.leadout = self.leadout.checked_add(diff)
.ok_or(TocError::SectorSize)?;
},
// Noop.
Ordering::Equal => {},
}
Ok(())
}
}
/// # Set Media Kind.
///
/// This method can be used to override the table of content's derived
/// media format.
///
/// This is weird, but might come in handy if you need to correct a not-
/// quite-right CDTOC metadata tag value, such as one that accidentally
/// included the data session in its leading track count or ordered the
/// sectors of a data-audio CD sequentially.
///
/// ## Examples
///
/// ```
/// use cdtoc::{Toc, TocKind};
///
/// // This will be interpreted as audio-only.
/// let mut toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
///
/// // If the track count was wrong and it is really a mixed-mode CD-Extra
/// // disc, this will fix it right up:
/// assert!(toc.set_kind(TocKind::CDExtra).is_ok());
/// assert_eq!(
/// toc.to_string(),
/// "3+96+2D2B+6256+B327+D84A",
/// );
/// ```
///
/// ## Errors
///
/// This will return an error if there aren't enough sectors or tracks for
/// the new kind.
pub fn set_kind(&mut self, kind: TocKind) -> Result<(), TocError> {
match (self.kind, kind) {
// The last "audio" track is really data.
(TocKind::Audio, TocKind::CDExtra) => {
let len = self.audio.len();
if len == 1 { return Err(TocError::NoAudio); }
self.data = self.audio.remove(len - 1);
},
// The first "audio" track is really data.
(TocKind::Audio, TocKind::DataFirst) => {
if self.audio.len() == 1 { return Err(TocError::NoAudio); }
self.data = self.audio.remove(0);
},
// The "data" track is the really the last audio track.
(TocKind::CDExtra, TocKind::Audio) => {
self.audio.push(self.data);
self.data = 0;
},
// The "data" track is the really the last audio track.
(TocKind::DataFirst, TocKind::Audio) => {
self.audio.insert(0, self.data);
self.data = 0;
},
// Data should come first, not last.
(TocKind::CDExtra, TocKind::DataFirst) => {
// Move the old track to the end of the audio list and replace
// with the first.
self.audio.push(self.data);
self.data = self.audio.remove(0);
},
// Data should come last, not first.
(TocKind::DataFirst, TocKind::CDExtra) => {
// Move the old track to the front of the audio list and
// replace with the last.
self.audio.insert(0, self.data);
self.data = self.audio.remove(self.audio.len() - 1);
},
// Noop.
_ => return Ok(()),
}
self.kind = kind;
Ok(())
}
}
impl Toc {
#[must_use]
/// # Audio Leadin.
///
/// Return the leadin of the audio session, sometimes called the "offset".
/// In practice, this is just where the first audio track begins.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.audio_leadin(), 150);
/// ```
pub fn audio_leadin(&self) -> u32 { self.audio[0] }
#[must_use]
/// # Normalized Audio Leadin.
///
/// This is the same as [`Toc::audio_leadin`], but _without_ the mandatory
/// 150-sector CD lead-in.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.audio_leadin(), 150);
/// assert_eq!(toc.audio_leadin_normalized(), 0);
/// ```
pub fn audio_leadin_normalized(&self) -> u32 { self.audio[0] - 150 }
#[must_use]
/// # Audio Leadout.
///
/// Return the leadout for the audio session. This is usually the same as
/// [`Toc::leadout`], but for CD-Extra discs, the audio leadout is actually
/// the start of the data, minus a gap of `11_400`.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.audio_leadout(), 55370);
/// ```
pub const fn audio_leadout(&self) -> u32 {
if matches!(self.kind, TocKind::CDExtra) {
self.data.saturating_sub(11_400)
}
else { self.leadout }
}
#[must_use]
/// # Normalized Audio Leadout.
///
/// This is the same as [`Toc::audio_leadout`], but _without_ the mandatory
/// 150-sector CD lead-in.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.audio_leadout(), 55370);
/// assert_eq!(toc.audio_leadout_normalized(), 55220);
/// ```
pub const fn audio_leadout_normalized(&self) -> u32 {
self.audio_leadout() - 150
}
#[must_use]
/// # Number of Audio Tracks.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.audio_len(), 4);
/// ```
pub fn audio_len(&self) -> usize { self.audio.len() }
#[must_use]
/// # Audio Sectors.
///
/// Return the starting positions of each audio track.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.audio_sectors(), &[150, 11563, 25174, 45863]);
/// ```
pub fn audio_sectors(&self) -> &[u32] { &self.audio }
#[allow(clippy::cast_possible_truncation)]
#[must_use]
/// # Audio Track.
///
/// Return the details of a given audio track on the disc, or `None` if the
/// track number is out of range.
pub fn audio_track(&self, num: usize) -> Option<Track> {
let len = self.audio_len();
if num == 0 || len < num { None }
else {
let from = self.audio[num - 1];
let to =
if num < len { self.audio[num] }
else { self.audio_leadout() };
Some(Track {
num: num as u8,
pos: TrackPosition::from((num, len)),
from,
to,
})
}
}
#[must_use]
/// # Audio Tracks.
///
/// Return an iterator of [`Track`] details covering the whole album.
pub fn audio_tracks(&self) -> Tracks<'_> {
Tracks::new(&self.audio, self.audio_leadout())
}
#[must_use]
/// # Data Sector.
///
/// Return the starting position of the data track, if any.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// // No data here.
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.data_sector(), None);
///
/// // This CD-Extra has data, though!
/// let toc = Toc::from_cdtoc("3+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.data_sector(), Some(45_863));
/// ```
pub const fn data_sector(&self) -> Option<u32> {
if self.kind.has_data() { Some(self.data) }
else { None }
}
#[must_use]
/// # Normalized Data Sector.
///
/// This is the same as [`Toc::data_sector`], but _without_ the mandatory
/// 150-sector CD lead-in.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// // No data here.
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.data_sector(), None);
///
/// // This CD-Extra has data, though!
/// let toc = Toc::from_cdtoc("3+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.data_sector(), Some(45_863));
/// assert_eq!(toc.data_sector_normalized(), Some(45_713));
/// ```
pub const fn data_sector_normalized(&self) -> Option<u32> {
if self.kind.has_data() { Some(self.data.saturating_sub(150)) }
else { None }
}
#[must_use]
/// # Has Data?
///
/// This returns `true` for mixed-mode CDs and `false` for audio-only ones.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.has_data(), false);
///
/// let toc = Toc::from_cdtoc("3+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.has_data(), true);
/// ```
pub const fn has_data(&self) -> bool { self.kind.has_data() }
#[must_use]
/// # HTOA Pre-gap "Track".
///
/// Return a `Track` object representing the space between the mandatory
/// disc leadin (`150`) and the start of the first audio track, if any.
///
/// Such regions usually only contain a small amount of silence — extra
/// padding, basically — but every once in a while might be a secret bonus
/// song.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// // This disc has no HTOA.
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert!(toc.htoa().is_none());
///
/// // But this one does!
/// let toc = Toc::from_cdtoc("15+247E+2BEC+4AF4+7368+9704+B794+E271+110D0+12B7A+145C1+16CAF+195CF+1B40F+1F04A+21380+2362D+2589D+2793D+2A760+2DA32+300E1+32B46").unwrap();
/// let htoa = toc.htoa().unwrap();
/// assert!(htoa.is_htoa()); // Should always be true.
///
/// // HTOAs have no track number.
/// assert_eq!(htoa.number(), 0);
///
/// // Their position is also technically invalid.
/// assert!(! htoa.position().is_valid());
///
/// // Their ranges are normal, though.
/// assert_eq!(htoa.sector_range(), 150..9342);
/// ```
pub fn htoa(&self) -> Option<Track> {
let leadin = self.audio_leadin();
if leadin == 150 || matches!(self.kind, TocKind::DataFirst) { None }
else {
Some(Track {
num: 0,
pos: TrackPosition::Invalid,
from: 150,
to: leadin,
})
}
}
#[must_use]
/// # CD Format.
///
/// This returns the [`TocKind`] corresponding to the table of contents,
/// useful if you want to know whether or not the disc has a data session,
/// and where it is in relation to the audio session.
///
/// ## Examples
///
/// ```
/// use cdtoc::{Toc, TocKind};
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.kind(), TocKind::Audio);
///
/// let toc = Toc::from_cdtoc("3+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.kind(), TocKind::CDExtra);
///
/// let toc = Toc::from_cdtoc("3+2D2B+6256+B327+D84A+X96").unwrap();
/// assert_eq!(toc.kind(), TocKind::DataFirst);
/// ```
pub const fn kind(&self) -> TocKind { self.kind }
#[must_use]
/// # Absolute Leadin.
///
/// Return the offset of the first track (no matter the session type).
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.leadin(), 150);
/// ```
pub fn leadin(&self) -> u32 {
if matches!(self.kind, TocKind::DataFirst) { self.data }
else { self.audio[0] }
}
#[must_use]
/// # Normalized Absolute Leadin.
///
/// This is the same as [`Toc::leadin`], but _without_ the mandatory
/// 150-sector CD lead-in.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.leadin(), 150);
/// assert_eq!(toc.leadin_normalized(), 0);
/// ```
pub fn leadin_normalized(&self) -> u32 {
if matches!(self.kind, TocKind::DataFirst) { self.data.saturating_sub(150) }
else { self.audio[0] - 150 }
}
#[must_use]
/// # Absolute Leadout.
///
/// Return the disc leadout, regardless of whether it marks the end of the
/// audio or data session.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.leadout(), 55_370);
/// ```
pub const fn leadout(&self) -> u32 { self.leadout }
#[must_use]
/// # Normalized Absolute Leadout.
///
/// This is the same as [`Toc::leadout`], but _without_ the mandatory
/// 150-sector CD lead-in.
///
/// ## Examples
///
/// ```
/// use cdtoc::Toc;
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(toc.leadout(), 55_370);
/// assert_eq!(toc.leadout_normalized(), 55_220);
/// ```
pub const fn leadout_normalized(&self) -> u32 { self.leadout - 150 }
#[must_use]
/// # Duration.
///
/// Return the total duration of all audio tracks.
///
/// ## Examples
///
/// ```
/// use cdtoc::{Duration, Toc};
///
/// let toc = Toc::from_cdtoc("4+96+2D2B+6256+B327+D84A").unwrap();
/// assert_eq!(
/// toc.duration(),
/// toc.audio_tracks().map(|t| t.duration()).sum(),
/// );
/// ```
pub fn duration(&self) -> Duration {
Duration::from(self.audio_leadout() - self.audio_leadin())
}
}
#[derive(Debug, Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
/// # CD Format.
///
/// This enum is used to differentiate between audio-only and mixed-mode discs
/// because that ultimately determines the formatting of CDTOC metadata values
/// and various derived third-party IDs.
pub enum TocKind {
#[default]
/// # Audio-Only.
Audio,
/// # Mixed w/ Trailing Data Session.
CDExtra,
/// # Mixed w/ Leading Data Session.
///
/// This would only be possible with a weird homebrew CD-R; retail CDs
/// place their data sessions at the end.
DataFirst,
}
impl fmt::Display for TocKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl TocKind {
#[must_use]
/// # As Str.
///
/// Return the value as a string slice.
pub const fn as_str(self) -> &'static str {
match self {
Self::Audio => "audio-only",
Self::CDExtra => "CD-Extra",
Self::DataFirst => "data+audio",
}
}
#[must_use]
/// # Has Data?
///
/// Returns `true` if the format is mixed-mode.
///
/// ## Examples
///
/// ```
/// use cdtoc::TocKind;
///
/// // Yep!
/// assert!(TocKind::CDExtra.has_data());
/// assert!(TocKind::DataFirst.has_data());
///
/// // Nope!
/// assert!(! TocKind::Audio.has_data());
/// ```
pub const fn has_data(self) -> bool {
matches!(self, Self::CDExtra | Self::DataFirst)
}
}
/// # Parse CDTOC Metadata.
///
/// This parses the audio track count and sector positions from a CDTOC-style
/// metadata tag value. It will return a parsing error if the formatting is
/// grossly wrong, but will not validate the sanity of the count/parts.
fn parse_cdtoc_metadata(src: &[u8]) -> Result<(Vec<u32>, Option<u32>, u32), TocError> {
let src = src.trim();
let mut split = src.split(|b| b'+'.eq(b));
// The number of audio tracks comes first.
let audio_len = split.next()
.and_then(u8::htou)
.ok_or(TocError::TrackCount)?;
// We should have starting positions for just as many tracks.
let sectors: Vec<u32> = split
.by_ref()
.take(usize::from(audio_len))
.map(u32::htou)
.collect::<Option<Vec<u32>>>()
.ok_or(TocError::SectorSize)?;
// Make sure we actually do.
let sectors_len = sectors.len();
if 0 == sectors_len { return Err(TocError::NoAudio); }
if sectors_len != usize::from(audio_len) {
return Err(TocError::SectorCount(audio_len, sectors_len));
}
// There should be at least one more entry to mark the audio leadout.
let last1 = split.next()
.ok_or(TocError::SectorCount(audio_len, sectors_len - 1))?;
let last1 = u32::htou(last1).ok_or(TocError::SectorSize)?;
// If there is yet another entry, we've got a mixed-mode disc.
if let Some(last2) = split.next() {
// Unlike the other values, this entry might have an x-prefix to denote
// a non-standard data-first position.
let last2 = u32::htou(last2)
.or_else(||
last2.strip_prefix(b"X").or_else(|| last2.strip_prefix(b"x"))
.and_then(u32::htou)
)
.ok_or(TocError::SectorSize)?;
// That should be that!
let remaining = split.count();
if remaining == 0 {
// "last1" is data, "last2" is leadout.
if last1 < last2 {
Ok((sectors, Some(last1), last2))
}
// "last2" is data, "last1" is leadout.
else {
Ok((sectors, Some(last2), last1))
}
}
// Too many sectors!
else {
Err(TocError::SectorCount(audio_len, sectors_len + remaining))
}
}
// A typical audio-only CD.
else { Ok((sectors, None, last1)) }
}
#[cfg(test)]
mod tests {
use super::*;
use brunch as _;
use serde_json as _;
const CDTOC_AUDIO: &str = "B+96+5DEF+A0F2+F809+1529F+1ACB3+20CBC+24E14+2AF17+2F4EA+35BDD+3B96D";
const CDTOC_EXTRA: &str = "A+96+3757+696D+C64F+10A13+14DA2+19E88+1DBAA+213A4+2784E+2D7AF+36F11";
const CDTOC_DATA_AUDIO: &str = "A+3757+696D+C64F+10A13+14DA2+19E88+1DBAA+213A4+2784E+2D7AF+36F11+X96";
#[test]
/// # Test Audio-Only Parsing.
fn t_audio() {
let toc = Toc::from_cdtoc(CDTOC_AUDIO).expect("Unable to parse CDTOC_AUDIO.");
let sectors = vec![
150,
24047,
41202,
63497,
86687,
109747,
134332,
151060,
175895,
193770,
220125,
];
assert_eq!(toc.audio_len(), 11);
assert_eq!(toc.audio_sectors(), §ors);
assert_eq!(toc.data_sector(), None);
assert_eq!(toc.has_data(), false);
assert_eq!(toc.kind(), TocKind::Audio);
assert_eq!(toc.audio_leadin(), 150);
assert_eq!(toc.audio_leadout(), 244077);
assert_eq!(toc.leadin(), 150);
assert_eq!(toc.leadout(), 244077);
assert_eq!(toc.to_string(), CDTOC_AUDIO);
// This should match when built with the equivalent parts.
assert_eq!(
Toc::from_parts(sectors, None, 244077),
Ok(toc),
);
// Let's also quickly test that a long TOC works gets the audio track
// count right.
let toc = Toc::from_cdtoc("20+96+33BA+5B5E+6C74+7C96+91EE+A9A3+B1AC+BEFC+D2E6+E944+103AC+11426+14B58+174E2+1A9F7+1C794+1F675+21AB9+24090+277DD+2A783+2D508+2DEAA+2F348+31F20+37419+3A463+3DC2F+4064B+43337+4675B+4A7C0")
.expect("Long TOC failed.");
assert_eq!(toc.audio_len(), 32);
assert_eq!(
toc.to_string(),
"20+96+33BA+5B5E+6C74+7C96+91EE+A9A3+B1AC+BEFC+D2E6+E944+103AC+11426+14B58+174E2+1A9F7+1C794+1F675+21AB9+24090+277DD+2A783+2D508+2DEAA+2F348+31F20+37419+3A463+3DC2F+4064B+43337+4675B+4A7C0"
);
// And one more with a hexish track count.
let toc = Toc::from_cdtoc("10+96+2B4E+4C51+6B3C+9E08+CD43+FC99+13A55+164B8+191C9+1C0FF+1F613+21B5A+23F70+27A4A+2C20D+2FC65").unwrap();
assert_eq!(toc.audio_len(), 16);
assert_eq!(
toc.to_string(),
"10+96+2B4E+4C51+6B3C+9E08+CD43+FC99+13A55+164B8+191C9+1C0FF+1F613+21B5A+23F70+27A4A+2C20D+2FC65"
);
}
#[test]
/// # Test CD-Extra Parsing.
fn t_extra() {
let toc = Toc::from_cdtoc(CDTOC_EXTRA).expect("Unable to parse CDTOC_EXTRA.");
let sectors = vec![
150,
14167,
26989,
50767,
68115,
85410,
106120,
121770,
136100,
161870,
];
assert_eq!(toc.audio_len(), 10);
assert_eq!(toc.audio_sectors(), §ors);
assert_eq!(toc.data_sector(), Some(186287));
assert_eq!(toc.has_data(), true);
assert_eq!(toc.kind(), TocKind::CDExtra);
assert_eq!(toc.audio_leadin(), 150);
assert_eq!(toc.audio_leadout(), 174887);
assert_eq!(toc.leadin(), 150);
assert_eq!(toc.leadout(), 225041);
assert_eq!(toc.to_string(), CDTOC_EXTRA);
// This should match when built with the equivalent parts.
assert_eq!(
Toc::from_parts(sectors, Some(186287), 225041),
Ok(toc),
);
}
#[test]
/// # Test Data-First Parsing.
fn t_data_first() {
let toc = Toc::from_cdtoc(CDTOC_DATA_AUDIO)
.expect("Unable to parse CDTOC_DATA_AUDIO.");
let sectors = vec![
14167,
26989,
50767,
68115,
85410,
106120,
121770,
136100,
161870,
186287,
];
assert_eq!(toc.audio_len(), 10);
assert_eq!(toc.audio_sectors(), §ors);
assert_eq!(toc.data_sector(), Some(150));
assert_eq!(toc.has_data(), true);
assert_eq!(toc.kind(), TocKind::DataFirst);
assert_eq!(toc.audio_leadin(), 14167);
assert_eq!(toc.audio_leadout(), 225041);
assert_eq!(toc.leadin(), 150);
assert_eq!(toc.leadout(), 225041);
assert_eq!(toc.to_string(), CDTOC_DATA_AUDIO);
// This should match when built with the equivalent parts.
assert_eq!(
Toc::from_parts(sectors, Some(150), 225041),
Ok(toc),
);
}
#[test]
/// # Test Metadata Failures.
fn t_bad() {
for i in [
"A+96+3757+696D+C64F+10A13+14DA2+19E88+1DBAA+213A4+2784E+2D7AF+36F11+36F12",
"A+96+3757+696D+C64F+10A13+14DA2+19E88+1DBAA+213A4+2784E",
"0+96",
"A+96+3757+696D+C64F+10A13+14DA2+19E88+2784E+1DBAA+213A4+2D7AF+36F11",
] {
assert!(Toc::from_cdtoc(i).is_err());
}
}
#[test]
/// # Test Kind Conversions.
fn t_rekind() {
// Start with audio.
let mut toc = Toc::from_cdtoc(CDTOC_AUDIO)
.expect("Unable to parse CDTOC_AUDIO.");
// To CD-Extra.
assert!(toc.set_kind(TocKind::CDExtra).is_ok());
assert_eq!(toc.audio_len(), 10);
assert_eq!(
toc.audio_sectors(),
&[
150,
24047,
41202,
63497,
86687,
109747,
134332,
151060,
175895,
193770,
]
);
assert_eq!(toc.data_sector(), Some(220125));
assert_eq!(toc.has_data(), true);
assert_eq!(toc.kind(), TocKind::CDExtra);
assert_eq!(toc.audio_leadin(), 150);
assert_eq!(toc.audio_leadout(), 208725);
assert_eq!(toc.leadin(), 150);
assert_eq!(toc.leadout(), 244077);
// Back again.
assert!(toc.set_kind(TocKind::Audio).is_ok());
assert_eq!(Toc::from_cdtoc(CDTOC_AUDIO).unwrap(), toc);
// To data-audio.
assert!(toc.set_kind(TocKind::DataFirst).is_ok());
assert_eq!(toc.audio_len(), 10);
assert_eq!(
toc.audio_sectors(),
&[
24047,
41202,
63497,
86687,
109747,
134332,
151060,
175895,
193770,
220125,
]
);
assert_eq!(toc.data_sector(), Some(150));
assert_eq!(toc.has_data(), true);
assert_eq!(toc.kind(), TocKind::DataFirst);
assert_eq!(toc.audio_leadin(), 24047);
assert_eq!(toc.audio_leadout(), 244077);
assert_eq!(toc.leadin(), 150);
assert_eq!(toc.leadout(), 244077);
// Back again.
assert!(toc.set_kind(TocKind::Audio).is_ok());
assert_eq!(Toc::from_cdtoc(CDTOC_AUDIO).unwrap(), toc);
// Now test data-to-other-data conversions.
toc = Toc::from_cdtoc(CDTOC_EXTRA)
.expect("Unable to parse CDTOC_EXTRA.");
let extra = toc.clone();
let data_audio = Toc::from_cdtoc(CDTOC_DATA_AUDIO)
.expect("Unable to parse CDTOC_DATA_AUDIO.");
// To data-audio.
assert!(toc.set_kind(TocKind::DataFirst).is_ok());
assert_eq!(toc, data_audio);
// And back again.
assert!(toc.set_kind(TocKind::CDExtra).is_ok());
assert_eq!(toc, extra);
}
}