lavalink_rs/python/model/
mod.rs

1pub mod client;
2pub mod events;
3pub mod http;
4pub mod player;
5pub mod search;
6pub mod track;
7
8use pyo3::prelude::*;
9use pyo3::types::PyDict;
10use pyo3::wrap_pymodule;
11
12#[pymodule]
13pub fn model(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
14    m.add_class::<crate::model::UserId>()?;
15    m.add_class::<crate::model::GuildId>()?;
16    m.add_class::<crate::model::ChannelId>()?;
17
18    self::client::client(py, m)?;
19    m.add_wrapped(wrap_pymodule!(self::events::events))?;
20    self::http::http(py, m)?;
21    self::player::player(py, m)?;
22    m.add_wrapped(wrap_pymodule!(self::search::search))?;
23    m.add_wrapped(wrap_pymodule!(self::track::track))?;
24
25    let sys = PyModule::import(py, "sys")?;
26    let raw_modules = sys.getattr("modules")?;
27    let sys_modules: &Bound<'_, PyDict> = raw_modules.downcast()?;
28
29    sys_modules.set_item("lavalink_rs.model.client", m.getattr("client")?)?;
30    sys_modules.set_item("lavalink_rs.model.events", m.getattr("events")?)?;
31    sys_modules.set_item("lavalink_rs.model.http", m.getattr("http")?)?;
32    sys_modules.set_item("lavalink_rs.model.player", m.getattr("player")?)?;
33    sys_modules.set_item("lavalink_rs.model.search", m.getattr("search")?)?;
34    sys_modules.set_item("lavalink_rs.model.track", m.getattr("track")?)?;
35
36    Ok(())
37}
38
39#[pymethods]
40impl crate::model::UserId {
41    #[new]
42    fn new_py(user_id: u64) -> Self {
43        user_id.into()
44    }
45
46    #[getter]
47    fn get_inner(&self) -> pyo3::PyResult<u64> {
48        Ok(self.0)
49    }
50
51    #[setter]
52    fn set_inner(&mut self, id: u64) -> pyo3::PyResult<()> {
53        self.0 = id;
54        Ok(())
55    }
56}
57
58#[pymethods]
59impl crate::model::GuildId {
60    #[new]
61    fn new_py(user_id: u64) -> Self {
62        user_id.into()
63    }
64
65    #[getter]
66    fn get_inner(&self) -> pyo3::PyResult<u64> {
67        Ok(self.0)
68    }
69
70    #[setter]
71    fn set_inner(&mut self, id: u64) -> pyo3::PyResult<()> {
72        self.0 = id;
73        Ok(())
74    }
75}
76
77#[pymethods]
78impl crate::model::ChannelId {
79    #[new]
80    fn new_py(channel_id: u64) -> Self {
81        channel_id.into()
82    }
83
84    #[getter]
85    fn get_inner(&self) -> pyo3::PyResult<u64> {
86        Ok(self.0)
87    }
88
89    #[setter]
90    fn set_inner(&mut self, id: u64) -> pyo3::PyResult<()> {
91        self.0 = id;
92        Ok(())
93    }
94}
95
96#[derive(FromPyObject)]
97pub enum PyUserId {
98    #[pyo3(transparent, annotation = "UserId")]
99    UserId(crate::model::UserId),
100    #[pyo3(transparent, annotation = "int")]
101    Int(u64),
102}
103
104impl Into<crate::model::UserId> for PyUserId {
105    fn into(self) -> crate::model::UserId {
106        match self {
107            Self::UserId(x) => x,
108            Self::Int(x) => x.into(),
109        }
110    }
111}
112
113#[derive(FromPyObject)]
114pub enum PyGuildId {
115    #[pyo3(transparent, annotation = "GuildId")]
116    GuildId(crate::model::GuildId),
117    #[pyo3(transparent, annotation = "int")]
118    Int(u64),
119}
120
121impl Into<crate::model::GuildId> for PyGuildId {
122    fn into(self) -> crate::model::GuildId {
123        match self {
124            Self::GuildId(x) => x,
125            Self::Int(x) => x.into(),
126        }
127    }
128}
129
130#[derive(FromPyObject)]
131pub enum PyChannelId {
132    #[pyo3(transparent, annotation = "ChannelId")]
133    ChannelId(crate::model::ChannelId),
134    #[pyo3(transparent, annotation = "int")]
135    Int(u64),
136}
137
138impl Into<crate::model::ChannelId> for PyChannelId {
139    fn into(self) -> crate::model::ChannelId {
140        match self {
141            Self::ChannelId(x) => x,
142            Self::Int(x) => x.into(),
143        }
144    }
145}