#![warn(missing_docs)]
mod context;
mod error;
mod event;
mod handler;
mod types;
mod window;
pub(crate) use context::*;
pub use error::*;
pub use event::*;
pub use types::*;
pub use window::*;
use std::{
ffi::{c_char, c_void, CStr, CString},
panic::{catch_unwind, AssertUnwindSafe},
};
use webui_sys::*;
use crate::handler::LoggerHandler;
pub fn exit() {
unsafe { webui_exit() }
}
pub fn wait() {
unsafe { webui_wait() }
}
pub fn wait_async() -> bool {
unsafe { webui_wait_async() }
}
pub fn open_url(url: &str) {
let url = CString::new(url).unwrap();
unsafe { webui_open_url(url.as_ptr()) }
}
pub(crate) fn set_config(option: Config, status: bool) {
unsafe {
webui_set_config(option as _, status);
}
}
pub fn set_show_wait_connection(status: bool) {
set_config(Config::ShowWaitConnection, status);
}
pub fn set_ui_event_blocking(status: bool) {
set_config(Config::UiEventBlocking, status);
}
pub fn set_folder_monitor(status: bool) {
set_config(Config::FolderMonitor, status);
}
pub fn set_multi_client(status: bool) {
set_config(Config::MultiClient, status);
}
pub fn set_use_cookies(status: bool) {
set_config(Config::UseCookies, status);
}
pub fn set_asynchronous_response(status: bool) {
set_config(Config::AsynchronousResponse, status);
}
pub fn set_default_root_folder(path: &str) -> Result<(), WebUIError> {
let path = CString::new(path).unwrap();
let result = unsafe { webui_set_default_root_folder(path.as_ptr()) };
WebUIError::from_bool(result)
}
pub fn get_mime_type(file: &str) -> String {
let file = CString::new(file).unwrap();
unsafe {
let mine_type = webui_get_mime_type(file.as_ptr());
CStr::from_ptr(mine_type).to_string_lossy().to_string()
}
}
pub fn browser_exist(browser: Browser) -> bool {
unsafe { webui_browser_exist(browser as _) }
}
pub fn set_browser_folder(path: &str) {
let path = CString::new(path).unwrap();
unsafe { webui_set_browser_folder(path.as_ptr()) }
}
pub fn delete_all_profiles() {
unsafe { webui_delete_all_profiles() }
}
pub fn get_port() -> usize {
unsafe { webui_get_free_port() }
}
pub fn encode(text: &str) -> String {
let text = CString::new(text).unwrap();
let encoded = unsafe { webui_encode(text.as_ptr()) };
let result = unsafe { CStr::from_ptr(encoded as _).to_string_lossy().to_string() };
unsafe { webui_free(encoded as _) };
result
}
pub fn decode(text: &str) -> String {
let text = CString::new(text).unwrap();
let decoded = unsafe { webui_decode(text.as_ptr()) };
let result = unsafe { CStr::from_ptr(decoded as _).to_string_lossy().to_string() };
unsafe { webui_free(decoded as _) };
result
}
pub fn set_tls_certificate(certificate_pem: &str, private_key_pem: &str) -> Result<(), WebUIError> {
let certificate_pem = CString::new(certificate_pem).unwrap();
let private_key_pem = CString::new(private_key_pem).unwrap();
let result = unsafe { webui_set_tls_certificate(certificate_pem.as_ptr(), private_key_pem.as_ptr()) };
WebUIError::from_bool(result)
}
pub fn set_logger<F>(callback: F)
where
F: LoggerHandler,
{
extern "C" fn shim(level: usize, log: *const c_char, user_data: *mut c_void) {
let level: LoggerLevel = unsafe { std::mem::transmute(level) };
let log = unsafe { CStr::from_ptr(log).to_string_lossy().to_string() };
let callback = unsafe { &*(user_data as *mut Box<dyn LoggerHandler>) };
let _ = catch_unwind(AssertUnwindSafe(|| callback(level, &log)));
}
let user_data: Box<dyn LoggerHandler> = Box::new(callback);
let user_data = Box::into_raw(Box::new(user_data));
unsafe {
webui_set_logger(Some(shim), user_data as _);
}
}
pub unsafe fn clean() {
webui_clean();
}
pub(crate) fn get_last_error_number() -> usize {
unsafe { webui_get_last_error_number() }
}
pub(crate) fn get_last_error_message() -> String {
unsafe {
let message = webui_get_last_error_message();
CStr::from_ptr(message).to_string_lossy().to_string()
}
}