openlibspot_core/
config.rs

1use std::{fmt, path::PathBuf, str::FromStr};
2
3use url::Url;
4
5pub(crate) const KEYMASTER_CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd";
6pub(crate) const ANDROID_CLIENT_ID: &str = "9a8d2f0ce77a4e248bb71fefcb557637";
7pub(crate) const IOS_CLIENT_ID: &str = "58bd3c95768941ea9eb4350aaa033eb3";
8
9#[derive(Clone, Debug)]
10pub struct SessionConfig {
11    pub client_id: String,
12    pub device_id: String,
13    pub proxy: Option<Url>,
14    pub ap_port: Option<u16>,
15    pub tmp_dir: PathBuf,
16    pub autoplay: Option<bool>,
17}
18
19impl SessionConfig {
20    pub(crate) fn default_for_os(os: &str) -> Self {
21        let device_id = uuid::Uuid::new_v4().as_hyphenated().to_string();
22        let client_id = match os {
23            "android" => ANDROID_CLIENT_ID,
24            "ios" => IOS_CLIENT_ID,
25            _ => KEYMASTER_CLIENT_ID,
26        }
27        .to_owned();
28
29        Self {
30            client_id,
31            device_id,
32            proxy: None,
33            ap_port: None,
34            tmp_dir: std::env::temp_dir(),
35            autoplay: None,
36        }
37    }
38}
39
40impl Default for SessionConfig {
41    fn default() -> Self {
42        Self::default_for_os(std::env::consts::OS)
43    }
44}
45
46#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
47pub enum DeviceType {
48    Unknown = 0,
49    Computer = 1,
50    Tablet = 2,
51    Smartphone = 3,
52    Speaker = 4,
53    Tv = 5,
54    Avr = 6,
55    Stb = 7,
56    AudioDongle = 8,
57    GameConsole = 9,
58    CastAudio = 10,
59    CastVideo = 11,
60    Automobile = 12,
61    Smartwatch = 13,
62    Chromebook = 14,
63    UnknownSpotify = 100,
64    CarThing = 101,
65    Observer = 102,
66    HomeThing = 103,
67}
68
69impl FromStr for DeviceType {
70    type Err = ();
71    fn from_str(s: &str) -> Result<Self, Self::Err> {
72        use self::DeviceType::*;
73        match s.to_lowercase().as_ref() {
74            "computer" => Ok(Computer),
75            "tablet" => Ok(Tablet),
76            "smartphone" => Ok(Smartphone),
77            "speaker" => Ok(Speaker),
78            "tv" => Ok(Tv),
79            "avr" => Ok(Avr),
80            "stb" => Ok(Stb),
81            "audiodongle" => Ok(AudioDongle),
82            "gameconsole" => Ok(GameConsole),
83            "castaudio" => Ok(CastAudio),
84            "castvideo" => Ok(CastVideo),
85            "automobile" => Ok(Automobile),
86            "smartwatch" => Ok(Smartwatch),
87            "chromebook" => Ok(Chromebook),
88            "carthing" => Ok(CarThing),
89            "homething" => Ok(HomeThing),
90            _ => Err(()),
91        }
92    }
93}
94
95impl From<&DeviceType> for &str {
96    fn from(d: &DeviceType) -> &'static str {
97        use self::DeviceType::*;
98        match d {
99            Unknown => "Unknown",
100            Computer => "Computer",
101            Tablet => "Tablet",
102            Smartphone => "Smartphone",
103            Speaker => "Speaker",
104            Tv => "TV",
105            Avr => "AVR",
106            Stb => "STB",
107            AudioDongle => "AudioDongle",
108            GameConsole => "GameConsole",
109            CastAudio => "CastAudio",
110            CastVideo => "CastVideo",
111            Automobile => "Automobile",
112            Smartwatch => "Smartwatch",
113            Chromebook => "Chromebook",
114            UnknownSpotify => "UnknownSpotify",
115            CarThing => "CarThing",
116            Observer => "Observer",
117            HomeThing => "HomeThing",
118        }
119    }
120}
121
122impl From<DeviceType> for &str {
123    fn from(d: DeviceType) -> &'static str {
124        (&d).into()
125    }
126}
127
128impl fmt::Display for DeviceType {
129    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130        let str: &str = self.into();
131        f.write_str(str)
132    }
133}
134
135impl Default for DeviceType {
136    fn default() -> DeviceType {
137        DeviceType::Speaker
138    }
139}