itunes_com/wrappers/
types.rs

1use std::marker::PhantomData;
2
3use windows::Win32::System::Com::VARIANT;
4
5
6pub type PersistentId = u64;
7
8
9/// A wrapper around a COM VARIANT type
10pub struct Variant<'a, T: 'a> {
11    inner: VARIANT,
12    lifetime: PhantomData<&'a T>,
13}
14
15impl<'a, T> Variant<'a, T> {
16    pub(crate) fn new(inner: VARIANT) -> Self {
17        Self { inner, lifetime: PhantomData }
18    }
19
20    /// Get the wrapped `VARIANT`
21    pub fn as_raw(&self) -> &VARIANT {
22        &self.inner
23    }
24}
25
26/// The rating of a track (one to five stars)
27pub enum Rating {
28    /// No rating
29    None,
30    /// One-star rating
31    One,
32    /// Two-star rating
33    Two,
34    /// Three-star rating
35    Three,
36    /// Four-star rating
37    Four,
38    /// Five-star rating
39    Five,
40}
41
42impl Rating {
43    /// Create a `Rating` instance.
44    ///
45    /// All invalid values are mapped to `Rating::None`
46    pub fn from_stars(stars: Option<u8>) -> Self {
47        match stars {
48            Some(1) => Self::One,
49            Some(2) => Self::Two,
50            Some(3) => Self::Three,
51            Some(4) => Self::Four,
52            Some(5) => Self::Five,
53            _ => Self::None,
54        }
55    }
56
57    /// Get the count of stars of this rating
58    pub fn stars(&self) -> Option<u8> {
59        match self {
60            Rating::None => None,
61            Rating::One => Some(1),
62            Rating::Two => Some(2),
63            Rating::Three => Some(3),
64            Rating::Four => Some(4),
65            Rating::Five => Some(5),
66        }
67    }
68}
69
70// Conversion with LONG because that's what iTunes uses.
71impl std::convert::From<super::LONG> for Rating {
72    fn from(long: super::LONG) -> Rating {
73        match long {
74            0..=19 => Rating::None,
75            20..=39 => Rating::One,
76            40..=59 => Rating::Two,
77            60..=79 => Rating::Three,
78            80..=99 => Rating::Four,
79            100.. => Rating::Five,
80            // Not supposed to happen
81            _ => Rating::None,
82        }
83    }
84}
85
86impl std::convert::From<Rating> for super::LONG {
87    fn from(rating: Rating) -> super::LONG {
88        match rating {
89            Rating::None => 0,
90            Rating::One => 20,
91            Rating::Two => 40,
92            Rating::Three => 60,
93            Rating::Four => 80,
94            Rating::Five => 100,
95        }
96    }
97}