dalbit_core/
manifest.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use anyhow::Result;
use indexmap::IndexMap;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tokio::fs;

use crate::{polyfill::Polyfill, TargetVersion};

#[async_trait::async_trait]
pub trait WritableManifest: Send + Sized + Serialize + DeserializeOwned {
    #[inline]
    async fn from_file(path: impl Into<PathBuf> + Send) -> Result<Self> {
        let content = fs::read_to_string(path.into()).await?;

        Ok(toml::from_str(content.as_str())?)
    }

    #[inline]
    async fn write(&self, path: impl Into<PathBuf> + Send) -> Result<()> {
        fs::write(path.into(), toml::to_string(self)?).await?;

        Ok(())
    }
}

/// Manifest for dalbit transpiler. This is a writable manifest.
#[derive(Debug, Deserialize, Serialize)]
pub struct Manifest {
    input: PathBuf,
    output: PathBuf,
    file_extension: Option<String>,
    target_version: TargetVersion,
    pub minify: bool,
    modifiers: IndexMap<String, bool>,
    polyfill: Polyfill,
}

impl Default for Manifest {
    fn default() -> Self {
        Self {
            input: Path::new("input.luau").to_owned(),
            output: Path::new("output.lua").to_owned(),
            file_extension: Some("lua".to_owned()),
            target_version: TargetVersion::Lua53,
            minify: true,
            modifiers: IndexMap::new(),
            polyfill: Polyfill::default(),
        }
    }
}

impl WritableManifest for Manifest {}

impl Manifest {
    #[inline]
    pub fn input(&self) -> &PathBuf {
        &self.input
    }

    #[inline]
    pub fn output(&self) -> &PathBuf {
        &self.output
    }

    #[inline]
    pub fn file_extension(&self) -> &Option<String> {
        &self.file_extension
    }

    #[inline]
    pub fn modifiers(&self) -> &IndexMap<String, bool> {
        &self.modifiers
    }

    #[inline]
    pub fn target_version(&self) -> &TargetVersion {
        &self.target_version
    }

    #[inline]
    pub fn polyfill(&self) -> &Polyfill {
        &self.polyfill
    }
}