polywrap_tests_utils/mocks/plugins/
with_env.rs

1use polywrap_core::invoker::Invoker;
2use polywrap_plugin::error::PluginError;
3use polywrap_plugin::{implementor::plugin_impl, module::PluginModule, JSON};
4use serde::{Deserialize, Serialize};
5
6use std::{fmt::Debug, sync::Arc};
7use wrap_manifest_schemas::versions::{WrapManifest, WrapManifestAbi};
8
9#[derive(Clone, Debug, Deserialize, Serialize)]
10pub struct GetEnvArgs {
11    key: String,
12}
13
14#[derive(Clone, Debug, Deserialize, Serialize)]
15pub struct Env {
16    foo: String,
17}
18
19#[derive(Debug)]
20pub struct PluginEnv;
21
22pub trait Module: PluginModule {
23    fn check_env_is_bar(
24        &mut self,
25        args: &GetEnvArgs,
26        invoker: Arc<dyn Invoker>,
27        env: Option<Env>,
28    ) -> Result<bool, PluginError>;
29}
30
31#[plugin_impl]
32impl Module for PluginEnv {
33    fn check_env_is_bar(
34        &mut self,
35        args: &GetEnvArgs,
36        _: Arc<dyn Invoker>,
37        _env: Option<Env>,
38    ) -> Result<bool, PluginError> {
39        if let Some(_env) = _env {
40            let value = match args.key.as_str() {
41                "foo" => &_env.foo,
42                &_ => panic!("Property does not exist"),
43            };
44            return Ok(value == "bar");
45        }
46
47        Ok(false)
48    }
49}
50
51pub fn get_manifest() -> WrapManifest {
52    WrapManifest {
53        name: "env".to_string(),
54        type_: "plugin".to_string(),
55        version: "0.1".to_string(),
56        abi: JSON::from_value::<WrapManifestAbi>(JSON::json!({})).unwrap(),
57    }
58}