#Dependency injection for my command arg ``handle: AppHandle`` does not work
57 messages · Page 1 of 1 (latest)
this looks correct, what error are you getting?
in my frontend when i run my application it says the command translate does not exist
i invoked it by
plugin:name|translate but still
heres the repo
might it be that this can be a problem when i have a plugin which i load as a dependency from cargo toml using path option
but the schema didnt complain about
translator-bindings:default
i even had auto completions
well, this shouldn't be commented out for sure
I think permissions issues are reported clearly as missing/incorrect permissions. if the error is about command it usually just not registering it correctly
#[tauri::command(rename_all = "snake_case")]
pub async fn translate(opts: gtranslate::TranslateOptions<'_>, handle: tauri::AppHandle) -> Result<(), AppError> {
let translated= handle.translator_bindings().lock().await.translator.translate(
Duration::from_secs(5), // todo
opts
).await.map_err(|err| {
AppError::WrapError { wrapped: err.to_string() }
})?;
// todo stuff
println!("{translated}");
Ok(())
}
the trait bound `TranslateOptions<'_>: CommandArg<'_, R>` is not satisfied
the following other types implement trait `Deserialize<'de>`:
`&'a [u8]` implements `Deserialize<'de>`
`&'a serde_json::raw::RawValue` implements `Deserialize<'de>`
`&'a std::path::Path` implements `Deserialize<'de>`
`&'a str` implements `Deserialize<'de>`
`&'p jsonptr::pointer::Pointer` implements `Deserialize<'de>`
`()` implements `Deserialize<'de>`
`(T,)` implements `Deserialize<'de>`
`(T0, T1)` implements `Deserialize<'de>`
and 301 others
required for `TranslateOptions<'_>` to implement `CommandArg<'_, R>`rustcClick for full compiler diagnostic
lib.rs(39, 21): Error originated from macro call here
so everything in my args needs to be serializable?
oh bruh
btw im just dumb it shouldve been obvious
yes but that's usually quite simple. is TranslateOptions your object?
its from another crate
so im making a wrapper around it and implement deserialize manually
pub struct TranslationOpts<'a> {
pub opts: gtranslate::TranslateOptions<'a>
}
impl std::fmt::Debug for TranslationOpts<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"client: {}\nsource_lang: {}\ntarget_lang: {}\ndst_target: {}\nquery: {}\n",
self.opts.client,
self.opts.source_lang,
self.opts.target_lang,
self.opts.dst_target,
self.opts.query
)
}
}
struct StrVisitor;
impl<'de> Visitor<'de> for StrVisitor {
type Value = TranslationOpts<'de>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a str")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>, {
let mut translation_opts = TranslationOpts::<'de>{
opts: TranslateOptions{
client: Default::default(),
source_lang: Default::default(),
target_lang: Default::default(),
dst_target: Default::default(),
query: Default::default(),
}
};
while let Some((key, value)) = map.next_entry::<&'de str, &'de str>().expect("er gaat wat mis") {
match key {
"client" => translation_opts.opts.client = value,
"source_lang" => translation_opts.opts.source_lang = value,
"target_lang" => translation_opts.opts.target_lang = value,
"dst_target" => translation_opts.opts.dst_target = value,
"query" => translation_opts.opts.query = value,
_ => return Err(serde::de::Error::custom("translation options misses fields"))
}
}
Ok(translation_opts)
}
}
impl<'de> Deserialize<'de> for TranslationOpts<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de> {
Ok(deserializer.deserialize_map(StrVisitor).expect("hier faalt ie al"))
}
}
ive implemented deserializer
#[tauri::command(rename_all = "snake_case")]
pub async fn translate(translations_options: TranslationOpts<'_>, handle: tauri::AppHandle) -> Result<(), AppError> {
let translated= handle.translator_bindings().lock().await.translator.translate(
Duration::from_secs(5), // todo
translations_options.opts
).await.map_err(|err| {
AppError::WrapError { wrapped: err.to_string() }
})?;
// todo stuff
println!("{translated}");
Ok(())
}
command looks like dis
currently its complaining about my appHandle
Dependency injection for my command arg handle: AppHandle does not work
change the name of the arg to app_handle
the name shouldn't matter, the injection logic checks for the type, not the name
in my command I have fn command<R: Runtime>( app: AppHandle<R>,
ty
bro we came up to the same conclusion at the same time thats cool ngl
oke the cool part is things are working
but i dont think my capabilities and permissions were wrong
i think im strugglign to hard because i just wanted my plugin to hook commands into my app instead of doing it from my app which wouldve been easier i guess
this is just a warning
ive never done something with ipc customization idk where that error comes from
oh wait nvm it works
there is just a problem the way im passing arguments in frontend i need to fix
anyways thnx yall
no problem, I hope the rest will work. plugins are tricky
ye it works perfect this error is just related to my serializer and my incorrect argument passage in frontend
made adjustments
works perfect
bro Tauri is lit most of my struggles were because of not reading the docs properly
like its literally obvious that anything my commands return should impl Serialize and anything i pass as arguments need to be Deseriazable
and one of my other struggles were related to async runtime, i was using sync::Mutex instead of an async one, and Future's require to be both Send and Sync so when control is yielded to the async runtime it may resume at a diifferent thread/core, but i was clearly blocking both the main thread, and sync::Mutex's MutexGuard is not Send so i had to use tokio's async mutex
im so happy rn