diff --git a/lib/Cargo.toml b/lib/Cargo.toml index eef84bb9ef..59dcc6ca96 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -17,10 +17,14 @@ license = "MIT" [features] openssl = ["hyper-tls"] rustls = ["hyper-rustls"] -default = ["openssl"] +tokio-runtime = ["tokio"] +sidevm-runtime = ["sidevm"] +default = ["openssl", "tokio-runtime"] + [dependencies] bytes = "1.0.1" -tokio = { version = "1.2", features = ["fs", "rt"]} +tokio = { version = "1.2", features = ["fs", "rt"], optional = true } +sidevm = { version = "0.1", optional = true } tracing = "0.1.23" tracing-futures = "0.2" @@ -32,6 +36,7 @@ hyper = { version = "0.14", features = ["client", "http1"] } hyper-tls = { version = "0.5", optional = true } futures = "0.3" hyper-rustls = { version = "0.22", optional = true } + [dev-dependencies] tracing-subscriber = "0.2.15" tokio = { version = "1.2", features = ["macros", "time", "fs", "rt-multi-thread"] } diff --git a/lib/src/api.rs b/lib/src/api.rs index 07c2207664..d6c4028dc9 100644 --- a/lib/src/api.rs +++ b/lib/src/api.rs @@ -5,7 +5,10 @@ use std::sync::{ use std::time::Duration; use futures::{Future, FutureExt}; -use tokio::time::timeout; +#[cfg(feature = "sidevm-runtime")] +use sidevm::{spawn, time::timeout}; +#[cfg(feature = "tokio-runtime")] +use tokio::{spawn, time::timeout}; use tracing_futures::Instrument; use telegram_bot_raw::{HttpRequest, Request, ResponseType}; @@ -94,7 +97,7 @@ impl Api { pub fn spawn(&self, request: Req) { let api = self.clone(); if let Ok(request) = request.serialize() { - tokio::spawn(async move { + spawn(async move { let _ = api.send_http_request::(request).await; }); } diff --git a/lib/src/connector/hyper.rs b/lib/src/connector/hyper.rs index f6d82ef8f3..181e8f0203 100644 --- a/lib/src/connector/hyper.rs +++ b/lib/src/connector/hyper.rs @@ -1,4 +1,5 @@ use std::io::{Cursor, Read}; +#[cfg(feature = "tokio-runtime")] use std::path::Path; use std::pin::Pin; use std::str::FromStr; @@ -76,6 +77,11 @@ impl Connector for MultipartValue::Text(text) => { fields.push((key, MultipartTemporaryValue::Text(text))) } + #[cfg(feature = "sidevm-runtime")] + MultipartValue::Path { .. } => { + return Err(ErrorKind::InvalidMultipartFilename.into()); + } + #[cfg(feature = "tokio-runtime")] MultipartValue::Path { file_name, path } => { let file_name = file_name .or_else(|| { @@ -157,6 +163,7 @@ impl Connector for } } +#[cfg(feature = "tokio-runtime")] pub fn default_connector() -> Result, Error> { #[cfg(feature = "rustls")] let connector = HttpsConnector::with_native_roots(); @@ -168,3 +175,12 @@ pub fn default_connector() -> Result, Error> { Client::builder().build(connector), ))) } + +#[cfg(feature = "sidevm-runtime")] +pub fn default_connector() -> Result, Error> { + Ok(Box::new(HyperConnector::new( + Client::builder() + .executor(sidevm::exec::HyperExecutor) + .build(sidevm::net::HttpConnector), + ))) +}