Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions pdl-compiler/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,7 @@ impl Diagnostics {
}

fn err_or<T>(self, value: T) -> Result<T, Diagnostics> {
if self.is_empty() {
Ok(value)
} else {
Err(self)
}
if self.is_empty() { Ok(value) } else { Err(self) }
}

pub fn emit(
Expand Down Expand Up @@ -268,11 +264,7 @@ impl<'d> Scope<'d> {
}

// Return failure if any diagnostic is raised.
if diagnostics.is_empty() {
Ok(scope)
} else {
Err(diagnostics)
}
if diagnostics.is_empty() { Ok(scope) } else { Err(diagnostics) }
}

/// Iterate over the child declarations of the selected declaration.
Expand Down Expand Up @@ -447,11 +439,7 @@ impl Schema {
}
_ => false,
});
if has_payload_size {
Size::Dynamic
} else {
Size::Unknown
}
if has_payload_size { Size::Dynamic } else { Size::Unknown }
}
FieldDesc::Typedef { type_id, .. }
| FieldDesc::FixedEnum { enum_id: type_id, .. }
Expand All @@ -478,11 +466,7 @@ impl Schema {
}
_ => false,
});
if has_array_size {
Size::Dynamic
} else {
Size::Unknown
}
if has_array_size { Size::Dynamic } else { Size::Unknown }
}
FieldDesc::Array { .. } => unreachable!(),
};
Expand Down Expand Up @@ -576,11 +560,7 @@ fn bit_width(value: usize) -> usize {

/// Return the maximum value for a scalar value.
fn scalar_max(width: usize) -> usize {
if width >= usize::BITS as usize {
usize::MAX
} else {
(1 << width) - 1
}
if width >= usize::BITS as usize { usize::MAX } else { (1 << width) - 1 }
}

/// Check declaration identifiers.
Expand Down Expand Up @@ -1650,7 +1630,7 @@ fn check_optional_fields(file: &File) -> Result<(), Diagnostics> {
)),
])
.with_notes(vec![
"note: expected scalar field identifier".to_owned()
"note: expected scalar field identifier".to_owned(),
]),
),
Some(Field { loc, .. }) => diagnostics.push(
Expand Down Expand Up @@ -3410,8 +3390,8 @@ mod test {
);
}

use analyzer::Size;
use Size::*;
use analyzer::Size;

#[derive(Debug, PartialEq, Eq)]
struct Annotations {
Expand Down
5 changes: 4 additions & 1 deletion pdl-compiler/src/backends/common/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ impl<S: Symbol> ByteAligner<S> {
self.staged_fields.push(Field { offset: self.staged_width, symbol, width });
self.staged_width += width;
if self.staged_width > Self::MAX_CHUNK_WIDTH {
panic!("total field width grew beyond max chunk width of {} before aligning to a byte boundary", Self::MAX_CHUNK_WIDTH)
panic!(
"total field width grew beyond max chunk width of {} before aligning to a byte boundary",
Self::MAX_CHUNK_WIDTH
)
}
self.try_commit_staged_chunk();
}
Expand Down
4 changes: 2 additions & 2 deletions pdl-compiler/src/backends/java/codegen/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

use std::iter;

use genco::{lang::Java, quote, tokens::quoted, Tokens};
use genco::{Tokens, lang::Java, quote, tokens::quoted};
use heck::ToUpperCamelCase;

use crate::{
ast::{Tag, TagOther, TagRange, TagValue},
backends::java::{
codegen::expr::{cast_symbol, literal, ExprTree},
Integral,
codegen::expr::{ExprTree, cast_symbol, literal},
},
};

Expand Down
14 changes: 3 additions & 11 deletions pdl-compiler/src/backends/java/codegen/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::{cell::RefCell, cmp::max};

use genco::{lang::Java, quote, quote_in, tokens::FormatInto, Tokens};
use genco::{Tokens, lang::Java, quote, quote_in, tokens::FormatInto};

use crate::backends::java::Integral;

Expand Down Expand Up @@ -140,11 +140,7 @@ impl ExprTree {
}

pub fn sub(&self, lhs: ExprId, rhs: ExprId) -> ExprId {
if self.is_literal(rhs, 0) {
lhs
} else {
self.op(lhs, BinOp::Sub, rhs)
}
if self.is_literal(rhs, 0) { lhs } else { self.op(lhs, BinOp::Sub, rhs) }
}

pub fn mul(&self, lhs: ExprId, rhs: ExprId) -> ExprId {
Expand All @@ -158,11 +154,7 @@ impl ExprTree {
}

pub fn div(&self, lhs: ExprId, rhs: ExprId) -> ExprId {
if self.is_literal(rhs, 1) {
lhs
} else {
self.op(lhs, BinOp::Divide, rhs)
}
if self.is_literal(rhs, 1) { lhs } else { self.op(lhs, BinOp::Divide, rhs) }
}

pub fn lshift(&self, lhs: ExprId, rhs: ExprId) -> ExprId {
Expand Down
15 changes: 8 additions & 7 deletions pdl-compiler/src/backends/java/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

use core::panic;
use genco::{
self,
prelude::{java, Java},
self, Tokens,
prelude::{Java, java},
quote, quote_in,
tokens::FormatInto,
Tokens,
};
use std::collections::HashMap;

Expand All @@ -41,12 +40,12 @@ pub mod expr;
mod packet;

use crate::backends::java::{
codegen::expr::{cast_symbol, ExprId, ExprTree},
inheritance::{ClassHeirarchy, Constraint},
Context, Field, WidthField,
codegen::expr::{ExprId, ExprTree, cast_symbol},
inheritance::{ClassHeirarchy, Constraint},
};

use super::{import, Chunk, Class, EndiannessValue, Integral, JavaFile, PacketDef};
use super::{Chunk, Class, EndiannessValue, Integral, JavaFile, PacketDef, import};

impl JavaFile<&Context> for Class {
fn generate(self, context: &Context) -> Tokens<Java> {
Expand Down Expand Up @@ -274,7 +273,9 @@ impl Field {
Integral::fitting(width),
),
_ => {
panic!("Bitfields ending in 'size' or 'count' are not supported. Use _size_ or _count_ instead.")
panic!(
"Bitfields ending in 'size' or 'count' are not supported. Use _size_ or _count_ instead."
)
}
}
} else if let Field::Integral { width: 1, .. } = self {
Expand Down
6 changes: 3 additions & 3 deletions pdl-compiler/src/backends/java/codegen/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@

use std::collections::{BTreeMap, HashMap};

use genco::{self, prelude::Java, quote, tokens::quoted, Tokens};
use genco::{self, Tokens, prelude::Java, quote, tokens::quoted};
use heck::{self, ToLowerCamelCase, ToUpperCamelCase};

use crate::{
ast::EndiannessValue,
backends::{
common::alignment::Alignment,
java::{
Context, Field, WidthField,
codegen::expr::ExprId,
inheritance::{ClassHeirarchy, Constraint, InheritanceNode},
Context, Field, WidthField,
},
},
};

use super::{expr::ExprTree, import, Chunk, Integral, PacketDef};
use super::{Chunk, Integral, PacketDef, expr::ExprTree, import};

pub fn gen_packet(name: &String, def: &PacketDef, ctx: &Context) -> Tokens<Java> {
let endianness = ctx.endianness;
Expand Down
6 changes: 1 addition & 5 deletions pdl-compiler/src/backends/java/inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ pub struct InheritanceNode {

impl InheritanceNode {
pub fn field_width(&self) -> Option<usize> {
if self.dyn_fields.is_empty() {
Some(self.static_field_width)
} else {
None
}
if self.dyn_fields.is_empty() { Some(self.static_field_width) } else { None }
}
}

Expand Down
5 changes: 2 additions & 3 deletions pdl-compiler/src/backends/java/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
// limitations under the License.

use genco::{
self,
prelude::{java, Java},
self, Tokens,
prelude::{Java, java},
tokens::FormatInto,
Tokens,
};
use heck::{self, ToLowerCamelCase, ToUpperCamelCase};
use std::{
Expand Down
4 changes: 2 additions & 2 deletions pdl-compiler/src/backends/java/preamble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use genco::{lang::Java, quote, Tokens};
use genco::{Tokens, lang::Java, quote};

use crate::{
ast::EndiannessValue,
backends::java::{codegen::expr::ExprTree, import, Integral, JavaFile},
backends::java::{Integral, JavaFile, codegen::expr::ExprTree, import},
};

pub struct Utils;
Expand Down
6 changes: 3 additions & 3 deletions pdl-compiler/src/backends/java/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.

use genco::{
Tokens,
lang::Java,
prelude::{java, quote_fn},
quote,
tokens::{quoted, FormatInto},
Tokens,
tokens::{FormatInto, quoted},
};
use heck::ToUpperCamelCase;
use serde_json::{Map, Value};
Expand All @@ -29,7 +29,7 @@ use std::{
str,
};

use super::{import, Class, Integral, JavaFile};
use super::{Class, Integral, JavaFile, import};
use crate::{
ast::{self, Decl, DeclDesc, Field, FieldDesc},
backends::{
Expand Down
2 changes: 1 addition & 1 deletion pdl-compiler/src/backends/rust/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::backends::rust::{mask_bits, types, ToIdent, ToUpperCamelCase};
use crate::backends::rust::{ToIdent, ToUpperCamelCase, mask_bits, types};
use crate::{analyzer, ast};
use quote::{format_ident, quote};

Expand Down
2 changes: 1 addition & 1 deletion pdl-compiler/src/backends/rust/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::backends::rust::{mask_bits, types, ToIdent, ToUpperCamelCase};
use crate::backends::rust::{ToIdent, ToUpperCamelCase, mask_bits, types};
use crate::{analyzer, ast};
use quote::{format_ident, quote};

Expand Down
14 changes: 3 additions & 11 deletions pdl-compiler/src/backends/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ fn generate_specialize_impl(
case.id.clone(),
) {
Some(id) if id != case.id => {
return Err(format!("{} and {} cannot be disambiguated", id, case.id))
return Err(format!("{} and {} cannot be disambiguated", id, case.id));
}
_ => (),
}
Expand Down Expand Up @@ -997,11 +997,7 @@ fn generate_enum_decl(id: &str, tags: &[ast::Tag], width: usize) -> proc_macro2:
ranges.first().unwrap().0 == 0
&& ranges.last().unwrap().1 == max
&& ranges.windows(2).all(|window| {
if let [left, right] = window {
left.1 == right.0 - 1
} else {
false
}
if let [left, right] = window { left.1 == right.0 - 1 } else { false }
})
}

Expand All @@ -1012,11 +1008,7 @@ fn generate_enum_decl(id: &str, tags: &[ast::Tag], width: usize) -> proc_macro2:

// Return the maximum value for the scalar type.
fn scalar_max(width: usize) -> usize {
if width >= usize::BITS as usize {
usize::MAX
} else {
(1 << width) - 1
}
if width >= usize::BITS as usize { usize::MAX } else { (1 << width) - 1 }
}

// Format an enum tag identifier to rust upper caml case.
Expand Down
6 changes: 1 addition & 5 deletions pdl-compiler/src/backends/rust/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ pub fn rust_type(field: &ast::Field) -> proc_macro2::TokenStream {
/// Suffix for `Buf::get_*` and `BufMut::put_*` methods when reading a
/// value with the given `width`.
fn endianness_suffix(endianness: ast::EndiannessValue, width: usize) -> &'static str {
if width > 8 && endianness == ast::EndiannessValue::LittleEndian {
"_le"
} else {
""
}
if width > 8 && endianness == ast::EndiannessValue::LittleEndian { "_le" } else { "" }
}

/// Parse an unsigned integer with the given `width`.
Expand Down
Loading
Loading