Skip to main content

crate_plugin_kit/manifest/
sections.rs

1//! The sections a manifest is made of.
2//!
3//! Only two sections are defined by this crate: `[plugin]`, which every host reads, and
4//! `[lib]`, which lets a plugin override the derived cdylib stem. Everything else a host
5//! adds is carried through untouched in the `extra` tables of `PluginManifest` and
6//! [`PluginSection`].
7//!
8//! The types are only data: nothing here reads or writes a file.
9
10use serde::{Deserialize, Serialize};
11
12/// The `[plugin]` section of a manifest.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct PluginSection {
15    /// Plugin name. This is the authoritative source — after loading, a host should
16    /// check that the plugin's self-declared name matches it.
17    pub name: String,
18
19    /// Plugin version.
20    pub version: String,
21
22    /// Cross-boundary ABI version. For the host's own use; this crate only moves it
23    /// around.
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub abi: Option<u32>,
26
27    /// Ecosystem family. For the host's own use; this crate only moves it around.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub family: Option<String>,
30
31    /// Plugin repository URL. Needed when downloading a prebuilt, to build the release
32    /// URL.
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    pub repository: Option<String>,
35
36    /// Human-readable description.
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub description: Option<String>,
39
40    /// Any other keys, preserved verbatim.
41    #[serde(flatten)]
42    pub extra: toml::Table,
43}
44
45/// The `[lib]` section of a manifest.
46#[derive(Debug, Clone, Default, Serialize, Deserialize)]
47pub struct LibSection {
48    /// cdylib file name stem (platform-independent, without the `lib` prefix and the
49    /// extension).
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub stem: Option<String>,
52
53    /// Any other keys, preserved verbatim.
54    #[serde(flatten)]
55    pub extra: toml::Table,
56}
57
58#[cfg(test)]
59mod tests {
60    use crate::manifest::tests::parse;
61
62    #[test]
63    fn parses_the_plugin_section() {
64        let m = parse(
65            r#"
66[plugin]
67name       = "cargo"
68version    = "0.1.0"
69abi        = 1
70family     = "rust"
71repository = "https://github.com/imba97/bmux-plugin-cargo"
72"#,
73        );
74        assert_eq!(m.plugin.name, "cargo");
75        assert_eq!(m.plugin.version, "0.1.0");
76        assert_eq!(m.plugin.abi, Some(1));
77        assert_eq!(m.plugin.family.as_deref(), Some("rust"));
78        assert_eq!(
79            m.plugin.repository.as_deref(),
80            Some("https://github.com/imba97/bmux-plugin-cargo")
81        );
82    }
83
84    #[test]
85    fn parses_the_lib_section() {
86        let m = parse("[plugin]\nname = \"cargo\"\nversion = \"0.1.0\"\n\n[lib]\nstem = \"s\"\n");
87        assert_eq!(m.lib.as_ref().and_then(|l| l.stem.as_deref()), Some("s"));
88    }
89}