Fix inconsistencies in the examples

This commit is contained in:
Serial 2022-03-15 20:52:10 -04:00
parent 45182b6e3d
commit 513cce7d41
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
4 changed files with 21 additions and 16 deletions

View file

@ -33,7 +33,7 @@ To try them out, run:
```bash
cargo run --example tag_reader /path/to/file
cargo run --example tag_stripper /path/to/file
cargo run --example tag_writer <options> --path /path/to/file
cargo run --example tag_writer <options> /path/to/file
```
## Documentation

View file

@ -2,21 +2,24 @@ use lofty::{Accessor, Probe};
use std::path::Path;
fn main() {
let path_str = std::env::args().nth(1).expect("Error: No path specified!");
let path_str = std::env::args().nth(1).expect("ERROR: No path specified!");
let path = Path::new(&path_str);
if !path.is_file() {
panic!("Error: Path is not a file!");
panic!("ERROR: Path is not a file!");
}
let tagged_file = Probe::open(path)
.expect("Error: Bad path provided!")
.expect("ERROR: Bad path provided!")
.read(true)
.expect("Error: Failed to read file!");
.expect("ERROR: Failed to read file!");
let tag = match tagged_file.primary_tag() {
Some(primary_tag) => primary_tag,
None => tagged_file.first_tag().expect("Error: No tags found!"),
// If the "primary" tag doesn't exist, we just grab the
// first tag we can find. Realistically, a tag reader would likely
// iterate through the tags to find a suitable one.
None => tagged_file.first_tag().expect("ERROR: No tags found!"),
};
println!("--- Tag Information ---");

View file

@ -2,17 +2,17 @@ use lofty::Probe;
use std::io::Write;
fn main() {
let path = std::env::args().nth(1).expect("Error: No path specified!");
let path = std::env::args().nth(1).expect("ERROR: No path specified!");
let tagged_file = Probe::open(path.as_str())
.expect("Error: Bad path provided!")
.expect("ERROR: Bad path provided!")
.read(false)
.expect("Error: Failed to read file!");
.expect("ERROR: Failed to read file!");
let tags = tagged_file.tags();
if tags.is_empty() {
eprintln!("No tags found, exiting.");
eprintln!("ERROR: No tags found, exiting.");
std::process::exit(0);
}

View file

@ -2,6 +2,8 @@ use lofty::{Accessor, Probe, Tag, TagExt};
use structopt::StructOpt;
use std::path::PathBuf;
#[derive(Debug, StructOpt)]
#[structopt(name = "tag_writer", about = "A simple tag writer example")]
struct Opt {
@ -17,17 +19,17 @@ struct Opt {
#[structopt(short, long)]
genre: Option<String>,
#[structopt(short, long)]
path: String,
#[structopt(parse(from_os_str))]
path: PathBuf,
}
fn main() {
let opt = Opt::from_args();
let mut tagged_file = Probe::open(opt.path.as_str())
.expect("Error: Bad path provided!")
let mut tagged_file = Probe::open(&opt.path)
.expect("ERROR: Bad path provided!")
.read(false)
.expect("Error: Failed to read file!");
.expect("ERROR: Failed to read file!");
let tag = match tagged_file.primary_tag_mut() {
Some(primary_tag) => primary_tag,
@ -76,7 +78,7 @@ fn main() {
tag.set_genre(genre)
}
tag.save_to_path(opt.path)
tag.save_to_path(&opt.path)
.expect("ERROR: Failed to write the tag!");
println!("INFO: Tag successfully updated!");