Skip to content

Commit

Permalink
rust-rewrite: Removed "split-into-modules" option for rs-structs output.
Browse files Browse the repository at this point in the history
  • Loading branch information
schilkp committed May 7, 2024
1 parent 3f84005 commit 56de2ed
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 170 deletions.
4 changes: 2 additions & 2 deletions reginald_codegen/src/builtin/rs/structs/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ pub(super) fn generate_enum(out: &mut dyn Write, inp: &Input, e: &Enum) -> Resul
}

/// Generate conversion impls.
pub(super) fn generate_enum_impls(out: &mut dyn Write, inp: &Input, e: &Enum, in_module: bool) -> Result<(), Error> {
pub(super) fn generate_enum_impls(out: &mut dyn Write, inp: &Input, e: &Enum) -> Result<(), Error> {
// Smallest uint type that can be used to represent the enum's content:
let enum_name = rs_pascalcase(&e.name);
let width_bytes = e.min_width_bytes();
let trait_prefix = trait_prefix(inp, in_module);
let trait_prefix = trait_prefix(inp);

match enum_impl(e) {
FromBytesImpl::FromBytes => {
Expand Down
60 changes: 22 additions & 38 deletions reginald_codegen/src/builtin/rs/structs/layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use reginald_utils::remove_wrapping_parens;
use super::{rs_pascalcase, rs_snakecase};

// Generate content for a layout struct
pub fn generate_layout(out: &mut dyn Write, inp: &Input, layout: &Layout, in_module: bool) -> Result<(), Error> {
pub fn generate_layout(out: &mut dyn Write, inp: &Input, layout: &Layout) -> Result<(), Error> {
let mut out = HeaderWriter::new(out);

// Layout struct:
generate_layout_struct(&mut out, inp, layout, None, in_module)?;
generate_layout_struct(&mut out, inp, layout, None)?;

out.push_section_with_header(&["\n", "// Layout-specific enums and sub-layouts:", "\n"]);

Expand All @@ -31,20 +31,20 @@ pub fn generate_layout(out: &mut dyn Write, inp: &Input, layout: &Layout, in_mod
}

for local_layout in layout.nested_local_layouts() {
generate_layout_struct(&mut out, inp, local_layout, None, in_module)?;
generate_layout_struct(&mut out, inp, local_layout, None)?;
}

out.pop_section();
out.push_section_with_header(&["\n", "// Conversion functions:", "\n"]);

generate_layout_impls(&mut out, inp, layout, in_module)?;
generate_layout_impls(&mut out, inp, layout)?;

for e in layout.nested_local_enums() {
enums::generate_enum_impls(&mut out, inp, e, in_module)?;
enums::generate_enum_impls(&mut out, inp, e)?;
}

for layout in layout.nested_local_layouts() {
generate_layout_impls(&mut out, inp, layout, in_module)?;
generate_layout_impls(&mut out, inp, layout)?;
}

out.pop_section();
Expand All @@ -59,7 +59,6 @@ pub fn generate_layout_struct(
inp: &Input,
layout: &Layout,
for_register: Option<&Register>,
in_module: bool,
) -> Result<(), Error> {
// Struct doc comment:
writeln!(out)?;
Expand Down Expand Up @@ -93,7 +92,7 @@ pub fn generate_layout_struct(
writeln!(out, "pub struct {} {{", rs_pascalcase(&layout.name))?;

for field in layout.fields_with_content() {
let field_type = register_layout_member_type(inp, field, in_module)?;
let field_type = register_layout_member_type(field)?;
let field_name = rs_snakecase(&field.name);
generate_doc_comment(out, &field.docs, " ")?;
writeln!(out, " pub {field_name}: {field_type},")?;
Expand All @@ -105,34 +104,29 @@ pub fn generate_layout_struct(
}

/// Type of a field inside a register struct.
pub fn register_layout_member_type(inp: &Input, field: &LayoutField, in_module: bool) -> Result<String, Error> {
pub fn register_layout_member_type(field: &LayoutField) -> Result<String, Error> {
match &field.accepts {
FieldType::UInt => rs_fitting_unsigned_type(mask_width(field.mask)),
FieldType::Bool => Ok("bool".to_string()),
FieldType::Enum(e) => Ok(prefix_with_super(inp, &rs_pascalcase(&e.name), e.is_local, in_module)),
FieldType::Layout(l) => Ok(prefix_with_super(inp, &rs_pascalcase(&l.name), l.is_local, in_module)),
FieldType::Enum(e) => Ok(rs_pascalcase(&e.name)),
FieldType::Layout(l) => Ok(rs_pascalcase(&l.name)),
FieldType::Fixed(_) => panic!("Fixed layout field has no type"),
}
}

pub fn generate_layout_impls(out: &mut dyn Write, inp: &Input, layout: &Layout, in_module: bool) -> Result<(), Error> {
generate_layout_impl_to_bytes(inp, out, layout, in_module)?;
generate_layout_impl_from_bytes(inp, out, layout, in_module)?;
pub fn generate_layout_impls(out: &mut dyn Write, inp: &Input, layout: &Layout) -> Result<(), Error> {
generate_layout_impl_to_bytes(inp, out, layout)?;
generate_layout_impl_from_bytes(inp, out, layout)?;
if inp.opts.generate_uint_conversion {
generate_layout_impl_uint_conv(inp, out, layout, in_module)?;
generate_layout_impl_uint_conv(inp, out, layout)?;
}
Ok(())
}

pub fn generate_layout_impl_to_bytes(
inp: &Input,
out: &mut dyn Write,
layout: &Layout,
in_module: bool,
) -> Result<(), Error> {
pub fn generate_layout_impl_to_bytes(inp: &Input, out: &mut dyn Write, layout: &Layout) -> Result<(), Error> {
let struct_name = rs_pascalcase(&layout.name);
let width_bytes = layout.width_bytes();
let trait_prefix = trait_prefix(inp, in_module);
let trait_prefix = trait_prefix(inp);

let mut out = IndentWriter::new(out, " ");

Expand Down Expand Up @@ -285,15 +279,10 @@ pub fn generate_layout_impl_to_bytes(
Ok(())
}

fn generate_layout_impl_from_bytes(
inp: &Input,
out: &mut dyn Write,
layout: &Layout,
in_module: bool,
) -> Result<(), Error> {
fn generate_layout_impl_from_bytes(inp: &Input, out: &mut dyn Write, layout: &Layout) -> Result<(), Error> {
let struct_name = rs_pascalcase(&layout.name);
let width_bytes = layout.width_bytes();
let trait_prefix = trait_prefix(inp, in_module);
let trait_prefix = trait_prefix(inp);

let mut out = IndentWriter::new(out, " ");

Expand Down Expand Up @@ -411,7 +400,7 @@ fn generate_layout_impl_from_bytes(
writeln!(out, " {field_name}: {numeric_value} != 0,")?;
}
FieldType::Enum(e) => {
let enum_name = prefix_with_super(inp, &rs_pascalcase(&e.name), e.is_local, in_module);
let enum_name = rs_pascalcase(&e.name);
let array_name = rs_snakecase(&field.name);

match enum_impl(e) {
Expand All @@ -431,7 +420,7 @@ fn generate_layout_impl_from_bytes(
}
}
FieldType::Layout(l) => {
let layout_name = prefix_with_super(inp, &rs_pascalcase(&l.name), l.is_local, in_module);
let layout_name = rs_pascalcase(&l.name);
let array_name = rs_snakecase(&field.name);
if l.can_always_unpack() {
writeln!(out, " {field_name}: {layout_name}::from_le_bytes(&{array_name}),")?;
Expand All @@ -458,14 +447,9 @@ fn generate_layout_impl_from_bytes(
Ok(())
}

fn generate_layout_impl_uint_conv(
inp: &Input,
out: &mut dyn Write,
layout: &Layout,
in_module: bool,
) -> Result<(), Error> {
fn generate_layout_impl_uint_conv(inp: &Input, out: &mut dyn Write, layout: &Layout) -> Result<(), Error> {
let struct_name = rs_pascalcase(&layout.name);
let trait_prefix = trait_prefix(inp, in_module);
let trait_prefix = trait_prefix(inp);

let (uint_type, uint_width_bytes) = match layout.width_bytes() {
1 => ("u8", 1),
Expand Down
24 changes: 3 additions & 21 deletions reginald_codegen/src/builtin/rs/structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ pub struct GeneratorOpts {
#[cfg_attr(feature = "cli", arg(verbatim_doc_comment))]
pub address_type: Option<String>,

/// Split registers and register blocks into seperate modules.
#[cfg_attr(feature = "cli", arg(long))]
#[cfg_attr(feature = "cli", arg(action = clap::ArgAction::Set))]
#[cfg_attr(feature = "cli", arg(default_value = "true"))]
#[cfg_attr(feature = "cli", arg(verbatim_doc_comment))]
pub split_into_modules: bool,

/// Trait to derive on all register structs.
///
/// May be given multiple times.
Expand Down Expand Up @@ -138,7 +131,7 @@ pub fn generate(out: &mut dyn Write, map: &RegisterMap, opts: &GeneratorOpts) ->
out.push_section_with_header(&["\n", &rs_header_comment("Shared Enums"), "\n"]);
for shared_enum in inp.map.shared_enums() {
enums::generate_enum(&mut out, &inp, shared_enum)?;
enums::generate_enum_impls(&mut out, &inp, shared_enum, false)?;
enums::generate_enum_impls(&mut out, &inp, shared_enum)?;
}
out.pop_section();

Expand All @@ -147,7 +140,7 @@ pub fn generate(out: &mut dyn Write, map: &RegisterMap, opts: &GeneratorOpts) ->
for layout in inp.map.shared_layouts() {
writeln!(&mut out)?;
writeln!(&mut out, "{}", &rs_header_comment(&format!("`{}` Shared Layout", layout.name)))?;
layouts::generate_layout(&mut out, &inp, layout, false)?;
layouts::generate_layout(&mut out, &inp, layout)?;
}

// ===== Individual Registers: =====
Expand Down Expand Up @@ -235,25 +228,14 @@ fn generate_traits(out: &mut dyn Write) -> Result<(), Error> {
/// Decide trait prefix. If an external override is given, use that.
/// Otherwise, use the local definition (Which may be in the parent
/// module)
fn trait_prefix(inp: &Input, in_module: bool) -> String {
fn trait_prefix(inp: &Input) -> String {
if let Some(overide) = &inp.opts.external_traits {
String::from(overide)
} else if in_module {
String::from("super::")
} else {
String::new()
}
}

/// Prefix a given identifier with `super::` if required.
fn prefix_with_super(inp: &Input, s: &str, is_local: bool, in_module: bool) -> String {
if inp.opts.split_into_modules && !is_local && in_module {
format!("super::{s}")
} else {
String::from(s)
}
}

#[allow(clippy::enum_variant_names)]
enum FromBytesImpl {
FromBytes,
Expand Down
Loading

0 comments on commit 56de2ed

Please sign in to comment.