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 26, 2023
1 parent ac69376 commit 5969b82
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 99 deletions.
7 changes: 5 additions & 2 deletions .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 All @@ -67,7 +67,7 @@ jobs:
uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.71.1"
components: clippy
components: clippy, rustfmt
- uses: actions/cache@v3
with:
path: |
Expand All @@ -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
23 changes: 7 additions & 16 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 @@ -74,11 +65,11 @@ fn generate_bindings(ruby_include_path: &Path) -> bindgen::Bindings {
.allowlist_type("pm_pack_size")
.allowlist_type("pm_parser_t")
.allowlist_type("pm_string_t")
.allowlist_type(r#"^pm_\w+_node_t"#)
.allowlist_type(r#"^pm_\w+_flags"#)
.allowlist_type(r"^pm_\w+_node_t")
.allowlist_type(r"^pm_\w+_flags")
// Enums
.rustified_non_exhaustive_enum("pm_comment_type_t")
.rustified_non_exhaustive_enum(r#"pm_\w+_flags"#)
.rustified_non_exhaustive_enum(r"pm_\w+_flags")
.rustified_non_exhaustive_enum("pm_node_type")
.rustified_non_exhaustive_enum("pm_pack_encoding")
.rustified_non_exhaustive_enum("pm_pack_endian")
Expand All @@ -102,7 +93,7 @@ fn generate_bindings(ruby_include_path: &Path) -> bindgen::Bindings {
.allowlist_function("pm_string_source")
.allowlist_function("pm_version")
// Vars
.allowlist_var(r#"^pm_encoding\S+"#)
.allowlist_var(r"^pm_encoding\S+")
.generate()
.expect("Unable to generate prism bindings")
}
Expand Down
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 = 120
match_block_trailing_comma = true
4 changes: 2 additions & 2 deletions rust/prism-sys/tests/node_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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() {
Expand Down
4 changes: 2 additions & 2 deletions rust/prism-sys/tests/pack_tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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,
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]
Expand Down
8 changes: 2 additions & 6 deletions rust/prism-sys/tests/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use std::{
};

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_comment_t, pm_comment_type_t, pm_diagnostic_t, pm_node_destroy, pm_parse, pm_parser_free, pm_parser_init,
pm_parser_t,
};

Expand Down Expand Up @@ -84,10 +83,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
10 changes: 3 additions & 7 deletions rust/prism-sys/tests/utils_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ 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
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 +58,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 = 300
match_block_trailing_comma = true
Loading

0 comments on commit 5969b82

Please sign in to comment.