mstickerlib/
lib.rs

1#![allow(clippy::tabs_in_doc_comments)]
2#![warn(unreachable_pub)]
3#![cfg_attr(all(doc, nightly), feature(doc_auto_cfg))]
4
5//! **WARINING: this crate is unstable und still have many anti-patterns**
6
7pub mod database;
8pub mod error;
9pub mod image;
10pub mod matrix;
11pub mod tg;
12#[cfg(feature = "ffmpeg")]
13mod video;
14
15use std::sync::OnceLock;
16
17struct Client(OnceLock<reqwest::Client>);
18static CLIENT: Client = Client(OnceLock::new());
19
20impl Client {
21	fn get(&self) -> &'static reqwest::Client {
22		if let Some(value) = CLIENT.0.get() {
23			return value;
24		};
25		set_client(reqwest::Client::default()).ok();
26		CLIENT.0.get().unwrap() //now it must be set
27	}
28}
29
30/// set the crate wide [reqwest::Client].
31/// This function should be called before performing any other interaction with this create.
32/// Otherwise the client can not be set anymore and an error will be return.
33/// If this function is not called, the client will be automaticly initialize with [reqwest::Client::default]
34pub fn set_client(client: reqwest::Client) -> Result<(), ()> {
35	init();
36	CLIENT.0.set(client).map_err(|_| ())
37}
38
39pub fn get_client() -> &'static reqwest::Client {
40	CLIENT.get()
41}
42
43// XXX Hacky: We abuse the fact that HTTP client will always be needed before ffmpeg.
44fn init() {
45	#[cfg(feature = "ffmpeg")]
46	{
47		static GUARD: OnceLock<()> = OnceLock::new();
48		// from doc: "Returns Ok(()) if the cell’s value was set by this call."
49		// so init will only be called once
50		if GUARD.set(()).is_ok() {
51			ffmpeg::init().expect("Failed to initialise ffmpeg");
52		}
53	}
54}