[][src]Crate titlefmt

titlefmt is a title formatting expression library. It enables automatic formatting of media metadata.

Example

extern crate titlefmt;

use std::collections::HashMap;
use titlefmt::{ metadata, Formatter };

pub struct MetadataProvider<'a> {
    metadata_dict: HashMap<&'a str, &'a str>,
}

impl<'a> metadata::Provider for MetadataProvider<'a> {
    fn tag_value(&self, key: &str) -> Option<String> {
        let entry = self.metadata_dict.get(key);
        if let Some(value) = entry {
            let s = value.to_string();
            Some(s)
        }
        else {
            None
        }
    }
}

impl<'a> MetadataProvider<'a> {
    pub fn new(metadata_dict: HashMap<&'a str, &'a str>) -> MetadataProvider<'a> {
        MetadataProvider {
            metadata_dict,
        }
    }
}

fn main() {
    let formatter = Formatter::new();
    // tests with optional expressions
    {
        let expression = formatter.parser().parse("%tracknumber%.[ %artist% -] %title%").unwrap();
        {
            let test_metadata = {
                let mut dict = HashMap::new();
                dict.insert("tracknumber", "1");
                dict.insert("title", "9th Symphony, 1. Allegro ma non troppo, un poco maestoso");
                dict.insert("composer", "Ludwig van Beethoven");
                MetadataProvider::new(dict)
            };
            let s = expression.apply(&test_metadata);
            assert_eq!("01. 9th Symphony, 1. Allegro ma non troppo, un poco maestoso", s.to_string().as_str());
        }
        {
            let test_metadata = {
                let mut dict = HashMap::new();
                dict.insert("tracknumber", "5");
                dict.insert("title", "Always Crashing In The Same Car");
                dict.insert("artist", "David Bowie");
                MetadataProvider::new(dict)
            };
            let s = expression.apply(&test_metadata);
            assert_eq!("05. David Bowie - Always Crashing In The Same Car", s.to_string().as_str());
        }
    }
}

Modules

expression

Expression module.

function

Functions module.

metadata

Metadata provider trait module.

Macros

expect_integer_result

Get the integer result for a sub-expression or return an error.

expect_string_result

Get the string result for a sub-expression or return an error.

make_function_object

Make a new Function from a function, using the name of the argument provided as its name.

try_integer_result

Get the integer result for a sub-expression and return an Option where T is the integer type. If no type is given, then i32 is assumed.

Structs

FormatParser

Title formatting parser, spawned from a Formatter.

Formatter

Title formatting context.