lavalink_rs/python/model/
mod.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
pub mod client;
pub mod events;
pub mod http;
pub mod player;
pub mod search;
pub mod track;

use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::wrap_pymodule;

#[pymodule]
pub fn model(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<crate::model::UserId>()?;
    m.add_class::<crate::model::GuildId>()?;
    m.add_class::<crate::model::ChannelId>()?;

    self::client::client(py, m)?;
    m.add_wrapped(wrap_pymodule!(self::events::events))?;
    self::http::http(py, m)?;
    self::player::player(py, m)?;
    m.add_wrapped(wrap_pymodule!(self::search::search))?;
    m.add_wrapped(wrap_pymodule!(self::track::track))?;

    let sys = PyModule::import(py, "sys")?;
    let raw_modules = sys.getattr("modules")?;
    let sys_modules: &Bound<'_, PyDict> = raw_modules.downcast()?;

    sys_modules.set_item("lavalink_rs.model.client", m.getattr("client")?)?;
    sys_modules.set_item("lavalink_rs.model.events", m.getattr("events")?)?;
    sys_modules.set_item("lavalink_rs.model.http", m.getattr("http")?)?;
    sys_modules.set_item("lavalink_rs.model.player", m.getattr("player")?)?;
    sys_modules.set_item("lavalink_rs.model.search", m.getattr("search")?)?;
    sys_modules.set_item("lavalink_rs.model.track", m.getattr("track")?)?;

    Ok(())
}

#[pymethods]
impl crate::model::UserId {
    #[new]
    fn new_py(user_id: u64) -> Self {
        user_id.into()
    }

    #[getter]
    fn get_inner(&self) -> pyo3::PyResult<u64> {
        Ok(self.0)
    }

    #[setter]
    fn set_inner(&mut self, id: u64) -> pyo3::PyResult<()> {
        self.0 = id;
        Ok(())
    }
}

#[pymethods]
impl crate::model::GuildId {
    #[new]
    fn new_py(user_id: u64) -> Self {
        user_id.into()
    }

    #[getter]
    fn get_inner(&self) -> pyo3::PyResult<u64> {
        Ok(self.0)
    }

    #[setter]
    fn set_inner(&mut self, id: u64) -> pyo3::PyResult<()> {
        self.0 = id;
        Ok(())
    }
}

#[pymethods]
impl crate::model::ChannelId {
    #[new]
    fn new_py(channel_id: u64) -> Self {
        channel_id.into()
    }

    #[getter]
    fn get_inner(&self) -> pyo3::PyResult<u64> {
        Ok(self.0)
    }

    #[setter]
    fn set_inner(&mut self, id: u64) -> pyo3::PyResult<()> {
        self.0 = id;
        Ok(())
    }
}

#[derive(FromPyObject)]
pub enum PyUserId {
    #[pyo3(transparent, annotation = "UserId")]
    UserId(crate::model::UserId),
    #[pyo3(transparent, annotation = "int")]
    Int(u64),
}

impl Into<crate::model::UserId> for PyUserId {
    fn into(self) -> crate::model::UserId {
        match self {
            Self::UserId(x) => x,
            Self::Int(x) => x.into(),
        }
    }
}

#[derive(FromPyObject)]
pub enum PyGuildId {
    #[pyo3(transparent, annotation = "GuildId")]
    GuildId(crate::model::GuildId),
    #[pyo3(transparent, annotation = "int")]
    Int(u64),
}

impl Into<crate::model::GuildId> for PyGuildId {
    fn into(self) -> crate::model::GuildId {
        match self {
            Self::GuildId(x) => x,
            Self::Int(x) => x.into(),
        }
    }
}

#[derive(FromPyObject)]
pub enum PyChannelId {
    #[pyo3(transparent, annotation = "ChannelId")]
    ChannelId(crate::model::ChannelId),
    #[pyo3(transparent, annotation = "int")]
    Int(u64),
}

impl Into<crate::model::ChannelId> for PyChannelId {
    fn into(self) -> crate::model::ChannelId {
        match self {
            Self::ChannelId(x) => x,
            Self::Int(x) => x.into(),
        }
    }
}