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
pub mod db;
mod fingerprint;

use db::Repository;
use std::error::Error;
use std::path::Path;

pub struct MusicLibrary<T>
where
    T: Repository,
{
    repo: T,
}

impl<T> MusicLibrary<T>
where
    T: Repository,
{
    /// Add song
    pub fn add(&self, filename: &str) -> Result<(), Box<Error>> {
        let hash_array = fingerprint::calc_fingerprint(filename)?;

        let song = match Path::new(filename).file_stem() {
            Some(stem) => match stem.to_str() {
                Some(stem_str) => stem_str,
                None => return Err(Box::from(format!("can't convert {:?} to str", stem))),
            },
            None => return Err(Box::from("filename is empty")),
        };

        self.repo.index(song, &hash_array)
    }

    /// Recognize song
    pub fn recognize(&self, filename: &str) -> Result<String, Box<Error>> {
        let hash_array = fingerprint::calc_fingerprint(filename)?;

        match self.repo.find(&hash_array) {
            Ok(opt) => match opt {
                Some(res) => Ok(res),
                None => Ok(String::from("No matchings")),
            },
            Err(e) => Err(Box::from(e)),
        }
    }
}

#[cfg(test)]
mod tests {}