1use std::fmt;
9
10use egui_pop_model::ids::PluginId;
11use egui_pop_model::order::OrderError;
12
13use crate::ctx::{Ctx, CtxError};
14
15pub trait Plugin: 'static {
17 fn id(&self) -> PluginId;
18
19 fn title(&self) -> String {
22 self.id().to_string()
23 }
24
25 fn requires(&self) -> Vec<PluginId> {
27 Vec::new()
28 }
29
30 fn priority(&self) -> i32 {
32 0
33 }
34
35 fn enabled(&self, _ctx: &Ctx<'_>) -> bool {
37 true
38 }
39
40 fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError>;
42
43 fn disconnect(&mut self, _ctx: &mut Ctx<'_>) {}
46}
47
48#[derive(Debug)]
50pub enum PluginError {
51 Ctx(CtxError),
53 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#[derive(Debug)]
76pub enum BuildError {
77 Order(OrderError),
79 Connect {
81 plugin: PluginId,
82 error: PluginError,
83 },
84 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#[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 pub reason: Option<String>,
119}