bmux_plugin 0.0.1-alpha.1

Plugin system for bmux terminal multiplexer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use crate::{
    PluginDeclaration, PluginDependency, PluginEntrypoint, PluginExecutionClass, PluginId,
    PluginOwnedPath,
};
use bmux_plugin_sdk::{
    ApiVersion, HostScope, PluginCommand, PluginError, PluginEventPublication,
    PluginEventSubscription, PluginFeature, PluginService, Result, VersionRange,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum PluginRuntime {
    #[default]
    Native,
    Process,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginManifestCompatibility {
    pub minimum: String,
    #[serde(default)]
    pub maximum: Option<String>,
}

impl Default for PluginManifestCompatibility {
    fn default() -> Self {
        Self {
            minimum: "1.0".to_string(),
            maximum: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginManifestDependency {
    pub plugin_id: String,
    #[serde(default = "default_dependency_version_req")]
    pub version_req: String,
    #[serde(default = "default_true")]
    pub required: bool,
}

impl PluginManifestCompatibility {
    /// # Errors
    ///
    /// Returns an error when any declared version cannot be parsed.
    pub fn to_version_range(&self) -> std::result::Result<VersionRange, String> {
        let minimum = self.minimum.parse::<ApiVersion>()?;
        let maximum = self
            .maximum
            .as_deref()
            .map(str::parse::<ApiVersion>)
            .transpose()?;
        Ok(VersionRange { minimum, maximum })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginManifest {
    pub id: String,
    pub name: String,
    pub version: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub homepage: Option<String>,
    #[serde(default)]
    pub provider_priority: i32,
    #[serde(default)]
    pub execution_class: PluginExecutionClass,
    #[serde(default)]
    pub owns_namespaces: BTreeSet<String>,
    #[serde(default)]
    pub owns_paths: BTreeSet<PluginOwnedPath>,
    #[serde(default)]
    pub runtime: PluginRuntime,
    #[serde(default)]
    pub entry: Option<PathBuf>,
    #[serde(default)]
    pub entry_args: Vec<String>,
    #[serde(default)]
    pub process_persistent_worker: bool,
    #[serde(default = "default_entry_symbol")]
    pub entry_symbol: String,
    #[serde(default)]
    pub plugin_api: PluginManifestCompatibility,
    #[serde(default)]
    pub native_abi: PluginManifestCompatibility,
    #[serde(default)]
    #[serde(alias = "required_host_scopes")]
    pub required_capabilities: BTreeSet<HostScope>,
    #[serde(default)]
    pub provided_capabilities: BTreeSet<HostScope>,
    #[serde(default)]
    pub provided_features: BTreeSet<PluginFeature>,
    #[serde(default)]
    pub services: Vec<PluginService>,
    #[serde(default)]
    pub commands: Vec<PluginCommand>,
    #[serde(default)]
    pub event_subscriptions: Vec<PluginEventSubscription>,
    /// Declares the event kinds this plugin publishes. Used by the
    /// server to wire up routing (e.g. forwarding to streaming
    /// clients) without scanning BPDL schemas at runtime. Omitting
    /// the field or leaving it empty means the plugin's emissions
    /// stay intra-process.
    #[serde(default)]
    pub event_publications: Vec<PluginEventPublication>,
    #[serde(default)]
    pub dependencies: Vec<PluginManifestDependency>,
    #[serde(default)]
    pub keybindings: PluginManifestKeybindings,
    #[serde(default)]
    pub ready_signals: Vec<bmux_plugin_sdk::ReadySignalDecl>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct PluginManifestKeybindings {
    #[serde(default)]
    pub runtime: BTreeMap<String, String>,
    #[serde(default)]
    pub global: BTreeMap<String, String>,
    #[serde(default)]
    pub scroll: BTreeMap<String, String>,
}

impl PluginManifest {
    /// # Errors
    ///
    /// Returns an error when the manifest cannot be parsed.
    pub fn from_toml_str(value: &str) -> Result<Self> {
        Ok(toml::from_str(value)?)
    }

    /// # Errors
    ///
    /// Returns an error when the manifest cannot be read or parsed.
    pub fn from_path(path: &Path) -> Result<Self> {
        let contents = fs::read_to_string(path)?;
        Self::from_toml_str(&contents)
    }

    /// # Errors
    ///
    /// Returns an error when the manifest cannot be converted into a checked
    /// declaration.
    pub fn to_declaration(&self) -> Result<PluginDeclaration> {
        let declaration = PluginDeclaration {
            id: PluginId::new(self.id.clone())?,
            display_name: self.name.clone(),
            plugin_version: self.version.clone(),
            plugin_api: self.plugin_api.to_version_range().map_err(|details| {
                PluginError::InvalidVersionRange {
                    plugin_id: self.id.clone(),
                    field: "plugin_api",
                    details,
                }
            })?,
            native_abi: self.native_abi.to_version_range().map_err(|details| {
                PluginError::InvalidVersionRange {
                    plugin_id: self.id.clone(),
                    field: "native_abi",
                    details,
                }
            })?,
            entrypoint: match self.runtime {
                PluginRuntime::Native => PluginEntrypoint::Native {
                    symbol: self.entry_symbol.clone(),
                },
                PluginRuntime::Process => {
                    let command = self
                        .entry
                        .as_ref()
                        .map(|entry| entry.to_string_lossy().to_string())
                        .ok_or_else(|| PluginError::MissingEntryPath {
                            plugin_id: self.id.clone(),
                        })?;
                    PluginEntrypoint::Process {
                        command,
                        args: self.entry_args.clone(),
                        persistent_worker: self.process_persistent_worker,
                    }
                }
            },
            description: self.description.clone(),
            homepage: self.homepage.clone(),
            provider_priority: self.provider_priority,
            execution_class: self.execution_class,
            owns_namespaces: self.owns_namespaces.clone(),
            owns_paths: self.owns_paths.clone(),
            required_capabilities: self.required_capabilities.clone(),
            provided_capabilities: self.provided_capabilities.clone(),
            provided_features: self.provided_features.clone(),
            services: self.services.clone(),
            commands: self.commands.clone(),
            event_subscriptions: self.event_subscriptions.clone(),
            event_publications: self.event_publications.clone(),
            dependencies: self
                .dependencies
                .iter()
                .map(|dependency| {
                    Ok(PluginDependency {
                        plugin_id: PluginId::new(dependency.plugin_id.clone())?,
                        version_req: dependency.version_req.clone(),
                        required: dependency.required,
                    })
                })
                .collect::<Result<Vec<_>>>()?,
            lifecycle: crate::PluginLifecycle::default(),
            ready_signals: self.ready_signals.clone(),
        };
        declaration.validate()?;
        Ok(declaration)
    }

    #[must_use]
    pub fn resolve_entry_path(&self, base_dir: &Path) -> Option<PathBuf> {
        let entry = self.entry.as_ref()?;
        if entry.is_absolute() {
            Some(entry.clone())
        } else {
            Some(base_dir.join(entry))
        }
    }
}

fn default_entry_symbol() -> String {
    bmux_plugin_sdk::DEFAULT_NATIVE_ENTRY_SYMBOL.to_string()
}

const fn default_true() -> bool {
    true
}

fn default_dependency_version_req() -> String {
    "*".to_string()
}

#[cfg(test)]
mod tests {
    use super::{PluginManifest, PluginRuntime};
    use crate::{PluginEntrypoint, PluginExecutionClass};
    use bmux_plugin_sdk::{HostScope, PluginError};

    #[test]
    fn parses_native_plugin_manifest() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "git.status"
name = "Git Status"
version = "0.1.0"
runtime = "native"
entry = "libgit_status.dylib"
required_capabilities = ["bmux.commands", "bmux.events.subscribe"]

[[commands]]
name = "hello"
summary = "hello"
execution = "provider_exec"

[[event_subscriptions]]
kinds = ["bmux.core/server-started"]
"#,
        )
        .expect("manifest should parse");

        assert_eq!(manifest.id, "git.status");
        assert!(
            manifest
                .required_capabilities
                .contains(&HostScope::new("bmux.commands").expect("scope should parse"))
        );
        assert_eq!(manifest.commands.len(), 1);
        assert_eq!(manifest.event_subscriptions.len(), 1);
        assert!(manifest.keybindings.runtime.is_empty());
        assert!(manifest.keybindings.global.is_empty());
        assert!(manifest.keybindings.scroll.is_empty());
    }

    #[test]
    fn parses_manifest_keybinding_proposals() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "bmux.windows"
name = "Windows"
version = "0.1.0"
runtime = "native"
entry = "libwindows.dylib"

[keybindings.runtime]
c = "plugin:bmux.windows:new-window"
"alt+w" = "plugin:bmux.windows:switch-window"
"#,
        )
        .expect("manifest should parse");

        assert_eq!(
            manifest.keybindings.runtime.get("c").map(String::as_str),
            Some("plugin:bmux.windows:new-window")
        );
    }

    #[test]
    fn plugin_api_and_native_abi_default_to_1_0() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.minimal"
name = "Minimal"
version = "0.1.0"
"#,
        )
        .expect("manifest should parse without plugin_api/native_abi/entry");

        assert_eq!(manifest.plugin_api.minimum, "1.0");
        assert!(manifest.plugin_api.maximum.is_none());
        assert_eq!(manifest.native_abi.minimum, "1.0");
        assert!(manifest.native_abi.maximum.is_none());
        assert!(manifest.entry.is_none());

        // Verify conversion to declaration also works
        let declaration = manifest.to_declaration().expect("declaration should build");
        assert_eq!(declaration.id.as_str(), "test.minimal");
    }

    #[test]
    fn entry_is_optional_and_resolves_when_present() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.with_entry"
name = "With Entry"
version = "0.1.0"
entry = "libfoo.dylib"
"#,
        )
        .expect("manifest should parse");

        assert_eq!(
            manifest.entry.as_deref(),
            Some(std::path::Path::new("libfoo.dylib"))
        );
        assert_eq!(
            manifest.resolve_entry_path(std::path::Path::new("/base")),
            Some(std::path::PathBuf::from("/base/libfoo.dylib"))
        );

        let no_entry = PluginManifest::from_toml_str(
            r#"
id = "test.no_entry"
name = "No Entry"
version = "0.1.0"
"#,
        )
        .expect("manifest should parse without entry");

        assert!(no_entry.entry.is_none());
        assert!(
            no_entry
                .resolve_entry_path(std::path::Path::new("/base"))
                .is_none()
        );
    }

    #[test]
    fn explicit_plugin_api_and_native_abi_override_defaults() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.custom_compat"
name = "Custom Compat"
version = "0.1.0"
entry = "unused.dylib"

[plugin_api]
minimum = "2.0"
maximum = "3.0"

[native_abi]
minimum = "1.5"
"#,
        )
        .expect("manifest with explicit compat should parse");

        assert_eq!(manifest.plugin_api.minimum, "2.0");
        assert_eq!(manifest.plugin_api.maximum.as_deref(), Some("3.0"));
        assert_eq!(manifest.native_abi.minimum, "1.5");
        assert!(manifest.native_abi.maximum.is_none());

        let declaration = manifest.to_declaration().expect("declaration should build");
        assert_eq!(declaration.id.as_str(), "test.custom_compat");
    }

    #[test]
    fn execution_class_defaults_to_native_standard() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.execution.default"
name = "Execution Default"
version = "0.1.0"
"#,
        )
        .expect("manifest should parse");
        assert_eq!(
            manifest.execution_class,
            PluginExecutionClass::NativeStandard
        );
    }

    #[test]
    fn execution_class_parses_interpreter() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.execution.interpreter"
name = "Execution Interpreter"
version = "0.1.0"
execution_class = "interpreter"
"#,
        )
        .expect("manifest should parse");
        assert_eq!(manifest.execution_class, PluginExecutionClass::Interpreter);
    }

    #[test]
    fn parses_command_ownership_fields() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.ownership"
name = "Ownership"
version = "0.1.0"
owns_namespaces = ["logs", "recording"]
owns_paths = [["terminal", "doctor"], ["terminal", "install-terminfo"]]
"#,
        )
        .expect("manifest should parse");
        assert!(manifest.owns_namespaces.contains("logs"));
        assert!(
            manifest
                .owns_paths
                .iter()
                .any(|path| path.0 == vec!["terminal".to_string(), "doctor".to_string()])
        );
    }

    #[test]
    fn parses_process_runtime_manifest() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.process"
name = "Process Runtime"
version = "0.1.0"
runtime = "process"
entry = "python3"
entry_args = ["plugin.py", "--stdio"]
process_persistent_worker = true
"#,
        )
        .expect("manifest should parse");
        assert_eq!(manifest.runtime, PluginRuntime::Process);

        let declaration = manifest
            .to_declaration()
            .expect("declaration should support process runtime");
        assert!(matches!(
            declaration.entrypoint,
            PluginEntrypoint::Process {
                ref command,
                ref args,
                persistent_worker: true,
            } if command == "python3" && args == &vec!["plugin.py".to_string(), "--stdio".to_string()]
        ));
    }

    #[test]
    fn process_runtime_requires_entry_command() {
        let manifest = PluginManifest::from_toml_str(
            r#"
id = "test.process.missing"
name = "Missing Process Entry"
version = "0.1.0"
runtime = "process"
"#,
        )
        .expect("manifest should parse");
        let error = manifest
            .to_declaration()
            .expect_err("process runtime requires entry command");
        assert!(matches!(error, PluginError::MissingEntryPath { .. }));
    }
}