Native Template System
Coverage: src-tauri/…/template.rs
Overview
The native template system in src-tauri/…/template.rs manages bundled application scripts that must exist on disk for runtime use. At present it owns one template asset: ask.js, embedded at compile time through include_bytes!("../../scripts/ask.js") as SCRIPT_ASK and exposed on Template { ask: Vec<u8> }.
Template::new(template_dir) is the entry point. It starts from Template::default(), which fills ask from the embedded bytes, then ensures template_dir/ask.js exists and is current by calling update_or_create_file(&template_dir.join("ask.js"), SCRIPT_ASK). The constructor logs whether the file was created, updated, already current, or failed, but it does not return an error to callers; it always returns the in-memory Template populated from the bundled script bytes.
Disk state is managed by helper functions in the same file. create_dir() creates the parent directory for the target file, read_file_contents() loads any existing file, and write_file_contents() overwrites it. update_or_create_file() writes the bundled bytes when the file is missing or unreadable, or when the bundled script declares a newer semantic version than the existing file. Version metadata is extracted by read_version_info(), which scans script comments for @name, @version, and @url lines and returns those values as JSON.
The version check is semver-based. If an existing ask.js has no version string, it is overwritten. If both current and bundled files have versions, the bundled file is written only when Version::parse(new_version)? > Version::parse(current_version)?; equal or older bundled versions leave the existing disk file untouched. The embedded source file at src-tauri/…/ask.js currently declares @version 0.1.0 and defines window.ChatAsk with sync(message) for filling a textarea and submit() for clicking the last main form button.
Coupling
- If src-tauri/…/ask.js changes, update its
@versionline whenever existing installations should receive the new script. src-tauri/…/template.rs only overwrites an existing template_dir/ask.js when the bundled semver is greater than the file already on disk. - If the script header format changes, update
read_version_info()in src-tauri/…/template.rs at the same time. It relies on newline-terminated@name,@version, and@urlregexes, so metadata without a trailing newline or with a different tag spelling can make the updater treat version fields as empty. - If callers begin depending on failure visibility from template installation, change
Template::new()in src-tauri/…/template.rs to return aResult. It currently logsupdate_or_create_file()errors and still returns a default in-memory template, so write failures are not propagated.
Risks
- src-tauri/…/template.rs parses both bundled and existing versions with
semver::Version::parse. If either file contains a non-semver@version,update_or_create_file()returns an error;Template::new()logs that error and leaves the disk copy unchanged, which can silently strand users on an old or missing script from the caller's perspective. - src-tauri/…/template.rs treats any read error from
read_file_contents()as a missing file and attempts to overwrite viawrite_file_contents(). If the original error was a permission or filesystem problem, the constructor only logs the failure, so the rest of the app may continue without the expected ask.js on disk.