Skip to main content

robost_plugin_api/
lib.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use thiserror::Error;
4
5/// Dynamically-typed value passed between the host and a plugin function.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(untagged)]
8pub enum Value {
9    Null,
10    Bool(bool),
11    Int(i64),
12    Float(f64),
13    String(String),
14    List(Vec<Value>),
15    Map(HashMap<String, Value>),
16}
17
18impl Value {
19    pub fn as_str(&self) -> Option<&str> {
20        match self {
21            Value::String(s) => Some(s),
22            _ => None,
23        }
24    }
25}
26
27/// Errors a plugin function may return to the host.
28#[derive(Debug, Error, Serialize, Deserialize)]
29pub enum PluginError {
30    #[error("invalid argument '{name}': {reason}")]
31    InvalidArgument { name: String, reason: String },
32    #[error("permission denied: {0}")]
33    PermissionDenied(String),
34    #[error("io error: {0}")]
35    Io(String),
36    #[error("plugin error: {0}")]
37    Other(String),
38}
39
40/// Type alias for plugin function results.
41pub type PluginResult = Result<HashMap<String, Value>, PluginError>;
42
43/// Manifest read from `plugin.toml`.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct PluginManifest {
46    pub plugin: PluginInfo,
47    #[serde(default)]
48    pub function: Vec<FunctionDef>,
49    #[serde(default)]
50    pub permissions: Permissions,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct PluginInfo {
55    pub name: String,
56    pub version: String,
57    pub description: Option<String>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct FunctionDef {
62    pub name: String,
63    #[serde(default)]
64    pub inputs: Vec<ParamDef>,
65    #[serde(default)]
66    pub outputs: Vec<ParamDef>,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct ParamDef {
71    pub name: String,
72    #[serde(rename = "type")]
73    pub ty: String,
74    pub description: Option<String>,
75}
76
77#[derive(Debug, Clone, Default, Serialize, Deserialize)]
78pub struct Permissions {
79    #[serde(default)]
80    pub filesystem: Vec<String>,
81    #[serde(default = "bool_false")]
82    pub network: bool,
83    #[serde(default = "bool_false")]
84    pub screen: bool,
85}
86
87fn bool_false() -> bool {
88    false
89}