Add a shared error type and shared file helpers to the Rust host
Replace the three error styles with one Error type and one Result alias in error.rs. The error crosses IPC as the same plain string as before. The message text does not change. Move the shared file mechanics to fsutil.rs. These are the reserved-name check, the link and junction check, the limited reads, and the atomic writes. Each module keeps its own path rules, limits, and messages. The plugin settings writer now flushes its file to disk before the rename. Remove the current_root_path and list_entries wrappers and an unused mime value. Use named windows-sys constants for the process, file, drive, and error codes. Add a test that compares the command lists in lib.rs, build.rs, and the capability files. The settings capability must be a subset of the full list. In Cargo.toml, set the author, keep only the rlib crate type, and remove the unused thiserror dependency. Remove panic = "abort" from the release profile. A panic on a worker thread then stops only that thread. It does not stop the editor with unsaved buffers.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
mod error;
|
||||
mod fsutil;
|
||||
mod pty;
|
||||
mod terminal_platform;
|
||||
mod plugins;
|
||||
@@ -118,3 +120,91 @@ pub fn run() {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod command_list_tests {
|
||||
//! Each command is listed in four places. This test keeps the lists the same.
|
||||
use std::collections::BTreeSet;
|
||||
use std::path::Path;
|
||||
|
||||
fn read(relative: &str) -> String {
|
||||
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join(relative);
|
||||
std::fs::read_to_string(&path).unwrap_or_else(|error| panic!("Cannot read {}: {error}", path.display()))
|
||||
}
|
||||
|
||||
/// Fail on a duplicate entry, which a set would hide.
|
||||
fn unique(file: &str, names: impl Iterator<Item = String>) -> BTreeSet<String> {
|
||||
let mut set = BTreeSet::new();
|
||||
for name in names {
|
||||
assert!(set.insert(name.clone()), "{file} lists the command {name} more than once.");
|
||||
}
|
||||
set
|
||||
}
|
||||
|
||||
/// The text between the first `start` and the next `end` after it.
|
||||
fn section<'a>(text: &'a str, start: &str, end: &str, file: &str) -> &'a str {
|
||||
let from = text.find(start).unwrap_or_else(|| panic!("{file} has no `{start}`")) + start.len();
|
||||
let length = text[from..].find(end).unwrap_or_else(|| panic!("{file} has no `{end}` after `{start}`"));
|
||||
&text[from..from + length]
|
||||
}
|
||||
|
||||
fn handler_commands() -> BTreeSet<String> {
|
||||
let source = read("src/lib.rs");
|
||||
let list = section(&source, "generate_handler![", "]", "src/lib.rs");
|
||||
unique("src/lib.rs", list.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|entry| !entry.is_empty())
|
||||
.map(|entry| entry.rsplit("::").next().unwrap().to_owned()))
|
||||
}
|
||||
|
||||
fn build_commands() -> BTreeSet<String> {
|
||||
let source = read("build.rs");
|
||||
let list = section(&source, ".commands(&[", "]", "build.rs");
|
||||
unique("build.rs", list.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|entry| !entry.is_empty())
|
||||
.map(|entry| entry.trim_matches('"').to_owned()))
|
||||
}
|
||||
|
||||
fn capability_commands(relative: &str) -> BTreeSet<String> {
|
||||
let value: serde_json::Value = serde_json::from_str(&read(relative))
|
||||
.unwrap_or_else(|error| panic!("{relative} is not valid JSON: {error}"));
|
||||
let permissions = value["permissions"]
|
||||
.as_array()
|
||||
.unwrap_or_else(|| panic!("{relative} has no permissions list"))
|
||||
.iter()
|
||||
.filter_map(|permission| permission.as_str())
|
||||
// Plugin permissions such as `core:default` have a prefix. App commands do not.
|
||||
.filter(|permission| !permission.contains(':'))
|
||||
.map(|permission| {
|
||||
let command = permission.strip_prefix("allow-")
|
||||
.unwrap_or_else(|| panic!("{relative} has an app permission without `allow-`: {permission}"));
|
||||
command.replace('-', "_")
|
||||
});
|
||||
unique(relative, permissions)
|
||||
}
|
||||
|
||||
fn difference(left: &BTreeSet<String>, right: &BTreeSet<String>) -> Vec<String> {
|
||||
left.difference(right).cloned().collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_lists_match() {
|
||||
let handler = handler_commands();
|
||||
let build = build_commands();
|
||||
let default = capability_commands("capabilities/default.json");
|
||||
let settings = capability_commands("capabilities/settings.json");
|
||||
assert!(handler.len() > 50, "Found only {} commands in generate_handler! in src/lib.rs", handler.len());
|
||||
for (name, list) in [("build.rs", &build), ("capabilities/default.json", &default)] {
|
||||
assert!(
|
||||
&handler == list,
|
||||
"The commands in {name} differ from generate_handler! in src/lib.rs.\nMissing from {name}: {:?}\nOnly in {name}: {:?}",
|
||||
difference(&handler, list),
|
||||
difference(list, &handler),
|
||||
);
|
||||
}
|
||||
// The settings windows get an intentional subset of the commands.
|
||||
let unknown = difference(&settings, &handler);
|
||||
assert!(unknown.is_empty(), "capabilities/settings.json allows commands that generate_handler! in src/lib.rs does not register: {unknown:?}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user