dalbit_core/
manifest.rs

1use anyhow::Result;
2use indexmap::IndexMap;
3use serde::{de::DeserializeOwned, Deserialize, Serialize};
4use std::path::{Path, PathBuf};
5use tokio::fs;
6
7use crate::{polyfill::Polyfill, TargetVersion};
8
9#[async_trait::async_trait]
10pub trait WritableManifest: Send + Sized + Serialize + DeserializeOwned {
11    #[inline]
12    async fn from_file(path: impl Into<PathBuf> + Send) -> Result<Self> {
13        let content = fs::read_to_string(path.into()).await?;
14
15        Ok(toml::from_str(content.as_str())?)
16    }
17
18    #[inline]
19    async fn write(&self, path: impl Into<PathBuf> + Send) -> Result<()> {
20        fs::write(path.into(), toml::to_string(self)?).await?;
21
22        Ok(())
23    }
24}
25
26/// Manifest for dalbit transpiler. This is a writable manifest.
27#[derive(Debug, Deserialize, Serialize)]
28pub struct Manifest {
29    input: PathBuf,
30    output: PathBuf,
31    file_extension: Option<String>,
32    target_version: TargetVersion,
33    pub minify: bool,
34    modifiers: IndexMap<String, bool>,
35    polyfill: Polyfill,
36}
37
38impl Default for Manifest {
39    fn default() -> Self {
40        Self {
41            input: Path::new("input.luau").to_owned(),
42            output: Path::new("output.lua").to_owned(),
43            file_extension: Some("lua".to_owned()),
44            target_version: TargetVersion::Lua53,
45            minify: true,
46            modifiers: IndexMap::new(),
47            polyfill: Polyfill::default(),
48        }
49    }
50}
51
52impl WritableManifest for Manifest {}
53
54impl Manifest {
55    #[inline]
56    pub fn input(&self) -> &PathBuf {
57        &self.input
58    }
59
60    #[inline]
61    pub fn output(&self) -> &PathBuf {
62        &self.output
63    }
64
65    #[inline]
66    pub fn file_extension(&self) -> &Option<String> {
67        &self.file_extension
68    }
69
70    #[inline]
71    pub fn modifiers(&self) -> &IndexMap<String, bool> {
72        &self.modifiers
73    }
74
75    #[inline]
76    pub fn target_version(&self) -> &TargetVersion {
77        &self.target_version
78    }
79
80    #[inline]
81    pub fn polyfill(&self) -> &Polyfill {
82        &self.polyfill
83    }
84}