mod conversions_to_wit;
#[cfg(not(with_testing))]
mod runtime;
#[cfg(with_testing)]
mod test_runtime;
#[doc(hidden)]
pub mod wit;
#[cfg(not(with_testing))]
pub use self::runtime::ServiceRuntime;
#[cfg(with_testing)]
pub use self::test_runtime::MockServiceRuntime;
#[doc(hidden)]
pub use self::wit::export_service;
use crate::util::BlockingWait as _;
#[cfg(with_testing)]
pub type ServiceRuntime<Application> = MockServiceRuntime<Application>;
#[macro_export]
macro_rules! service {
($service:ident) => {
#[doc(hidden)]
static mut SERVICE: Option<$service> = None;
$crate::export_service!($service with_types_in $crate::service::wit);
impl $crate::service::wit::exports::linera::app::service_entrypoints::Guest for $service {
fn handle_query(argument: Vec<u8>) -> Vec<u8> {
use $crate::util::BlockingWait as _;
$crate::ServiceLogger::install();
let request = $crate::serde_json::from_slice(&argument)
.unwrap_or_else(|_| panic!("Query {argument:?} is invalid and could not be deserialized"));
let response = $crate::service::run_async_entrypoint(
unsafe { &mut SERVICE },
move |service| service.handle_query(request).blocking_wait(),
);
$crate::serde_json::to_vec(&response)
.expect("Failed to serialize query response")
}
}
#[cfg(not(target_arch = "wasm32"))]
fn main() {}
};
}
pub fn run_async_entrypoint<Service, Output>(
service: &mut Option<Service>,
entrypoint: impl FnOnce(&mut Service) -> Output,
) -> Output
where
Service: crate::Service,
{
let service =
service.get_or_insert_with(|| Service::new(ServiceRuntime::new()).blocking_wait());
entrypoint(service)
}