veilid/veilid-cli/src/tools.rs

36 lines
1.0 KiB
Rust
Raw Normal View History

2023-06-08 18:07:09 +00:00
pub use cfg_if::*;
pub use log::*;
pub use parking_lot::*;
pub use veilid_tools::*;
2022-06-28 03:46:29 +00:00
use core::future::Future;
2023-06-08 18:07:09 +00:00
use core::str::FromStr;
2022-06-28 03:46:29 +00:00
cfg_if! {
if #[cfg(feature="rt-async-std")] {
pub use async_std::net::TcpStream;
pub fn block_on<F: Future<Output = T>, T>(f: F) -> T {
async_std::task::block_on(f)
}
} else if #[cfg(feature="rt-tokio")] {
pub use tokio::net::TcpStream;
pub fn block_on<F: Future<Output = T>, T>(f: F) -> T {
let rt = tokio::runtime::Runtime::new().unwrap();
let local = tokio::task::LocalSet::new();
local.block_on(&rt, f)
}
2023-08-29 20:15:47 +00:00
} else {
compile_error!("needs executor implementation")
2022-06-28 03:46:29 +00:00
}
}
2023-06-08 18:07:09 +00:00
pub fn json_str_u64(value: &json::JsonValue) -> u64 {
u64::from_str(value.as_str().unwrap_or_default()).unwrap_or_default()
}
pub fn json_str_vec_u8(value: &json::JsonValue) -> Vec<u8> {
data_encoding::BASE64URL_NOPAD
.decode(value.as_str().unwrap_or_default().as_bytes())
.unwrap_or_default()
}