diff --git a/example/src-tauri/src/main.rs b/example/src-tauri/src/main.rs index 8c9bf50..7a0b71d 100644 --- a/example/src-tauri/src/main.rs +++ b/example/src-tauri/src/main.rs @@ -62,7 +62,7 @@ fn main() { nested::some_struct ]) .with_events(tauri_specta::collect_events![DemoEvent, EmptyEvent]) - .build_plugin(), + .to_plugin(), ) .setup(|app| { let handle = app.handle(); diff --git a/src/lib.rs b/src/lib.rs index b3aacaf..de5a22a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -317,6 +317,8 @@ where phantom: PhantomData, } +const PLUGIN_NAME: &str = "tauri-specta"; + impl Exporter where TLang: ExportLanguage, @@ -324,12 +326,10 @@ where TEvents: EventsTypeState, { #[must_use] - pub fn build_plugin(self) -> tauri::plugin::TauriPlugin { - const PLUGIN_NAME: &str = "tauri-specta"; - + pub fn to_plugin(self) -> tauri::plugin::TauriPlugin { let builder = tauri::plugin::Builder::new(PLUGIN_NAME); - let plugin_utils = self.build_plugin_utils(PLUGIN_NAME); + let plugin_utils = self.to_utils_for_plugin(PLUGIN_NAME); builder .invoke_handler(plugin_utils.invoke_handler) @@ -342,7 +342,7 @@ where } #[must_use] - pub fn build_plugin_utils( + pub fn to_utils_for_plugin( mut self, plugin_name: &'static str, ) -> PluginUtils @@ -419,43 +419,52 @@ where type HardcodedRuntime = tauri::Wry; -impl Exporter +impl Exporter where TLang: ExportLanguage, TCommands: CommandsTypeState, + TEvents: EventsTypeState, { /// Exports the output of [`internal::render`] for a collection of [`FunctionDataType`] into a TypeScript file. pub fn export(self) -> Result<(), TsExportError> { + self.export_for_plugin(PLUGIN_NAME) + } + + pub fn export_for_plugin(mut self, plugin_name: &'static str) -> Result<(), TsExportError> { + self.cfg.get_or_insert_with(Default::default).plugin_name = PluginName::new(plugin_name); + self.export_inner().map(|_| ()) } } -#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub(crate) struct PluginName(pub Option<&'static str>); +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub(crate) struct PluginName(&'static str); pub(crate) enum ItemType { Event, Command, } +impl Default for PluginName { + fn default() -> Self { + PluginName(PLUGIN_NAME) + } +} + impl PluginName { pub fn new(plugin_name: &'static str) -> Self { - Self(Some(plugin_name)) + Self(plugin_name) } pub fn apply_as_prefix(&self, s: &str, item_type: ItemType) -> String { - match &self.0 { - Some(plugin_name) => { - format!( - "plugin:{plugin_name}{}{}", - match item_type { - ItemType::Event => ":", - ItemType::Command => "|", - }, - s, - ) - } - None => s.to_string(), - } + format!( + "plugin:{}{}{}", + self.0, + match item_type { + ItemType::Event => ":", + ItemType::Command => "|", + }, + s, + ) } }