swc_plugin_runner/
plugin_module_bytes.rs1use serde::{Deserialize, Serialize};
2
3use crate::runtime;
4
5pub trait PluginModuleBytes {
9 fn get_module_name(&self) -> &str;
12 fn compile_module(&self, rt: &dyn runtime::Runtime) -> runtime::Module;
14}
15
16#[derive(Debug, Clone, Eq, Serialize, Deserialize, PartialEq)]
18pub struct RawPluginModuleBytes {
19 plugin_name: String,
20 bytes: Vec<u8>,
21}
22
23impl PluginModuleBytes for RawPluginModuleBytes {
24 fn get_module_name(&self) -> &str {
25 &self.plugin_name
26 }
27
28 fn compile_module(&self, rt: &dyn runtime::Runtime) -> runtime::Module {
29 let cache = rt.prepare_module(&self.bytes).unwrap();
30 runtime::Module::Cache(cache)
31 }
32}
33
34impl RawPluginModuleBytes {
35 pub fn new(identifier: String, bytes: Vec<u8>) -> Self {
36 Self {
37 plugin_name: identifier,
38 bytes,
39 }
40 }
41}
42
43#[derive(Serialize, Deserialize)]
47pub struct CompiledPluginModuleBytes {
48 plugin_name: String,
49 #[serde(skip)]
50 cache: Option<runtime::ModuleCache>,
51}
52
53impl CompiledPluginModuleBytes {
54 pub fn new(identifier: String, cache: runtime::ModuleCache) -> Self {
55 Self {
56 plugin_name: identifier,
57 cache: Some(cache),
58 }
59 }
60
61 pub fn from_raw_module(rt: &dyn runtime::Runtime, raw: RawPluginModuleBytes) -> Self {
64 let cache = rt.prepare_module(&raw.bytes).unwrap();
65 CompiledPluginModuleBytes {
66 plugin_name: raw.plugin_name,
67 cache: Some(cache),
68 }
69 }
70
71 pub fn clone_module(&self, builder: &dyn runtime::Runtime) -> Self {
72 CompiledPluginModuleBytes {
73 plugin_name: self.plugin_name.clone(),
74 cache: self
75 .cache
76 .as_ref()
77 .map(|cache| builder.clone_cache(cache).unwrap()),
78 }
79 }
80}
81
82impl PluginModuleBytes for CompiledPluginModuleBytes {
83 fn get_module_name(&self) -> &str {
84 &self.plugin_name
85 }
86
87 fn compile_module(&self, rt: &dyn runtime::Runtime) -> runtime::Module {
88 let cache = self.cache.as_ref().unwrap();
89 let cache = rt.clone_cache(cache).unwrap();
90 runtime::Module::Cache(cache)
91 }
92}