Skip to content

Commit

Permalink
Rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampfkarren committed Jan 7, 2021
1 parent 3736f79 commit b85fa8b
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 266 deletions.
220 changes: 111 additions & 109 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,109 +1,111 @@
use crate::structures::*;
use serde::{ser::SerializeMap, Serialize, Serializer};
use std::{
collections::BTreeMap,
fs::{self, File},
io::Write,
path::PathBuf,
};

const SRC: &str = "src";

fn serialize_project_tree<S: Serializer>(
tree: &BTreeMap<String, TreePartition>,
serializer: S,
) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(Some(tree.len() + 1))?;
map.serialize_entry("$className", "DataModel")?;
for (k, v) in tree {
map.serialize_entry(k, v)?;
}
map.end()
}

#[derive(Clone, Debug, Serialize)]
struct Project {
name: String,
#[serde(serialize_with = "serialize_project_tree")]
tree: BTreeMap<String, TreePartition>,
}

impl Project {
fn new() -> Self {
Self {
name: "project".to_string(),
tree: BTreeMap::new(),
}
}
}

#[derive(Clone, Debug)]
pub struct FileSystem {
project: Project,
root: PathBuf,
source: PathBuf,
}

impl FileSystem {
pub fn from_root(root: PathBuf) -> Self {
let source = root.join(SRC);
let project = Project::new();

fs::create_dir(&source).ok(); // It'll error later if it matters

Self {
project,
root,
source,
}
}
}

impl InstructionReader for FileSystem {
fn read_instruction<'a>(&mut self, instruction: Instruction<'a>) {
match instruction {
Instruction::AddToTree {
name,
mut partition,
} => {
assert!(
self.project.tree.get(&name).is_none(),
"Duplicate item added to tree! Instances can't have the same name: {}",
name
);

if let Some(path) = partition.path {
partition.path = Some(PathBuf::from(SRC).join(path));
}

for mut child in partition.children.values_mut() {
if let Some(path) = &child.path {
child.path = Some(PathBuf::from(SRC).join(path));
}
}

self.project.tree.insert(name, partition);
}

Instruction::CreateFile { filename, contents } => {
let mut file = File::create(self.source.join(&filename)).unwrap_or_else(|error| panic!("can't create file {:?}: {:?}", filename, error));
file.write_all(&contents).expect("can't write file");
}

Instruction::CreateFolder { folder } => {
fs::create_dir_all(self.source.join(folder)).expect("can't write folder");
}
}
}

fn finish_instructions(&mut self) {
let mut file = File::create(self.root.join("default.project.json"))
.expect("can't create default.project.json");
file.write_all(
&serde_json::to_string_pretty(&self.project)
.expect("couldn't serialize project")
.as_bytes(),
)
.expect("can't write project");
}
}
use crate::structures::*;
use serde::{ser::SerializeMap, Serialize, Serializer};
use std::{
collections::BTreeMap,
fs::{self, File},
io::Write,
path::PathBuf,
};

const SRC: &str = "src";

fn serialize_project_tree<S: Serializer>(
tree: &BTreeMap<String, TreePartition>,
serializer: S,
) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(Some(tree.len() + 1))?;
map.serialize_entry("$className", "DataModel")?;
for (k, v) in tree {
map.serialize_entry(k, v)?;
}
map.end()
}

#[derive(Clone, Debug, Serialize)]
struct Project {
name: String,
#[serde(serialize_with = "serialize_project_tree")]
tree: BTreeMap<String, TreePartition>,
}

impl Project {
fn new() -> Self {
Self {
name: "project".to_string(),
tree: BTreeMap::new(),
}
}
}

#[derive(Clone, Debug)]
pub struct FileSystem {
project: Project,
root: PathBuf,
source: PathBuf,
}

impl FileSystem {
pub fn from_root(root: PathBuf) -> Self {
let source = root.join(SRC);
let project = Project::new();

fs::create_dir(&source).ok(); // It'll error later if it matters

Self {
project,
root,
source,
}
}
}

impl InstructionReader for FileSystem {
fn read_instruction<'a>(&mut self, instruction: Instruction<'a>) {
match instruction {
Instruction::AddToTree {
name,
mut partition,
} => {
assert!(
self.project.tree.get(&name).is_none(),
"Duplicate item added to tree! Instances can't have the same name: {}",
name
);

if let Some(path) = partition.path {
partition.path = Some(PathBuf::from(SRC).join(path));
}

for mut child in partition.children.values_mut() {
if let Some(path) = &child.path {
child.path = Some(PathBuf::from(SRC).join(path));
}
}

self.project.tree.insert(name, partition);
}

Instruction::CreateFile { filename, contents } => {
let mut file = File::create(self.source.join(&filename)).unwrap_or_else(|error| {
panic!("can't create file {:?}: {:?}", filename, error)
});
file.write_all(&contents).expect("can't write file");
}

Instruction::CreateFolder { folder } => {
fs::create_dir_all(self.source.join(folder)).expect("can't write folder");
}
}
}

fn finish_instructions(&mut self) {
let mut file = File::create(self.root.join("default.project.json"))
.expect("can't create default.project.json");
file.write_all(
&serde_json::to_string_pretty(&self.project)
.expect("couldn't serialize project")
.as_bytes(),
)
.expect("can't write project");
}
}
110 changes: 56 additions & 54 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ fn repr_instance<'a>(
.into(),
);

let script_children_count = child.get_children_ids()
let script_children_count = child
.get_children_ids()
.iter()
.filter(|id| has_scripts.get(id) == Some(&true))
.count();
Expand All @@ -107,57 +108,57 @@ fn repr_instance<'a>(
// If there's only script children, don't bother with a meta file at all
// TODO: Lot of redundant code here
match script_children_count {
_ if script_children_count == total_children_count => {
Some((
vec![
Instruction::CreateFolder {
folder: folder_path.clone(),
},
Instruction::CreateFile {
filename: Cow::Owned(folder_path.join(format!("init{}.lua", extension))),
contents: Cow::Borrowed(source),
}
],
folder_path
))
},

0 => {
Some((
vec![
Instruction::CreateFile {
filename: Cow::Owned(base.join(format!("{}{}.lua", child.name, extension))),
contents: Cow::Borrowed(source),
},
Instruction::CreateFile {
filename: Cow::Owned(base.join(format!("{}.meta.json", child.name))),
contents: meta_contents,
},
],
Cow::Borrowed(base)
))
},

_ => {
Some((
vec![
Instruction::CreateFolder {
folder: folder_path.clone(),
},
Instruction::CreateFile {
filename: Cow::Owned(
folder_path.join(format!("init{}.lua", extension)),
),
contents: Cow::Borrowed(source),
},
Instruction::CreateFile {
filename: Cow::Owned(folder_path.join("init.meta.json")),
contents: meta_contents,
},
],
folder_path
))
},
_ if script_children_count == total_children_count => Some((
vec![
Instruction::CreateFolder {
folder: folder_path.clone(),
},
Instruction::CreateFile {
filename: Cow::Owned(
folder_path.join(format!("init{}.lua", extension)),
),
contents: Cow::Borrowed(source),
},
],
folder_path,
)),

0 => Some((
vec![
Instruction::CreateFile {
filename: Cow::Owned(
base.join(format!("{}{}.lua", child.name, extension)),
),
contents: Cow::Borrowed(source),
},
Instruction::CreateFile {
filename: Cow::Owned(
base.join(format!("{}.meta.json", child.name)),
),
contents: meta_contents,
},
],
Cow::Borrowed(base),
)),

_ => Some((
vec![
Instruction::CreateFolder {
folder: folder_path.clone(),
},
Instruction::CreateFile {
filename: Cow::Owned(
folder_path.join(format!("init{}.lua", extension)),
),
contents: Cow::Borrowed(source),
},
Instruction::CreateFile {
filename: Cow::Owned(folder_path.join("init.meta.json")),
contents: meta_contents,
},
],
folder_path,
)),
}
}
}
Expand Down Expand Up @@ -189,7 +190,8 @@ fn repr_instance<'a>(
let mut instructions = Vec::new();

if !NON_TREE_SERVICES.contains(other_class) {
instructions.push(Instruction::add_to_tree(&child, new_base.to_path_buf()));
instructions
.push(Instruction::add_to_tree(&child, new_base.to_path_buf()));
}

if !child.get_children_ids().is_empty() {
Expand Down Expand Up @@ -348,7 +350,7 @@ fn check_has_scripts(
result
}

pub fn process_instructions(tree: &RbxTree, instruction_reader: &mut InstructionReader) {
pub fn process_instructions(tree: &RbxTree, instruction_reader: &mut dyn InstructionReader) {
let root = tree.get_root_id();
let root_instance = tree.get_instance(root).expect("fake root id?");
let path = PathBuf::new();
Expand Down
Loading

0 comments on commit b85fa8b

Please sign in to comment.