Skip to main content

egui_pop_api/links/
lint.rs

1//! The links' lint: every link the application declares checked against
2//! the application and the policy, so a renamed command, a removed file
3//! or a blocked scheme cannot leave a dead link behind.
4
5use egui_pop_model::links::{self as model, Target};
6use egui_pop_model::menus::Item;
7
8use super::{CMD_OPEN, CMD_REVEAL, declared, links};
9use crate::app::AppHandle;
10
11/// What is wrong with the links the application declares, one line each:
12/// a target that does not parse, a blocked scheme, a `command:` link
13/// naming a command that is not declared, a local file that is not
14/// there, a `docs:` page the viewer does not have (when a viewer is
15/// installed and knows its pages). Walked: links declared by plugins ([`crate::Ctx::link`],
16/// [`super::declare`]) and drawn as declared so far ([`super::link`]),
17/// menu items running `links.open` or `files.reveal`, the links in the
18/// tips' texts, and the actions of the toasts shown so far.
19pub fn problems(app: &AppHandle) -> Vec<String> {
20    let Some(links) = links(app) else {
21        return vec!["the links plugin is not connected".into()];
22    };
23    let policy = links.policy();
24    let docs = links.inside().and_then(|i| i.docs_pages());
25    let commands: Vec<String> = app
26        .commands()
27        .into_iter()
28        .map(|(s, _)| s.id.as_str().to_string())
29        .collect();
30    let mut all: Vec<(String, String)> = declared(app);
31    for menu in app.menu_ids() {
32        for item in app.menu(&menu) {
33            if let Item::Command { id, args, .. } = &item.item
34                && (*id == CMD_OPEN || *id == CMD_REVEAL)
35            {
36                let key = if *id == CMD_OPEN { "target" } else { "path" };
37                match args[key].as_str() {
38                    Some(t) => all.push((format!("menu `{menu}`"), t.to_string())),
39                    None => all.push((format!("menu `{menu}`"), String::new())),
40                }
41            }
42        }
43    }
44    if let Some(tips) = app.service(&crate::tips::TIPS) {
45        for tip in tips.all() {
46            for f in model::find(&tip.text) {
47                all.push((format!("tip `{}`", tip.id), f.target));
48            }
49        }
50    }
51    if let Some(toasts) = app.service(&crate::toasts::TOASTS) {
52        for t in toasts.history().into_iter().chain(toasts.shown()) {
53            if let Some(a) = &t.toast.action
54                && (a.command == CMD_OPEN || a.command == CMD_REVEAL)
55                && let Some(target) = a.args["target"].as_str().or(a.args["path"].as_str())
56            {
57                all.push((format!("toast `{}`", t.toast.text), target.to_string()));
58            }
59        }
60    }
61    all.sort();
62    all.dedup();
63    all.iter()
64        .filter_map(|(place, target)| {
65            check(&policy, target, &commands, docs.as_deref())
66                .err()
67                .map(|e| format!("{place}: {e}"))
68        })
69        .collect()
70}
71
72/// One declared link's problem, if any; `docs` lists the `docs:` pages
73/// there are (`None`: not checked).
74pub fn check(
75    policy: &model::Policy,
76    target: &str,
77    commands: &[String],
78    docs: Option<&[String]>,
79) -> Result<(), String> {
80    model::check_declared(policy, target, commands, docs)?;
81    // A file the application ships or names must be there.
82    if let Ok(t) = Target::parse(target)
83        && let Some(facts) = super::facts_of(&t)
84        && !facts.exists
85    {
86        return Err(format!("`{target}`: the file is not there"));
87    }
88    Ok(())
89}