egui_pop_api/tips/
lint.rs1use egui_pop_model::when::When;
5
6use super::{TIPS, Tip, markup, targets_seen};
7use crate::app::AppHandle;
8
9pub fn problems(app: &AppHandle, targets: &[&str]) -> Vec<String> {
16 let Some(tips) = app.service(&TIPS) else {
17 return vec!["the tips plugin is not connected".into()];
18 };
19 let commands: Vec<String> = app
20 .commands()
21 .into_iter()
22 .map(|(s, _)| s.id.as_str().to_string())
23 .collect();
24 let mut named: Vec<String> = targets.iter().map(|t| t.to_string()).collect();
25 named.extend(targets_seen(&app.egui()));
26 tips.all()
27 .iter()
28 .flat_map(|t| check(t, &commands, &named))
29 .collect()
30}
31
32pub fn check(tip: &Tip, commands: &[String], targets: &[String]) -> Vec<String> {
34 let mut out = Vec::new();
35 let id = &tip.id;
36 let mut say = |what: String| out.push(format!("tip `{id}`: {what}"));
37 if tip.title.trim().is_empty() {
38 say("its title is empty".into());
39 }
40 if tip.text.trim().is_empty() {
41 say("its text is empty".into());
42 }
43 if !tip.origin.is_empty() && !id.starts_with(&format!("{}.", tip.origin)) {
44 say(format!(
45 "its id is not prefixed by its origin `{}`",
46 tip.origin
47 ));
48 }
49 match &tip.command {
50 Some(c) if !commands.iter().any(|k| k == c.as_str()) => {
51 say(format!("its command `{c}` is not declared"));
52 }
53 None if markup::has_own_key(&tip.text) => {
54 say("`{key}` without a command".into());
55 }
56 _ => {}
57 }
58 for c in markup::named_commands(&tip.text) {
59 if !commands.contains(&c) {
60 say(format!("`{{key:{c}}}` names a command not declared"));
61 }
62 }
63 if !tip.surface.is_empty()
64 && let Err(e) = When::parse(&tip.surface)
65 {
66 say(format!("its surface `{}` does not parse: {e}", tip.surface));
67 }
68 if let Some(t) = &tip.target
69 && !targets.contains(t)
70 {
71 say(format!("its target `{t}` is never named"));
72 }
73 out
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use egui_pop_model::ids::CommandId;
80
81 #[test]
82 fn a_dead_tip_is_reported() {
83 let commands = vec!["photos.rate".to_string()];
84 let targets = vec!["photos.stars".to_string()];
85 let good = Tip::new("photos.rate", "Rate", "Press {key}.")
86 .command(CommandId::from_static("photos.rate"))
87 .surface("panel == 'grid'")
88 .target("photos.stars")
89 .origin("photos");
90 assert!(check(&good, &commands, &targets).is_empty());
91 let dead = Tip::new("rate", "Rate", "Press {key}, {key:photos.gone}.")
92 .command(CommandId::from_static("photos.renamed"))
93 .surface("panel ==")
94 .target("photos.moved")
95 .origin("photos");
96 let p = check(&dead, &commands, &targets);
97 assert_eq!(p.len(), 5, "{p:#?}");
98 let bare = Tip::new("x.y", "T", "Press {key}.");
99 assert_eq!(check(&bare, &commands, &targets).len(), 1);
100 }
101}