|
| 1 | +use crate::codegen::{CodeGen, CodeGenType, DynamicGenerator, StaticGenerator}; |
| 2 | +use anyhow::{bail, Result}; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +/// Options for using WIT in the code generation process. |
| 6 | +#[derive(Default)] |
| 7 | +pub(crate) struct WitOptions { |
| 8 | + /// The path of the .wit file to use. |
| 9 | + pub path: Option<PathBuf>, |
| 10 | + /// The name of the wit world to use. |
| 11 | + pub world: Option<String>, |
| 12 | +} |
| 13 | + |
| 14 | +impl WitOptions { |
| 15 | + pub fn from_tuple(opts: (Option<PathBuf>, Option<String>)) -> Result<Self> { |
| 16 | + match opts { |
| 17 | + (None, None) => Ok(Self { |
| 18 | + path: None, |
| 19 | + world: None, |
| 20 | + }), |
| 21 | + (None, Some(_)) => Ok(Self { |
| 22 | + path: None, |
| 23 | + world: None, |
| 24 | + }), |
| 25 | + (Some(_), None) => bail!("Must provide WIT world when providing WIT file"), |
| 26 | + (path, world) => Ok(Self { path, world }), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Whether WIT options were defined. |
| 31 | + pub fn defined(&self) -> bool { |
| 32 | + self.path.is_some() && self.world.is_some() |
| 33 | + } |
| 34 | + |
| 35 | + /// Unwraps a refernce to the .wit file path. |
| 36 | + pub fn unwrap_path(&self) -> &PathBuf { |
| 37 | + self.path.as_ref().unwrap() |
| 38 | + } |
| 39 | + |
| 40 | + /// Unwraps a reference to the WIT world name. |
| 41 | + pub fn unwrap_world(&self) -> &String { |
| 42 | + self.world.as_ref().unwrap() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// A code generation builder. |
| 47 | +#[derive(Default)] |
| 48 | +pub(crate) struct CodeGenBuilder { |
| 49 | + /// The QuickJS provider module version. |
| 50 | + provider_version: Option<&'static str>, |
| 51 | + /// WIT options for code generation. |
| 52 | + wit_opts: WitOptions, |
| 53 | + /// Whether to compress the original JS source. |
| 54 | + source_compression: bool, |
| 55 | +} |
| 56 | + |
| 57 | +impl CodeGenBuilder { |
| 58 | + /// Create a new [`CodeGenBuilder`]. |
| 59 | + pub fn new() -> Self { |
| 60 | + Self::default() |
| 61 | + } |
| 62 | + |
| 63 | + /// Set the provider version. |
| 64 | + pub fn provider_version(&mut self, v: &'static str) -> &mut Self { |
| 65 | + self.provider_version = Some(v); |
| 66 | + self |
| 67 | + } |
| 68 | + |
| 69 | + /// Set the wit options. |
| 70 | + pub fn wit_opts(&mut self, opts: WitOptions) -> &mut Self { |
| 71 | + self.wit_opts = opts; |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + /// Whether to compress the JS source. |
| 76 | + pub fn source_compression(&mut self, compress: bool) -> &mut Self { |
| 77 | + self.source_compression = compress; |
| 78 | + self |
| 79 | + } |
| 80 | + |
| 81 | + /// Build a [`CodeGenerator`]. |
| 82 | + pub fn build<T>(self) -> Result<Box<dyn CodeGen>> |
| 83 | + where |
| 84 | + T: CodeGen, |
| 85 | + { |
| 86 | + match T::classify() { |
| 87 | + CodeGenType::Static => self.build_static(), |
| 88 | + CodeGenType::Dynamic => self.build_dynamic(), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + fn build_static(self) -> Result<Box<dyn CodeGen>> { |
| 93 | + let mut static_gen = Box::new(StaticGenerator::new()); |
| 94 | + |
| 95 | + static_gen.source_compression = self.source_compression; |
| 96 | + static_gen.wit_opts = self.wit_opts; |
| 97 | + |
| 98 | + Ok(static_gen) |
| 99 | + } |
| 100 | + |
| 101 | + fn build_dynamic(self) -> Result<Box<dyn CodeGen>> { |
| 102 | + let mut dynamic_gen = Box::new(DynamicGenerator::new()); |
| 103 | + |
| 104 | + if let Some(v) = self.provider_version { |
| 105 | + dynamic_gen.import_namespace = String::from("javy_quickjs_provider_v"); |
| 106 | + dynamic_gen.import_namespace.push_str(v); |
| 107 | + } else { |
| 108 | + bail!("Provider version not specified") |
| 109 | + } |
| 110 | + |
| 111 | + dynamic_gen.wit_opts = self.wit_opts; |
| 112 | + |
| 113 | + Ok(dynamic_gen) |
| 114 | + } |
| 115 | +} |
0 commit comments