Skip to content

Commit

Permalink
Use cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Oct 25, 2023
1 parent ac69376 commit 08ef0ce
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 122 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/rust-bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
working-directory: rust/${{ matrix.crate }}
run: cargo test

cargo-clippy:
lint:
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -83,6 +83,9 @@ jobs:
- name: cargo clippy
working-directory: rust/${{ matrix.crate }}
run: cargo clippy --tests -- -W "clippy::pedantic"
- name: cargo fmt
working-directory: rust/${{ matrix.crate }}
run: cargo fmt --check

sanitizer-test:
name: Test with -Zsanitizer=${{ matrix.sanitizer }}
Expand Down
19 changes: 4 additions & 15 deletions rust/prism-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ fn main() {
println!("cargo:rustc-link-lib=static=rubyparser");

// Add `[root]/build/` to the search paths, so it can find `librubyparser.a`.
println!(
"cargo:rustc-link-search=native={}",
ruby_build_path.to_str().unwrap()
);
println!("cargo:rustc-link-search=native={}", ruby_build_path.to_str().unwrap());

// This is where the magic happens.
let bindings = generate_bindings(&ruby_include_path);
Expand All @@ -23,20 +20,14 @@ fn main() {
/// Gets the path to project files (`librubyparser*`) at `[root]/build/`.
///
fn ruby_build_path() -> PathBuf {
cargo_manifest_path()
.join("../../build/")
.canonicalize()
.unwrap()
cargo_manifest_path().join("../../build/").canonicalize().unwrap()
}

/// Gets the path to the header files that `bindgen` needs for doing code
/// generation.
///
fn ruby_include_path() -> PathBuf {
cargo_manifest_path()
.join("../../include/")
.canonicalize()
.unwrap()
cargo_manifest_path().join("../../include/").canonicalize().unwrap()
}

fn cargo_manifest_path() -> PathBuf {
Expand Down Expand Up @@ -112,7 +103,5 @@ fn generate_bindings(ruby_include_path: &Path) -> bindgen::Bindings {
fn write_bindings(bindings: &bindgen::Bindings) {
let out_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());

bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
bindings.write_to_file(out_path.join("bindings.rs")).expect("Couldn't write bindings!");
}
2 changes: 2 additions & 0 deletions rust/prism-sys/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 300
match_block_trailing_comma = true
11 changes: 3 additions & 8 deletions rust/prism-sys/tests/node_tests.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
use std::{ffi::CString, mem::MaybeUninit};

use prism_sys::{pm_parser_t, pm_parser_init, pm_parse, pm_parser_free};
use prism_sys::{pm_node_type, pm_node_destroy};
use prism_sys::{pm_node_destroy, pm_node_type};
use prism_sys::{pm_parse, pm_parser_free, pm_parser_init, pm_parser_t};

#[test]
fn node_test() {
let mut parser = MaybeUninit::<pm_parser_t>::uninit();
let code = CString::new("class Foo; end").unwrap();

unsafe {
pm_parser_init(
parser.as_mut_ptr(),
code.as_ptr().cast::<u8>(),
code.as_bytes().len(),
std::ptr::null(),
);
pm_parser_init(parser.as_mut_ptr(), code.as_ptr().cast::<u8>(), code.as_bytes().len(), std::ptr::null());

let parser = parser.assume_init_mut();
let parsed_node = pm_parse(parser);
Expand Down
5 changes: 1 addition & 4 deletions rust/prism-sys/tests/pack_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{ffi::CString, mem::MaybeUninit};

use prism_sys::{
pm_pack_encoding, pm_pack_endian, pm_pack_length_type, pm_pack_parse, pm_pack_result,
pm_pack_signed, pm_pack_size, pm_pack_type, pm_pack_variant, pm_size_to_native,
};
use prism_sys::{pm_pack_encoding, pm_pack_endian, pm_pack_length_type, pm_pack_parse, pm_pack_result, pm_pack_signed, pm_pack_size, pm_pack_type, pm_pack_variant, pm_size_to_native};

#[test]
fn pack_parse_test() {
Expand Down
25 changes: 4 additions & 21 deletions rust/prism-sys/tests/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use std::{
path::Path,
};

use prism_sys::{
pm_comment_t, pm_comment_type_t, pm_diagnostic_t,
pm_node_destroy, pm_parse, pm_parser_free, pm_parser_init,
pm_parser_t,
};
use prism_sys::{pm_comment_t, pm_comment_type_t, pm_diagnostic_t, pm_node_destroy, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t};

fn ruby_file_contents() -> (CString, usize) {
let rust_path = Path::new(env!("CARGO_MANIFEST_DIR"));
Expand Down Expand Up @@ -39,12 +35,7 @@ fn comments_test() {
let mut parser = MaybeUninit::<pm_parser_t>::uninit();

unsafe {
pm_parser_init(
parser.as_mut_ptr(),
source.as_ptr().cast::<u8>(),
source.as_bytes().len(),
std::ptr::null(),
);
pm_parser_init(parser.as_mut_ptr(), source.as_ptr().cast::<u8>(), source.as_bytes().len(), std::ptr::null());
let parser = parser.assume_init_mut();
let node = pm_parse(parser);

Expand All @@ -70,12 +61,7 @@ fn diagnostics_test() {
let mut parser = MaybeUninit::<pm_parser_t>::uninit();

unsafe {
pm_parser_init(
parser.as_mut_ptr(),
source.as_ptr().cast::<u8>(),
source.as_bytes().len(),
std::ptr::null(),
);
pm_parser_init(parser.as_mut_ptr(), source.as_ptr().cast::<u8>(), source.as_bytes().len(), std::ptr::null());
let parser = parser.assume_init_mut();
let node = pm_parse(parser);

Expand All @@ -84,10 +70,7 @@ fn diagnostics_test() {

let error = error_list.head as *const pm_diagnostic_t;
let message = CStr::from_ptr((*error).message);
assert_eq!(
message.to_string_lossy(),
"Cannot parse the expression"
);
assert_eq!(message.to_string_lossy(), "Cannot parse the expression");

let location = {
let start = (*error).start.offset_from(parser.start);
Expand Down
11 changes: 2 additions & 9 deletions rust/prism-sys/tests/utils_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ fn list_test() {
}

mod string {
use prism_sys::{
pm_string_free, pm_string_length, pm_string_source, pm_string_t,
pm_string_t__bindgen_ty_1, PM_STRING_SHARED, PM_STRING_OWNED,
PM_STRING_CONSTANT, PM_STRING_MAPPED
};
use prism_sys::{pm_string_free, pm_string_length, pm_string_source, pm_string_t, pm_string_t__bindgen_ty_1, PM_STRING_CONSTANT, PM_STRING_MAPPED, PM_STRING_OWNED, PM_STRING_SHARED};

use super::*;

Expand All @@ -59,10 +55,7 @@ mod string {
length: c_string.as_bytes().len(),
};

S {
c_string,
pm_string,
}
S { c_string, pm_string }
}

#[test]
Expand Down
52 changes: 31 additions & 21 deletions rust/prism/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![allow(
clippy::too_many_lines,
clippy::uninlined_format_args
)]
#![allow(clippy::too_many_lines, clippy::uninlined_format_args)]

use serde::Deserialize;
use std::fs::File;
Expand Down Expand Up @@ -41,7 +38,7 @@ enum NodeFieldType {
UInt32,

#[serde(rename = "flags")]
Flags
Flags,
}

#[derive(Debug, Deserialize)]
Expand All @@ -51,13 +48,13 @@ struct NodeField {
#[serde(rename = "type")]
field_type: NodeFieldType,

kind: Option<String>
kind: Option<String>,
}

#[derive(Debug, Deserialize)]
struct FlagValue {
name: String,
comment: String
comment: String,
}

#[derive(Debug, Deserialize)]
Expand All @@ -75,7 +72,7 @@ struct Node {
#[serde(default)]
fields: Vec<NodeField>,

comment: String
comment: String,
}

#[derive(Debug, Deserialize)]
Expand All @@ -100,10 +97,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
/// Gets the path to the config.yml file at `[root]/config.yml`.
///
fn config_path() -> PathBuf {
cargo_manifest_path()
.join("../../config.yml")
.canonicalize()
.unwrap()
cargo_manifest_path().join("../../config.yml").canonicalize().unwrap()
}

/// Gets the path to the root directory at `[root]`.
Expand Down Expand Up @@ -346,7 +340,7 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box<d
writeln!(file, " }}")?;
}
}
}
},
}
}

Expand All @@ -362,7 +356,9 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box<d
} else {
let mut padding = false;
for _ in &node.fields {
if padding { write!(file, ", ")?; }
if padding {
write!(file, ", ")?;
}
write!(file, "{{:?}}")?;
padding = true;
}
Expand All @@ -371,7 +367,9 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box<d
padding = false;

for field in &node.fields {
if padding { write!(file, ", ")?; }
if padding {
write!(file, ", ")?;
}
write!(file, "self.{}()", field.name)?;
padding = true;
}
Expand All @@ -393,7 +391,13 @@ fn write_visit(file: &mut File, config: &Config) -> Result<(), Box<dyn std::erro
writeln!(file, " match node {{")?;

for node in &config.nodes {
writeln!(file, " Node::{} {{ parser, pointer, marker }} => self.visit{}(&{} {{ parser: *parser, pointer: *pointer, marker: *marker }}),", node.name, struct_name(&node.name), node.name)?;
writeln!(
file,
" Node::{} {{ parser, pointer, marker }} => self.visit{}(&{} {{ parser: *parser, pointer: *pointer, marker: *marker }}),",
node.name,
struct_name(&node.name),
node.name
)?;
}

writeln!(file, " }}")?;
Expand Down Expand Up @@ -445,7 +449,7 @@ fn write_visit(file: &mut File, config: &Config) -> Result<(), Box<dyn std::erro
writeln!(file, " visitor.visit(&node);")?;
writeln!(file, " }}")?;
},
_ => {}
_ => {},
}
}

Expand All @@ -467,7 +471,9 @@ fn write_bindings(config: &Config) -> Result<(), Box<dyn std::error::Error>> {
let out_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("bindings.rs");
let mut file = std::fs::File::create(&out_path).expect("Unable to create file");

writeln!(file, r#"
writeln!(
file,
r#"
use std::marker::PhantomData;
use std::ptr::NonNull;
Expand Down Expand Up @@ -679,7 +685,8 @@ impl std::fmt::Debug for ConstantList<'_> {{
write!(f, "{{:?}}", self.iter().collect::<Vec<_>>())
}}
}}
"#)?;
"#
)?;

for node in &config.nodes {
writeln!(file, "const {}: u16 = pm_node_type::{} as u16;", type_name(&node.name), type_name(&node.name))?;
Expand Down Expand Up @@ -713,7 +720,9 @@ impl std::fmt::Debug for ConstantList<'_> {{
writeln!(file, "}}")?;
writeln!(file)?;

writeln!(file, r#"
writeln!(
file,
r#"
impl<'pr> Node<'pr> {{
/// Creates a new node from the given pointer.
///
Expand All @@ -724,7 +733,8 @@ impl<'pr> Node<'pr> {{
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub(crate) fn new(parser: NonNull<pm_parser_t>, node: *mut pm_node_t) -> Self {{
match unsafe {{ (*node).type_ }} {{
"#)?;
"#
)?;

for node in &config.nodes {
writeln!(file, " {} => Self::{} {{ parser, pointer: node.cast::<pm{}_t>(), marker: PhantomData }},", type_name(&node.name), node.name, struct_name(&node.name))?;
Expand Down
2 changes: 2 additions & 0 deletions rust/prism/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 120
match_block_trailing_comma = true
Loading

0 comments on commit 08ef0ce

Please sign in to comment.