mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-23 05:51:39 +00:00
Refactor code for improved readability by consolidating multi-line statements into single lines across various modules, including JSON serialization, snapshot middleware, and file name validation.
This commit is contained in:
parent
4caac7a31c
commit
2dfcac4ef7
8 changed files with 32 additions and 25 deletions
|
|
@ -166,7 +166,6 @@ pub fn to_vec_pretty_sorted<T: Serialize>(value: &T) -> anyhow::Result<Vec<u8>>
|
||||||
serde_json::to_vec_pretty(&sorted_value).context("Failed to serialize to JSON")
|
serde_json::to_vec_pretty(&sorted_value).context("Failed to serialize to JSON")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
|
|
@ -57,10 +57,7 @@ impl AdjacentMetadata {
|
||||||
// when encodeWindowsInvalidChars is enabled.
|
// when encodeWindowsInvalidChars is enabled.
|
||||||
// Strip known script type suffixes (.server, .client, .plugin) to get
|
// Strip known script type suffixes (.server, .client, .plugin) to get
|
||||||
// the base name for the meta file.
|
// the base name for the meta file.
|
||||||
let file_stem = path
|
let file_stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
|
||||||
.file_stem()
|
|
||||||
.and_then(|s| s.to_str())
|
|
||||||
.unwrap_or("");
|
|
||||||
let base_name = file_stem
|
let base_name = file_stem
|
||||||
.strip_suffix(".server")
|
.strip_suffix(".server")
|
||||||
.or_else(|| file_stem.strip_suffix(".client"))
|
.or_else(|| file_stem.strip_suffix(".client"))
|
||||||
|
|
|
||||||
|
|
@ -58,11 +58,7 @@ pub fn syncback_rbxm<'sync>(
|
||||||
serialized
|
serialized
|
||||||
} else {
|
} else {
|
||||||
// Clone the subtree, filtering out ignored instances
|
// Clone the subtree, filtering out ignored instances
|
||||||
let filtered_tree = clone_tree_filtered(
|
let filtered_tree = clone_tree_filtered(snapshot.new_tree(), inst.referent(), &tree_globs);
|
||||||
snapshot.new_tree(),
|
|
||||||
inst.referent(),
|
|
||||||
&tree_globs,
|
|
||||||
);
|
|
||||||
let mut serialized = Vec::new();
|
let mut serialized = Vec::new();
|
||||||
rbx_binary::to_writer(&mut serialized, &filtered_tree, &[filtered_tree.root_ref()])
|
rbx_binary::to_writer(&mut serialized, &filtered_tree, &[filtered_tree.root_ref()])
|
||||||
.context("failed to serialize filtered rbxm")?;
|
.context("failed to serialize filtered rbxm")?;
|
||||||
|
|
@ -99,7 +95,9 @@ fn add_children_filtered(
|
||||||
builder: &mut InstanceBuilder,
|
builder: &mut InstanceBuilder,
|
||||||
ignore_globs: &[Glob],
|
ignore_globs: &[Glob],
|
||||||
) {
|
) {
|
||||||
let parent = source.get_by_ref(parent_ref).expect("parent ref should exist");
|
let parent = source
|
||||||
|
.get_by_ref(parent_ref)
|
||||||
|
.expect("parent ref should exist");
|
||||||
|
|
||||||
for &child_ref in parent.children() {
|
for &child_ref in parent.children() {
|
||||||
let child_path = inst_path(source, child_ref);
|
let child_path = inst_path(source, child_ref);
|
||||||
|
|
@ -112,7 +110,9 @@ fn add_children_filtered(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let child = source.get_by_ref(child_ref).expect("child ref should exist");
|
let child = source
|
||||||
|
.get_by_ref(child_ref)
|
||||||
|
.expect("child ref should exist");
|
||||||
|
|
||||||
let mut child_builder = InstanceBuilder::new(child.class)
|
let mut child_builder = InstanceBuilder::new(child.class)
|
||||||
.with_name(child.name.clone())
|
.with_name(child.name.clone())
|
||||||
|
|
|
||||||
|
|
@ -69,11 +69,7 @@ pub fn syncback_rbxmx<'sync>(
|
||||||
serialized
|
serialized
|
||||||
} else {
|
} else {
|
||||||
// Clone the subtree, filtering out ignored instances
|
// Clone the subtree, filtering out ignored instances
|
||||||
let filtered_tree = clone_tree_filtered(
|
let filtered_tree = clone_tree_filtered(snapshot.new_tree(), inst.referent(), &tree_globs);
|
||||||
snapshot.new_tree(),
|
|
||||||
inst.referent(),
|
|
||||||
&tree_globs,
|
|
||||||
);
|
|
||||||
let mut serialized = Vec::new();
|
let mut serialized = Vec::new();
|
||||||
rbx_xml::to_writer(
|
rbx_xml::to_writer(
|
||||||
&mut serialized,
|
&mut serialized,
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,10 @@ pub fn name_for_inst<'old>(
|
||||||
encoded
|
encoded
|
||||||
} else {
|
} else {
|
||||||
validate_file_name(&new_inst.name).with_context(|| {
|
validate_file_name(&new_inst.name).with_context(|| {
|
||||||
format!("name '{}' is not legal to write to the file system", new_inst.name)
|
format!(
|
||||||
|
"name '{}' is not legal to write to the file system",
|
||||||
|
new_inst.name
|
||||||
|
)
|
||||||
})?;
|
})?;
|
||||||
new_inst.name.clone()
|
new_inst.name.clone()
|
||||||
};
|
};
|
||||||
|
|
@ -65,7 +68,10 @@ pub fn name_for_inst<'old>(
|
||||||
encoded
|
encoded
|
||||||
} else {
|
} else {
|
||||||
validate_file_name(&new_inst.name).with_context(|| {
|
validate_file_name(&new_inst.name).with_context(|| {
|
||||||
format!("name '{}' is not legal to write to the file system", new_inst.name)
|
format!(
|
||||||
|
"name '{}' is not legal to write to the file system",
|
||||||
|
new_inst.name
|
||||||
|
)
|
||||||
})?;
|
})?;
|
||||||
new_inst.name.clone()
|
new_inst.name.clone()
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -369,8 +369,8 @@ pub fn get_best_middleware(snapshot: &SyncbackSnapshot) -> Middleware {
|
||||||
middleware = Middleware::JsonModel;
|
middleware = Middleware::JsonModel;
|
||||||
} else {
|
} else {
|
||||||
middleware = match inst.class.as_str() {
|
middleware = match inst.class.as_str() {
|
||||||
"Folder" | "Configuration" | "Tool"
|
"Folder" | "Configuration" | "Tool" | "ScreenGui" | "SurfaceGui" | "BillboardGui"
|
||||||
| "ScreenGui" | "SurfaceGui" | "BillboardGui" | "AdGui" => Middleware::Dir,
|
| "AdGui" => Middleware::Dir,
|
||||||
"StringValue" => Middleware::Text,
|
"StringValue" => Middleware::Text,
|
||||||
"Script" => Middleware::ServerScript,
|
"Script" => Middleware::ServerScript,
|
||||||
"LocalScript" => Middleware::ClientScript,
|
"LocalScript" => Middleware::ClientScript,
|
||||||
|
|
@ -478,8 +478,9 @@ impl SyncbackRules {
|
||||||
let mut globs = Vec::with_capacity(self.ignore_trees.len());
|
let mut globs = Vec::with_capacity(self.ignore_trees.len());
|
||||||
|
|
||||||
for pattern in &self.ignore_trees {
|
for pattern in &self.ignore_trees {
|
||||||
let glob = Glob::new(pattern)
|
let glob = Glob::new(pattern).with_context(|| {
|
||||||
.with_context(|| format!("the pattern '{pattern}' is not a valid ignoreTrees glob"))?;
|
format!("the pattern '{pattern}' is not a valid ignoreTrees glob")
|
||||||
|
})?;
|
||||||
globs.push(glob);
|
globs.push(glob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,9 @@ impl<'sync> SyncbackSnapshot<'sync> {
|
||||||
// Try glob matching first, fall back to prefix matching for backwards compatibility
|
// Try glob matching first, fall back to prefix matching for backwards compatibility
|
||||||
if let Ok(glob) = Glob::new(pattern) {
|
if let Ok(glob) = Glob::new(pattern) {
|
||||||
if glob.is_match(&path) {
|
if glob.is_match(&path) {
|
||||||
log::debug!("Ignoring {path} because it matches ignoreTrees glob: {pattern}");
|
log::debug!(
|
||||||
|
"Ignoring {path} because it matches ignoreTrees glob: {pattern}"
|
||||||
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if path.starts_with(pattern.as_str()) {
|
} else if path.starts_with(pattern.as_str()) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,13 @@
|
||||||
//! Defines Rojo's HTTP API, all under /api. These endpoints generally return
|
//! Defines Rojo's HTTP API, all under /api. These endpoints generally return
|
||||||
//! JSON.
|
//! JSON.
|
||||||
|
|
||||||
use std::{collections::{HashMap, HashSet}, fs, path::PathBuf, str::FromStr, sync::Arc};
|
use std::{
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
fs,
|
||||||
|
path::PathBuf,
|
||||||
|
str::FromStr,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use futures::{sink::SinkExt, stream::StreamExt};
|
use futures::{sink::SinkExt, stream::StreamExt};
|
||||||
use hyper::{body, Body, Method, Request, Response, StatusCode};
|
use hyper::{body, Body, Method, Request, Response, StatusCode};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue