lavalink_rs/
lib.rs

1#![allow(clippy::type_complexity)]
2#![allow(rustdoc::bare_urls)]
3
4#[cfg(not(feature = "python"))]
5#[macro_use]
6extern crate tracing;
7
8#[cfg(feature = "python")]
9#[macro_use]
10extern crate log;
11
12#[cfg(feature = "python")]
13#[macro_use]
14extern crate macro_rules_attribute;
15
16#[macro_use]
17extern crate serde;
18
19#[cfg(feature = "_rustls-tls")]
20pub(crate) type HttpsConnector =
21    hyper_rustls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>;
22
23#[cfg(feature = "_native-tls")]
24pub(crate) type HttpsConnector =
25    hyper_tls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>;
26
27/// The main client, where everything gets done.
28pub mod client;
29/// Every possible error that the library can return.
30pub mod error;
31/// The REST API.
32pub mod http;
33/// Mappings of objects received or sent from or to the API.
34pub mod model;
35/// A Lavalink server connection.
36pub mod node;
37/// Player related methods.
38pub mod player_context;
39/// Re-exports of all the most common types.
40pub mod prelude;
41/// Macros that abstract annoying stuff.
42#[cfg(feature = "macros")]
43pub mod macros {
44    /// A macro that transforms `async` functions (and closures) into plain functions, whose return
45    /// type is a boxed [`Future`].
46    ///
47    /// [`Future`]: std::future::Future
48    pub use macros_dep::hook;
49}
50
51#[cfg(feature = "macros")]
52/// A macro that transforms `async` functions (and closures) into plain functions, whose return
53/// type is a boxed [`Future`].
54///
55/// [`Future`]: std::future::Future
56pub use macros::hook;
57
58#[cfg(feature = "python")]
59use pyo3::{prelude::*, types::PyDict, wrap_pymodule};
60
61#[cfg(feature = "python")]
62mod python;
63
64#[cfg(feature = "python")]
65#[pymodule]
66fn lavalink_rs(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
67    let handle = pyo3_log::Logger::new(py, pyo3_log::Caching::LoggersAndLevels)?
68        .filter(log::LevelFilter::Trace)
69        .install()
70        .expect("Someone installed a logger before lavalink_rs.");
71
72    // Some time in the future when logging changes, reset the caches:
73    handle.reset();
74
75    m.add_class::<client::LavalinkClient>()?;
76    m.add_class::<player_context::PlayerContext>()?;
77
78    m.add_class::<python::event::EventHandler>()?;
79    m.add_class::<python::http::Http>()?;
80    m.add_class::<python::node::Node>()?;
81    m.add_class::<node::NodeBuilder>()?;
82    m.add_class::<python::model::client::NodeDistributionStrategyPy>()?;
83    m.add_class::<player_context::TrackInQueue>()?;
84    m.add_class::<player_context::QueueRef>()?;
85
86    m.add_class::<model::UserId>()?;
87    m.add_class::<model::ChannelId>()?;
88    m.add_class::<model::GuildId>()?;
89
90    m.add_wrapped(wrap_pymodule!(python::model::model))?;
91
92    let sys = PyModule::import(py, "sys")?;
93    let raw_modules = sys.getattr("modules")?;
94    let sys_modules: &Bound<'_, PyDict> = raw_modules.downcast()?;
95    sys_modules.set_item("lavalink_rs.model", m.getattr("model")?)?;
96
97    Ok(())
98}