Struct discid::DiscId

source ·
pub struct DiscId { /* private fields */ }
Expand description

DiscId holds information about a disc (TOC, MCN, ISRCs).

Use DiscId::read, DiscId::read_features, DiscId::put or DiscId::parse to initialize an instance of DiscId.

Implementations§

source§

impl DiscId

source

pub fn read(device: Option<&str>) -> Result<DiscId, DiscError>

Read the disc in the given CD-ROM/DVD-ROM drive extracting only the TOC.

This function reads the disc in the drive specified by the given device identifier. If the device is None, the default device, as returned by DiscId::default_device, is used.

This function will only read the TOC, hence only the disc ID itself will be available. Use DiscId::read_features if you want to read also MCN and ISRCs.

Examples

Read from default device

use discid::DiscId;

let disc = DiscId::read(None).expect("Reading disc failed");
println!("ID: {}", disc.id());

Read from specific device

let disc = DiscId::read(Some("/dev/sr1")).expect("Reading disc failed");
println!("ID: {}", disc.id());
source

pub fn read_features( device: Option<&str>, features: Features ) -> Result<DiscId, DiscError>

Read the disc in the given CD-ROM/DVD-ROM drive with additional features.

This function is similar to DiscId::read but allows to read information about MCN and per-track ISRCs in addition to the normal TOC data.

The parameter features accepts a bitwise combination of values defined in Features. Features::READ is always implied, so it is not necessary to specify it.

Reading MCN and ISRCs is not available on all platforms. You can use the has_feature method to check if a specific feature is available. Passing unsupported features here will just be ignored.

Note that reading MCN and ISRC data is significantly slower than just reading the TOC, so only request the features you actually need.

Examples

Read both ISRC and MCN from default device

use discid::{DiscId, Features};

let features = Features::MCN | Features::ISRC;
let disc = DiscId::read_features(None, features).expect("Reading disc failed");
println!("ID : {}", disc.id());
println!("MCN: {}", disc.mcn());
for track in disc.tracks() {
    println!("#{} ISRC: {}", track.number, track.isrc);
}
Examples found in repository?
examples/readisrcs.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let result = DiscId::read_features(None, Features::ISRC);

    match result {
        Ok(disc) => {
            println!("Disc ID: {}", disc.id());

            for track in disc.tracks() {
                println!("Track #{} ISRC: {}", track.number, track.isrc);
            }
        }
        Err(e) => {
            eprintln!("{}", e);
            std::process::exit(1);
        }
    }
}
More examples
Hide additional examples
examples/readdiscid.rs (line 12)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
    // Read the device name from the command line or use the default.
    let arg1 = env::args().nth(1).unwrap_or_default();
    let device = if !arg1.is_empty() {
        Some(&arg1[..])
    } else {
        None
    };
    let result = DiscId::read_features(device, Features::all());

    match result {
        Ok(disc) => print_disc_info(disc),
        Err(e) => {
            eprintln!("{}", e);
            std::process::exit(1);
        }
    }
}
source

pub fn put(first: i32, offsets: &[i32]) -> Result<DiscId, DiscError>

Provides the TOC of a known CD.

This function may be used if the TOC has been read earlier and you want to calculate the disc ID afterwards, without accessing the disc drive.

first is the track number of the first track (1-99). The offsets parameter points to an array which contains the track offsets for each track. The first element, offsets[0], is the lead-out track. It must contain the total number of sectors on the disc. offsets must not be longer than 100 elements (lead-out + 99 tracks).

Examples:
use discid::DiscId;

let first_track = 1;
// The offsets contain the total number of sectors as first element, followed by
// the start sectors of each track.
let offsets = [
   242457, 150, 44942, 61305, 72755, 96360, 130485, 147315, 164275, 190702, 205412, 220437,
];
let disc = DiscId::put(first_track, &offsets).expect("DiscId::put() failed");
assert_eq!("lSOVc5h6IXSuzcamJS1Gp4_tRuA-", disc.id());
Examples found in repository?
examples/discfromtoc.rs (line 7)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    let offsets = [
        242457, 150, 44942, 61305, 72755, 96360, 130485, 147315, 164275, 190702, 205412, 220437,
    ];
    let result = DiscId::put(1, &offsets);

    match result {
        Ok(disc) => print_disc_info(disc),
        Err(e) => {
            eprintln!("{}", e);
            std::process::exit(1);
        }
    }
}
source

pub fn parse(toc: &str) -> Result<DiscId, DiscError>

Parses a TOC string and returns a DiscId instance for it.

The TOC string provided here must have the same format as returned by toc_string.

This function can be used if you already have a TOC string like e.g. 1 11 242457 150 44942 61305 72755 96360 130485 147315 164275 190702 205412 220437.

Examples:
use discid::DiscId;

let toc = "1 11 242457 150 44942 61305 72755 96360 130485 147315 164275 190702 205412 220437";
let disc = DiscId::parse(toc).expect("DiscId::put() failed");
assert_eq!("lSOVc5h6IXSuzcamJS1Gp4_tRuA-", disc.id());
assert_eq!(toc, disc.toc_string());
Examples found in repository?
examples/parsetoc.rs (line 5)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    let toc = "1 11 242457 150 44942 61305 72755 96360 130485 147315 164275 190702 205412 220437";
    let result = DiscId::parse(toc);

    match result {
        Ok(disc) => print_disc_info(disc),
        Err(e) => {
            eprintln!("{}", e);
            std::process::exit(1);
        }
    }
}
source

pub fn has_feature(feature: Features) -> bool

Check if a certain feature is implemented on the current platform.

This only works for single features, not bit masks with multiple features.

See the libdiscid feature matrix for a list of supported features per platform.

Examples
use discid::{DiscId, Features};

let can_read = DiscId::has_feature(Features::READ);
assert!(can_read);
source

pub fn version_string() -> String

Return version information about libdiscid.

The returned string will be e.g. "libdiscid 0.6.2".

Examples
use discid::DiscId;

println!("{}", DiscId::version_string());
Examples found in repository?
examples/discidinfo.rs (line 4)
3
4
5
6
fn main() {
    println!("Version       : {}", DiscId::version_string());
    println!("Default device: {}", DiscId::default_device());
}
source

pub fn default_device() -> String

Return the name of the default disc drive for this operating system.

The default device is system dependent, e.g. /dev/cdrom on Linux and D: on Windows.

Examples
use discid::DiscId;

println!("{}", DiscId::default_device());
Examples found in repository?
examples/discidinfo.rs (line 5)
3
4
5
6
fn main() {
    println!("Version       : {}", DiscId::version_string());
    println!("Default device: {}", DiscId::default_device());
}
source

pub fn id(&self) -> String

The MusicBrainz disc ID.

Examples found in repository?
examples/discfromtoc.rs (line 19)
18
19
20
21
22
fn print_disc_info(disc: DiscId) {
    println!("DiscId: {}", disc.id());
    println!("TOC   : {}", disc.toc_string());
    println!("\nSubmit via {}", disc.submission_url());
}
More examples
Hide additional examples
examples/readisrcs.rs (line 8)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let result = DiscId::read_features(None, Features::ISRC);

    match result {
        Ok(disc) => {
            println!("Disc ID: {}", disc.id());

            for track in disc.tracks() {
                println!("Track #{} ISRC: {}", track.number, track.isrc);
            }
        }
        Err(e) => {
            eprintln!("{}", e);
            std::process::exit(1);
        }
    }
}
examples/parsetoc.rs (line 17)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
examples/readdiscid.rs (line 24)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn freedb_id(&self) -> String

The FreeDB disc ID.

Examples found in repository?
examples/parsetoc.rs (line 18)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
More examples
Hide additional examples
examples/readdiscid.rs (line 25)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn toc_string(&self) -> String

Return a string representing CD Table Of Contents (TOC).

The TOC string is a list of integers separated by a single space character. The integers represent (in order):

  • First track number (normally one)
  • Last track number
  • Lead-out track offset
  • Up to 99 frame offsets

See also DiscId::parse and the documentation for Disc ID Calculation.

Examples found in repository?
examples/discfromtoc.rs (line 20)
18
19
20
21
22
fn print_disc_info(disc: DiscId) {
    println!("DiscId: {}", disc.id());
    println!("TOC   : {}", disc.toc_string());
    println!("\nSubmit via {}", disc.submission_url());
}
More examples
Hide additional examples
examples/parsetoc.rs (line 19)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
examples/readdiscid.rs (line 26)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn submission_url(&self) -> String

An URL for submitting the DiscID to MusicBrainz.

Examples found in repository?
examples/discfromtoc.rs (line 21)
18
19
20
21
22
fn print_disc_info(disc: DiscId) {
    println!("DiscId: {}", disc.id());
    println!("TOC   : {}", disc.toc_string());
    println!("\nSubmit via {}", disc.submission_url());
}
More examples
Hide additional examples
examples/parsetoc.rs (line 30)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
source

pub fn first_track_num(&self) -> i32

The number of the first track on this disc.

Examples found in repository?
examples/parsetoc.rs (line 20)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
More examples
Hide additional examples
examples/readdiscid.rs (line 28)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn last_track_num(&self) -> i32

The number of the last track on this disc.

Examples found in repository?
examples/parsetoc.rs (line 21)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
More examples
Hide additional examples
examples/readdiscid.rs (line 29)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn sectors(&self) -> i32

The length of the disc in sectors.

Examples found in repository?
examples/parsetoc.rs (line 22)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
More examples
Hide additional examples
examples/readdiscid.rs (line 30)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn mcn(&self) -> String

The media catalogue number on the disc, if present.

Examples found in repository?
examples/readdiscid.rs (line 27)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn tracks(&self) -> TrackIter

Returns an iterator to access information about each track on the disc.

Returns an instance of Track for each track.

Examples
use discid::DiscId;

let offsets = [
   242457, 150, 44942, 61305, 72755, 96360, 130485, 147315, 164275, 190702, 205412, 220437,
];
let disc = DiscId::put(1, &offsets).expect("DiscId::put() failed");
for track in disc.tracks() {
    println!("Track #{}", track.number);
    println!("    ISRC    : {}", track.isrc);
    println!("    Offset  : {}", track.offset);
    println!("    Sectors : {}", track.sectors);
}
Examples found in repository?
examples/readisrcs.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let result = DiscId::read_features(None, Features::ISRC);

    match result {
        Ok(disc) => {
            println!("Disc ID: {}", disc.id());

            for track in disc.tracks() {
                println!("Track #{} ISRC: {}", track.number, track.isrc);
            }
        }
        Err(e) => {
            eprintln!("{}", e);
            std::process::exit(1);
        }
    }
}
More examples
Hide additional examples
examples/parsetoc.rs (line 24)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }

    println!("\nSubmit via {}", disc.submission_url());
}
examples/readdiscid.rs (line 32)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
fn print_disc_info(disc: DiscId) {
    println!("DiscID      : {}", disc.id());
    println!("FreeDB ID   : {}", disc.freedb_id());
    println!("TOC         : {}", disc.toc_string());
    println!("MCN         : {}", disc.mcn());
    println!("First track : {}", disc.first_track_num());
    println!("Last track  : {}", disc.last_track_num());
    println!("Sectors     : {}\n", disc.sectors());

    for track in disc.tracks() {
        println!("Track #{}", track.number);
        println!("    ISRC    : {}", track.isrc);
        println!("    Offset  : {}", track.offset);
        println!("    Sectors : {}", track.sectors);
    }
}
source

pub fn nth_track(&self, number: i32) -> Track

Returns a Track instance for the nth track.

Panics

Panics if number is outside the range given by first_track_num and last_track_num.

Examples
use discid::DiscId;

let offsets = [
   242457, 150, 44942, 61305, 72755, 96360, 130485, 147315, 164275, 190702, 205412, 220437,
];
let disc = DiscId::put(1, &offsets).expect("DiscId::put() failed");
let track = disc.nth_track(7);
assert_eq!(7, track.number);
assert_eq!(147315, track.offset);
assert_eq!(16960, track.sectors);

Trait Implementations§

source§

impl Debug for DiscId

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for DiscId

§

impl !Send for DiscId

§

impl !Sync for DiscId

§

impl Unpin for DiscId

§

impl UnwindSafe for DiscId

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.