Skip to main content

egui_pop_api/
plugin.rs

1//! Plugins (ADR 0004): an id, requirements, a priority, `enabled`,
2//! `connect` and `disconnect`. Everything a plugin registers through
3//! [`Ctx`] is its own, removed when it is disconnected.
4//!
5//! Not to be confused with `egui::Plugin`, egui's context hooks: an
6//! egui-pop plugin is an application feature.
7
8use std::fmt;
9
10use egui_pop_model::ids::PluginId;
11use egui_pop_model::order::OrderError;
12
13use crate::ctx::{Ctx, CtxError};
14
15/// An application feature.
16pub trait Plugin: 'static {
17    fn id(&self) -> PluginId;
18
19    /// A human title (settings sections, the debug bridge); the id by
20    /// default.
21    fn title(&self) -> String {
22        self.id().to_string()
23    }
24
25    /// The plugins it needs connected first.
26    fn requires(&self) -> Vec<PluginId> {
27        Vec::new()
28    }
29
30    /// Among plugins ready at the same time, higher connects first.
31    fn priority(&self) -> i32 {
32        0
33    }
34
35    /// Whether to connect it (a setting may switch it off).
36    fn enabled(&self, _ctx: &Ctx<'_>) -> bool {
37        true
38    }
39
40    /// Registers what it brings.
41    fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError>;
42
43    /// Releases what it holds outside the framework; its registrations
44    /// are removed by the framework.
45    fn disconnect(&mut self, _ctx: &mut Ctx<'_>) {}
46}
47
48/// Why a plugin did not connect.
49#[derive(Debug)]
50pub enum PluginError {
51    /// A registration was refused.
52    Ctx(CtxError),
53    /// The plugin's own reason.
54    Failed(String),
55}
56
57impl fmt::Display for PluginError {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            PluginError::Ctx(e) => write!(f, "{e}"),
61            PluginError::Failed(e) => write!(f, "{e}"),
62        }
63    }
64}
65
66impl std::error::Error for PluginError {}
67
68impl From<CtxError> for PluginError {
69    fn from(e: CtxError) -> Self {
70        PluginError::Ctx(e)
71    }
72}
73
74/// Why an application could not be built.
75#[derive(Debug)]
76pub enum BuildError {
77    /// Missing requirement, cycle, duplicate id.
78    Order(OrderError),
79    /// A plugin failed to connect.
80    Connect {
81        plugin: PluginId,
82        error: PluginError,
83    },
84    /// The bus given to the builder already holds egui-pop's points: one
85    /// egui-pop application per bus (ADR 0030).
86    Bus(bus_contract::Refused),
87}
88
89impl fmt::Display for BuildError {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        match self {
92            BuildError::Order(e) => write!(f, "{e}"),
93            BuildError::Connect { plugin, error } => {
94                write!(f, "plugin `{plugin}` failed to connect: {error}")
95            }
96            BuildError::Bus(e) => write!(f, "{e}: one egui-pop application per bus"),
97        }
98    }
99}
100
101impl std::error::Error for BuildError {}
102
103impl From<OrderError> for BuildError {
104    fn from(e: OrderError) -> Self {
105        BuildError::Order(e)
106    }
107}
108
109/// A plugin as the application lists it.
110#[derive(Clone, Debug, PartialEq, serde::Serialize)]
111pub struct PluginInfo {
112    pub id: PluginId,
113    pub title: String,
114    pub requires: Vec<PluginId>,
115    pub priority: i32,
116    pub connected: bool,
117    /// Why it is not connected, if it is not.
118    pub reason: Option<String>,
119}