linera_wasmer_compiler/engine/
builder.rs1use super::Engine;
2use crate::CompilerConfig;
3use wasmer_types::{Features, HashAlgorithm, Target};
4
5pub struct EngineBuilder {
7 compiler_config: Option<Box<dyn CompilerConfig>>,
9 target: Option<Target>,
11 features: Option<Features>,
13 hash_algorithm: Option<HashAlgorithm>,
15}
16
17impl EngineBuilder {
18 pub fn new<T>(compiler_config: T) -> Self
20 where
21 T: Into<Box<dyn CompilerConfig>>,
22 {
23 Self {
24 compiler_config: Some(compiler_config.into()),
25 target: None,
26 features: None,
27 hash_algorithm: None,
28 }
29 }
30
31 pub fn headless() -> Self {
33 Self {
34 compiler_config: None,
35 target: None,
36 features: None,
37 hash_algorithm: None,
38 }
39 }
40
41 pub fn set_target(mut self, target: Option<Target>) -> Self {
43 self.target = target;
44 self
45 }
46
47 pub fn set_features(mut self, features: Option<Features>) -> Self {
49 self.features = features;
50 self
51 }
52
53 pub fn set_hash_algorithm(mut self, hash_algorithm: Option<HashAlgorithm>) -> Self {
55 self.hash_algorithm = hash_algorithm;
56 self
57 }
58
59 #[cfg(feature = "compiler")]
61 pub fn engine(self) -> Engine {
62 let target = self.target.unwrap_or_default();
63 if let Some(compiler_config) = self.compiler_config {
64 let features = self
65 .features
66 .unwrap_or_else(|| compiler_config.default_features_for_target(&target));
67 let mut engine = Engine::new(compiler_config, target, features);
68
69 engine.set_hash_algorithm(self.hash_algorithm);
70
71 engine
72 } else {
73 Engine::headless()
74 }
75 }
76
77 #[cfg(not(feature = "compiler"))]
79 pub fn engine(self) -> Engine {
80 Engine::headless()
81 }
82
83 pub fn features(&self) -> Option<&Features> {
85 self.features.as_ref()
86 }
87
88 pub fn target(&self) -> Option<&Target> {
90 self.target.as_ref()
91 }
92}