Skip to content

Commit

Permalink
Apply and enforce rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacdonald committed Feb 19, 2024
1 parent 7696148 commit 9b3e7c2
Show file tree
Hide file tree
Showing 74 changed files with 2,905 additions and 2,097 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: rustfmt
run: rustfmt --check src/**/*.rs
85 changes: 52 additions & 33 deletions benches/view/draw_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,67 @@ use std::path::PathBuf;

fn buffer_rendering(c: &mut Criterion) {
let mut app = Application::new(&Vec::new()).unwrap();
app.workspace.open_buffer(
&PathBuf::from("src/commands/buffer.rs")
).unwrap();
app.view.initialize_buffer(app.workspace.current_buffer.as_mut().unwrap()).unwrap();
app.workspace
.open_buffer(&PathBuf::from("src/commands/buffer.rs"))
.unwrap();
app.view
.initialize_buffer(app.workspace.current_buffer.as_mut().unwrap())
.unwrap();
let buffer_data = app.workspace.current_buffer.as_ref().unwrap().data();

c.bench_function("buffer rendering", move |b| b.iter(|| {
let mut presenter = app.view.build_presenter().unwrap();

presenter.print_buffer(
app.workspace.current_buffer.as_ref().unwrap(),
&buffer_data,
&app.workspace.syntax_set,
None,
None
).unwrap()
}));
c.bench_function("buffer rendering", move |b| {
b.iter(|| {
let mut presenter = app.view.build_presenter().unwrap();

presenter
.print_buffer(
app.workspace.current_buffer.as_ref().unwrap(),
&buffer_data,
&app.workspace.syntax_set,
None,
None,
)
.unwrap()
})
});
}

fn scrolled_buffer_rendering(c: &mut Criterion) {
let mut app = Application::new(&Vec::new()).unwrap();
app.workspace.open_buffer(
&PathBuf::from("src/commands/buffer.rs")
).unwrap();
app.view.initialize_buffer(app.workspace.current_buffer.as_mut().unwrap()).unwrap();
app.workspace
.open_buffer(&PathBuf::from("src/commands/buffer.rs"))
.unwrap();
app.view
.initialize_buffer(app.workspace.current_buffer.as_mut().unwrap())
.unwrap();
let buffer_data = app.workspace.current_buffer.as_ref().unwrap().data();

// Scroll to the bottom of the buffer.
app.workspace.current_buffer.as_mut().unwrap().cursor.move_to_last_line();
app.view.scroll_to_cursor(app.workspace.current_buffer.as_ref().unwrap()).unwrap();

c.bench_function("scrolled buffer rendering", move |b| b.iter(|| {
let mut presenter = app.view.build_presenter().unwrap();

presenter.print_buffer(
app.workspace.current_buffer.as_ref().unwrap(),
&buffer_data,
&app.workspace.syntax_set,
None,
None
).unwrap()
}));
app.workspace
.current_buffer
.as_mut()
.unwrap()
.cursor
.move_to_last_line();
app.view
.scroll_to_cursor(app.workspace.current_buffer.as_ref().unwrap())
.unwrap();

c.bench_function("scrolled buffer rendering", move |b| {
b.iter(|| {
let mut presenter = app.view.build_presenter().unwrap();

presenter
.print_buffer(
app.workspace.current_buffer.as_ref().unwrap(),
&buffer_data,
&app.workspace.syntax_set,
None,
None,
)
.unwrap()
})
});
}

criterion_group!(benches, buffer_rendering, scrolled_buffer_rendering);
Expand Down
69 changes: 40 additions & 29 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use regex::Regex;
use std::env;
use std::fs::{self, File, read_to_string};
use std::fs::{self, read_to_string, File};
use std::io::Write;
use std::path::Path;
use std::result::Result;

const COMMAND_REGEX: &str =
r"pub fn (.*)\(app: &mut Application\) -> Result";
const COMMAND_REGEX: &str = r"pub fn (.*)\(app: &mut Application\) -> Result";

fn main() {
generate_commands();
Expand All @@ -27,27 +26,29 @@ fn create_output_file() -> Result<File, String> {
let out_dir = env::var("OUT_DIR").expect("The compiler did not provide $OUT_DIR");
let out_file: std::path::PathBuf = [&out_dir, "hash_map"].iter().collect();
let mut file = File::create(&out_file).map_err(|_| {
format!("Couldn't create output file: {}", out_file.to_string_lossy())
format!(
"Couldn't create output file: {}",
out_file.to_string_lossy()
)
})?;
file
.write("{\n let mut commands: HashMap<&'static str, Command> = HashMap::new();\n"
.as_bytes())
.map_err(|_| "Failed to write command hash init")?;
file.write(
"{\n let mut commands: HashMap<&'static str, Command> = HashMap::new();\n".as_bytes(),
)
.map_err(|_| "Failed to write command hash init")?;

Ok(file)
}

fn write_commands(output: &mut File) -> Result<(), &str> {
let expression = Regex::new(COMMAND_REGEX)
.expect("Failed to compile command matching regex");
let entries = fs::read_dir("./src/commands/")
.map_err(|_| "Failed to read command module directory")?;
let expression = Regex::new(COMMAND_REGEX).expect("Failed to compile command matching regex");
let entries =
fs::read_dir("./src/commands/").map_err(|_| "Failed to read command module directory")?;
for entry in entries {
let path = entry
.map_err(|_| "Failed to read command module directory entry")?.path();
.map_err(|_| "Failed to read command module directory entry")?
.path();
let module_name = module_name(&path).unwrap();
let content = read_to_string(&path)
.map_err(|_| "Failed to read command module data")?;
let content = read_to_string(&path).map_err(|_| "Failed to read command module data")?;
for captures in expression.captures_iter(&content) {
let function_name = captures.get(1).unwrap().as_str();
write_command(output, &module_name, function_name)?;
Expand All @@ -57,25 +58,35 @@ fn write_commands(output: &mut File) -> Result<(), &str> {
Ok(())
}

fn write_command(output: &mut File, module_name: &str, function_name: &str) -> Result<usize, &'static str> {
output.write(
format!(
" commands.insert(\"{}::{}\", {}::{});\n",
module_name,
function_name,
module_name,
function_name
).as_bytes()
).map_err(|_| "Failed to write command")
fn write_command(
output: &mut File,
module_name: &str,
function_name: &str,
) -> Result<usize, &'static str> {
output
.write(
format!(
" commands.insert(\"{}::{}\", {}::{});\n",
module_name, function_name, module_name, function_name
)
.as_bytes(),
)
.map_err(|_| "Failed to write command")
}

fn finalize_output_file(output: &mut File) -> Result<usize, &str> {
output.write(" commands\n}\n".as_bytes())
output
.write(" commands\n}\n".as_bytes())
.map_err(|_| "Failed to write command hash return")
}

fn module_name(path: &Path) -> Result<String, &str> {
path.file_name().and_then(|name| {
name.to_string_lossy().split('.').next().map(|n| n.to_string())
}).ok_or("Unable to parse command module from file name")
path.file_name()
.and_then(|name| {
name.to_string_lossy()
.split('.')
.next()
.map(|n| n.to_string())
})
.ok_or("Unable to parse command module from file name")
}
Loading

0 comments on commit 9b3e7c2

Please sign in to comment.