#[cfg(feature = "build")]
pub mod build;
#[cfg(feature = "build")]
mod build_error;
mod compmd;
mod origin;
mod scheme;
use std::path::PathBuf;
use std::sync::Arc;
pub use compmd::Compmd;
pub use compmd_host;
use compmd_host::{Bundle, FolderStore, NoStore, Store};
use tauri::plugin::{Builder, TauriPlugin};
use tauri::{Manager, Runtime};
#[cfg(feature = "build")]
pub use build_error::BuildError;
#[macro_export]
macro_rules! include_bundle {
() => {
$crate::embedded(include!(concat!(env!("OUT_DIR"), "/compmd_bundle.rs")))
};
}
#[doc(hidden)]
#[must_use]
pub fn embedded(files: Option<&'static [(&'static str, &'static [u8])]>) -> Option<Bundle> {
files.map(|files| {
Bundle::embedded(files)
.unwrap_or_else(|error| panic!("the embedded bundle can't be served: {error}"))
})
}
#[must_use]
pub fn init<R: Runtime>(bundle: impl Into<Option<Bundle>>) -> TauriPlugin<R> {
let bundle = bundle.into();
let scheme = bundle.as_ref().map(|bundle| {
let scheme = bundle.manifest().scheme();
let scheme = scheme.unwrap_or_else(|| panic!("{}", BUILT_WITHOUT_SOURCE));
assert!(
origin::is_scheme(scheme),
"`{scheme}` isn't a scheme's name"
);
scheme.to_owned()
});
let origin = scheme.as_deref().map(origin::origin_of);
let mut plugin = Builder::new("compmd");
if let (Some(scheme), Some(origin)) = (&scheme, &origin) {
plugin = plugin
.js_init_script(origin::init_script(origin))
.register_uri_scheme_protocol(scheme.clone(), scheme::serve);
}
plugin
.setup(move |app, _api| {
let store = store_in(app.path().app_data_dir().ok());
app.manage(Compmd::new(bundle, origin, store));
Ok(())
})
.build()
}
fn store_in(data: Option<PathBuf>) -> Arc<dyn Store> {
data.map_or_else(
|| Arc::new(NoStore) as Arc<dyn Store>,
|data| Arc::new(FolderStore::new(data.join("compmd").join("packs"))),
)
}
pub(crate) const BUILT_WITHOUT_SOURCE: &str = "the bundle was built without a `source`, so it names no scheme: build it with the scheme it is served on, `compmd({ out, source: \"compmd://localhost/\" })`";
pub trait CompmdExt<R: Runtime> {
fn compmd(&self) -> &Compmd;
}
impl<R: Runtime, T: Manager<R>> CompmdExt<R> for T {
fn compmd(&self) -> &Compmd {
self.state::<Compmd>().inner()
}
}
#[cfg(test)]
mod tests;