nagisa_onebot/adapter/
config.rs1use serde_json::Value;
3use std::net::SocketAddr;
4use std::sync::Arc;
5use std::time::Duration;
6
7#[derive(Clone, Debug)]
11pub enum OneBotTransport {
12 Forward { url: String },
14 ReverseWs { bind: SocketAddr, path: String },
16 Http { api_url: String, post_bind: SocketAddr, post_path: String, secret: Option<String> },
19 HttpApi { api_url: String },
24 LLOneBotHttp { api_url: String, events: LLOneBotEventMode },
28}
29
30#[derive(Clone, Debug)]
32pub enum LLOneBotEventMode {
33 Sse,
35 LongPoll { interval: Duration },
37}
38
39#[derive(Clone, Debug)]
42pub struct OneBotConfig {
43 pub access_token: Option<String>,
44 pub mode: OneBotTransport,
46}
47
48impl OneBotConfig {
49 pub fn new(url: impl Into<String>) -> Self {
51 OneBotConfig { access_token: None, mode: OneBotTransport::Forward { url: url.into() } }
52 }
53 pub fn reverse_ws(bind: SocketAddr, path: impl Into<String>) -> Self {
55 OneBotConfig { access_token: None, mode: OneBotTransport::ReverseWs { bind, path: path.into() } }
56 }
57 pub fn http(api_url: impl Into<String>, post_bind: SocketAddr, post_path: impl Into<String>) -> Self {
59 OneBotConfig {
60 access_token: None,
61 mode: OneBotTransport::Http {
62 api_url: api_url.into(),
63 post_bind,
64 post_path: post_path.into(),
65 secret: None,
66 },
67 }
68 }
69 pub fn http_api(api_url: impl Into<String>) -> Self {
72 OneBotConfig { access_token: None, mode: OneBotTransport::HttpApi { api_url: api_url.into() } }
73 }
74 pub fn llonebot_http_sse(api_url: impl Into<String>) -> Self {
77 OneBotConfig {
78 access_token: None,
79 mode: OneBotTransport::LLOneBotHttp { api_url: api_url.into(), events: LLOneBotEventMode::Sse },
80 }
81 }
82 pub fn llonebot_http_long_poll(api_url: impl Into<String>, interval: Duration) -> Self {
85 OneBotConfig {
86 access_token: None,
87 mode: OneBotTransport::LLOneBotHttp {
88 api_url: api_url.into(),
89 events: LLOneBotEventMode::LongPoll { interval },
90 },
91 }
92 }
93 pub fn with_token(mut self, token: impl Into<String>) -> Self {
94 self.access_token = Some(token.into());
95 self
96 }
97 pub fn with_secret(mut self, secret: impl Into<String>) -> Self {
99 if let OneBotTransport::Http { secret: s, .. } = &mut self.mode {
100 *s = Some(secret.into());
101 }
102 self
103 }
104}
105
106pub type QuickOpFn = Arc<dyn Fn(&nagisa_types::event::Event) -> Option<Value> + Send + Sync>;
110
111pub(crate) fn encode_query(s: &str) -> String {
114 let mut out = String::with_capacity(s.len());
115 for b in s.bytes() {
116 match b {
117 b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => out.push(b as char),
118 _ => out.push_str(&format!("%{b:02X}")),
119 }
120 }
121 out
122}