Skip to content

Tauri State being used for filepath constant #31

Description

@mberry

The outcome of parsing the CLI is sent to tauri and then called back from it in multiple places, even though it doesn't change.

Sent to tauri:

     builder
        .manage(cli)

Pulled back from tauri:

async fn read_link(name: String, state: tauri::State<'_, Cli>) -> Result<LinkWrapper, ()> {
    let file_path = format!("{}/{name}", &state.path);
    let link = Link::load(&state.path, &file_path).expect(&format!("Error loading {file_path}"));

Really these could just be the global statics rather than state. There's a lazy static macro in use but OnceLock is also in the standard library now, which could initialise the global.

Which instead would look like:

pub static ARK_SHELF_WORKING_DIR: OnceLock<PathBuf> = OnceLock::new();
pub static SCORES_PATH: OnceLock<PathBuf> = OnceLock::new();

fn main() {
    let cli = Cli::parse();
    let base_dir = match cli.path {
        Ok(path) => PathBuf::from(path),
        // This is using platform defaults but otherwise stick with home_dir() + ark-shelf
        None => ProjectDirs::from("dev", "Ark Builders",  "Shelf-Desktop")
                .map_or(PathBuf::from(""), |proj| PathBuf::from(proj.data_dir()))
    };

    // Initialise global constants
    ARK_SHELF_WORKING_DIR.set(base_dir.clone()).expect("Setting Working Dir");
    SCORES_PATH.set(base_dir.join("scores")).expect("Setting Scores Path");

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions