Skip to content

Commit

Permalink
Merge branch 'add-toml-support' into fork-releases
Browse files Browse the repository at this point in the history
  • Loading branch information
Corecii committed Oct 11, 2022
2 parents 3c564e9 + 74a43b5 commit 42c0858
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ritz = "0.1.0"
roblox_install = "1.0.0"
serde = { version = "1.0.130", features = ["derive", "rc"] }
serde_json = "1.0.68"
toml = "0.5.9"
termcolor = "1.1.2"
thiserror = "1.0.30"
tokio = { version = "1.12.0", features = ["rt", "rt-multi-thread"] }
Expand Down
2 changes: 2 additions & 0 deletions src/snapshot/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub enum Transformer {
LuauServer,
LuauClient,
Json,
Toml,
Csv,

Project,
Expand All @@ -214,6 +215,7 @@ impl Transformer {
"rojo/luauserver" => Self::LuauServer,
"rojo/luauclient" => Self::LuauClient,
"rojo/json" => Self::Json,
"rojo/toml" => Self::Toml,
"rojo/csv" => Self::Csv,

"rojo/project" => Self::Project,
Expand Down
3 changes: 3 additions & 0 deletions src/snapshot_middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod meta_file;
mod project;
mod rbxm;
mod rbxmx;
mod toml;
mod txt;
mod util;

Expand All @@ -33,6 +34,7 @@ use self::{
project::snapshot_project,
rbxm::snapshot_rbxm,
rbxmx::snapshot_rbxmx,
toml::snapshot_toml,
txt::snapshot_txt,
util::PathExt,
};
Expand Down Expand Up @@ -173,6 +175,7 @@ pub fn snapshot_from_vfs(
Some(Transformer::Project) => snapshot_project(context, vfs, path),
Some(Transformer::JsonModel) => snapshot_json_model(context, vfs, path),
Some(Transformer::Json) => snapshot_json(context, vfs, path),
Some(Transformer::Toml) => snapshot_toml(context, vfs, path),
Some(Transformer::Csv) => snapshot_csv(context, vfs, path),
Some(Transformer::Plain) => snapshot_txt(context, vfs, path),
Some(Transformer::LuauModule) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: src/snapshot_middleware/toml.rs
assertion_line: 121
expression: instance_snapshot
---
snapshot_id: ~
metadata:
ignore_unknown_instances: false
instigating_source:
Path: /foo.toml
relevant_paths:
- /foo.toml
- /foo.meta.json
context: {}
name: foo
class_name: ModuleScript
properties:
Source:
String: "return {\n\t[\"1invalidident\"] = \"nice\",\n\tarray = {1, 2, 3},\n\tdates = {\n\t\tlocaldate = \"1979-05-27\",\n\t\tlocaldatetime = \"1979-05-27T07:32:00\",\n\t\tlocaltime = \"00:32:00.999999\",\n\t\toffset1 = \"1979-05-27T00:32:00.999999-07:00\",\n\t\toffset2 = \"1979-05-27T07:32:00Z\",\n\t},\n\t[\"false\"] = false,\n\tfloat = 1234.5452,\n\tint = 1234,\n\tobject = {\n\t\thello = \"world\",\n\t},\n\t[\"true\"] = true,\n}"
children: []

123 changes: 123 additions & 0 deletions src/snapshot_middleware/toml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::path::Path;

use anyhow::Context;
use maplit::hashmap;
use memofs::{IoResultExt, Vfs};

use crate::{
lua_ast::{Expression, Statement},
snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot},
};

use super::{meta_file::AdjacentMetadata, util::PathExt};

pub fn snapshot_toml(
context: &InstanceContext,
vfs: &Vfs,
path: &Path,
) -> anyhow::Result<Option<InstanceSnapshot>> {
let name = path.file_name_trim_end(".toml")?;
let contents = vfs.read(path)?;

let value: toml::Value = toml::from_slice(&contents)
.with_context(|| format!("File contains malformed TOML: {}", path.display()))?;

let as_lua = toml_to_lua(value).to_string();

let properties = hashmap! {
"Source".to_owned() => as_lua.into(),
};

let meta_path = path.with_file_name(format!("{}.meta.json", name));

let mut snapshot = InstanceSnapshot::new()
.name(name)
.class_name("ModuleScript")
.properties(properties)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
.relevant_paths(vec![path.to_path_buf(), meta_path.clone()])
.context(context),
);

if let Some(meta_contents) = vfs.read(&meta_path).with_not_found()? {
let mut metadata = AdjacentMetadata::from_slice(&meta_contents, meta_path)?;
metadata.apply_all(&mut snapshot)?;
}

Ok(Some(snapshot))
}

fn toml_to_lua(value: toml::Value) -> Statement {
Statement::Return(toml_to_lua_value(value))
}

fn toml_to_lua_value(value: toml::Value) -> Expression {
use toml::Value;

match value {
Value::Datetime(value) => Expression::String(value.to_string()),
Value::Boolean(value) => Expression::Bool(value),
Value::Float(value) => Expression::Number(value),
Value::Integer(value) => Expression::Number(value as f64),
Value::String(value) => Expression::String(value),
Value::Array(values) => {
Expression::Array(values.into_iter().map(toml_to_lua_value).collect())
}
Value::Table(values) => Expression::table(
values
.into_iter()
.map(|(key, value)| (key.into(), toml_to_lua_value(value)))
.collect(),
),
}
}

#[cfg(test)]
mod test {
use super::*;

use memofs::{InMemoryFs, VfsSnapshot};

#[test]
fn instance_from_vfs() {
let mut imfs = InMemoryFs::new();
imfs.load_snapshot(
"/foo.toml",
VfsSnapshot::file(
r#"
array = [1, 2, 3]
true = true
false = false
int = 1234
float = 1234.5452
"1invalidident" = "nice"
[object]
hello = "world"
[dates]
offset1 = 1979-05-27T00:32:00.999999-07:00
offset2 = 1979-05-27 07:32:00Z
localdatetime = 1979-05-27T07:32:00
localdate = 1979-05-27
localtime = 00:32:00.999999
"#,
),
)
.unwrap();

let mut vfs = Vfs::new(imfs.clone());

let instance_snapshot = snapshot_toml(
&InstanceContext::default(),
&mut vfs,
Path::new("/foo.toml"),
)
.unwrap()
.unwrap();

insta::assert_yaml_snapshot!(instance_snapshot);
}
}

0 comments on commit 42c0858

Please sign in to comment.