#You must call `.manage()` before using this command

1 messages · Page 1 of 1 (latest)

fervent depot
#

Hi, I have some problems with state management, I have several states and there is no problem with them, but with this one (VaultMeta) there is and I don't understand why.

I've seen other discussions on this topic and haven't found a similar error myself.

Thank you for your answers

main.rs

mod vaults;

#[path ="./structs/vault_meta.rs"]
mod vault_meta;

fn main() {

    let vault_meta: VaultMeta = match vault_meta::load_or_init() {
        Ok(v) => v,
        Err(err) => {
            match err {
                ...
            }
            return ();
        }
    };

tauri::Builder::default()
  .manage(AppState { pass: Default::default() })
  .manage(app_config)
  .manage(vault_meta)
  .setup(|app| {
      create_directory_if_not_exist(app);
      Ok(())
  })
  .invoke_handler(tauri::generate_handler![
          vaults::import_vault,
          vaults::get_all_vaults,
          vaults::get_vault_by_name,
          vaults::create_new_vault,
      ])...
}

vaults.rs

#[tauri::command]
pub fn get_vault_by_name(file_name: String, config: tauri::State<AppConfig>, meta: tauri::State<VaultMeta>) {
    let mut path = config.vaults_path.lock().unwrap().to_path_buf();
    path.push(file_name);
    path.set_extension(&config.vault_extension);
    if path.exists() {
        let mut file = fs::read_to_string(path).unwrap();
        file = file.replace("\r", "");
        let lines = file.split("\n");
        for line in lines {
            println!("{:?}", line);
        }
    }
}

vault_meta.rs

#[derive(Serialize, Deserialize)]
pub struct VaultMeta {
    meta: Mutex<HashMap<String, VaultMetaItem>>
}

#[derive(Serialize, Deserialize)]
pub struct VaultMetaItem {
    name: String,
    bg_color: String,
    highlight_color: String
}

thread 'main' panicked at 'state not managed for field meta on command get_vault_by_name. You must call .manage() before using this command', state.rs:51:7

fervent depot
#

I feel very stupid, the problem was in importing VaultMeta in the vaults.rs file, it should have been done with crate:: (use crate::vault_meta::VaultMeta;). I'll leave it here, maybe someone will need it...

P.S. tell me how to mark the issue as solved, I don't use Discord often