The error occurs because the function init_log_plugin() is returning a Builder, but you're calling the .build() method inside the function, which actually returns a Plugin. This mismatch between the return type in the function signature and the actual return value is causing the compilation error.
Here's how you can fix it:
Change the return type of the init_log_plugin() function to match the plugin type.
In your log_app.rs, modify the function signature to return the built plugin instead of the builder. Here's the corrected code:
use tauri_plugin_log::{Builder, Target, TargetKind, TimezoneStrategy};
use tauri::plugin::Plugin;
pub fn init_log_plugin() -> impl Plugin<tauri::Wry> {
let exe_dir = std::env::current_exe()
.expect("Erro ao obter o diretório do executável")
.parent()
.expect("Erro ao obter o diretório pai do executável")
.to_path_buf();
let log_file_name = "rtales".to_string();
Builder::default()
.target(
Target::new(
TargetKind::Folder {
path: exe_dir.clone(),
file_name: Some(log_file_name),
}
)
)
.timezone_strategy(TimezoneStrategy::UseLocal)
.build() // This returns a Plugin, not a Builder
}
By changing the return type to impl Plugin<tauri::Wry>, you're specifying that the function returns a type that implements the Plugin trait, which is what the .plugin() method expects.
Explanation:
- The
.plugin() method expects an argument that implements the Plugin trait for the specified runtime (tauri::Wry in this case).
- The
Builder is used to configure the plugin, but it's the .build() method that actually creates the plugin.
- By returning the built plugin from
init_log_plugin(), you ensure that the types align correctly.
Updated build.rs:
With the corrected init_log_plugin() function, your build.rs (or main.rs) remains the same:
mod log_app;
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.plugin(log_app::init_log_plugin())
.setup(|app| {
#[cfg(all(desktop))]
{
let handle = app.handle();
tray::create_tray(handle)?;
log::warn!(target: "TrayIcon", "TestError");
log::info!(target: "TrayIcon", "Aplicação configurada com sucesso!");
}
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running application");
}
Summary:
- Ensure that the return type of your function matches what you're actually returning.
- When building plugins with a builder pattern, the
.build() method typically returns the final plugin.
- The
.plugin() method in Tauri expects an object that implements the Plugin trait for the appropriate runtime.
By making these changes, your code should compile successfully without the error[E0277].
Additional Note:
If you prefer to return the Builder from init_log_plugin() and call .build() in your build.rs, you can modify your code like this:
In log_app.rs:
use tauri_plugin_log::{Builder, Target, TargetKind, TimezoneStrategy};
pub fn init_log_plugin() -> Builder {
// ... your existing configuration ...
Builder::default()
.target(
Target::new(
TargetKind::Folder {
path: exe_dir.clone(),
file_name: Some(log_file_name),
}
)
)
.timezone_strategy(TimezoneStrategy::UseLocal)
}
In build.rs:
.plugin(log_app::init_log_plugin().build())
But generally, it's cleaner to build the plugin within the init_log_plugin() function.