fltk-rs / fl2rust

A fluid (fltk ui designer) file to Rust transpiler
MIT License
52 stars 3 forks source link

Add 'ALLOWS' preamble to generated file #18

Closed blueglyph closed 1 year ago

blueglyph commented 2 years ago

Description

I would suggest to add an option or to directly add this preamble to the generated Rust code:

// Automatically generated by fl2rust
#![allow(unused_variables)]
#![allow(unused_mut)]
#![allow(unused_imports)]
#![allow(clippy::needless_update)]

This removes the necessity to create an intermediate file with that preamble that must include the generated file, which is awkward.

Temporary work-around

As a work-around, here is how I modified the build.rs example to make it work (removing the OUT_DIR which is not defined by the tools and should be modified for each project):

Note: the build.rs file must be in the same directory as Cargo.toml, NOT in the src directory!

use std::fs;
use std::io::{Read, Write};
use std::path::Path;

pub const ALLOWS: &str = "// Automatically generated by fl2rust
#![allow(unused_variables)]
#![allow(unused_mut)]
#![allow(unused_imports)]
#![allow(clippy::needless_update)]";

fn main() {
    println!("cargo:rerun-if-changed=src/myuifile.fl");
    let g = fl2rust::Generator::default();
    let src_filename = Path::new("src").join("myuifile.fl").into_os_string();
    let dst_filename = Path::new("src").join("myuifile.rs").into_os_string();
    g.in_out(&src_filename, &dst_filename).expect("Failed to generate rust from fl file!");

    let mut f = fs::File::open(&dst_filename).expect("Failed to open generated file");
    let mut buf = String::new();
    f.read_to_string(&mut buf).expect("Failed to read generated file");

    let mut f = fs::File::create(&dst_filename).expect("Failed to overwrite generated file");
    write!(f, "{}", ALLOWS).unwrap();
    write!(f, "{}", buf).unwrap();
}

Unfortunately it has to read the whole content and re-write the generated file, but it's fine to store that in a buffer as long as the generated file is not too large.

Version

fl2rust doesn't show a version, but it must correspond to the crate version 0.4.17

MoAlyousef commented 2 years ago

Hi

I've added a new option which allows to generate the Rust file with the directives preamble:

fn main() {
    println!("cargo:rerun-if-changed=src/myuifile.fl");
    println!("cargo:rerun-if-changed=src/myuifile.rs");
    let g = fl2rust::Generator::default();
    g.in_out_with_directives_preamble("src/myuifile.fl", "src/myuifile.rs").expect("Failed to generate rust from fl file!");
}

It won't be the default though since the convention with generated files is to include them from OUT_DIR, that's how bindgen does it for example.

You can try it using the main branch. If it's ok I'll publish a new release.

blueglyph commented 2 years ago

It works well, thanks a lot!

Curiously now the OUT_DIR is set when I test it, I have no idea why it wasn't working yesterday. Perhaps a quirk with the IntelliJ plugin, perhaps I did something wrong? It also detects when to run the script correctly, which it wasn't yesterday.