summaryrefslogtreecommitdiff
path: root/vendored/mpd/src/version.rs
blob: 7a62d6a3b142a7e68a5353d58ae3ac4ed31d256b (plain)
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
//! This module defines MPD version type and parsing code


use crate::error::ParseError;
use std::str::FromStr;

// Version {{{
/// MPD version
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, RustcEncodable)]
pub struct Version(pub u16, pub u16, pub u16);

impl FromStr for Version {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Version, ParseError> {
        let mut splits = s.splitn(3, '.').map(FromStr::from_str);
        match (splits.next(), splits.next(), splits.next()) {
            (Some(Ok(a)), Some(Ok(b)), Some(Ok(c))) => Ok(Version(a, b, c)),
            (Some(Err(e)), _, _) |
            (_, Some(Err(e)), _) |
            (_, _, Some(Err(e))) => Err(ParseError::BadInteger(e)),
            _ => Err(ParseError::BadVersion),
        }
    }
}
// }}}