slava-sh / rust-bundler

Creates a single-source-file version of a Cargo package
https://crates.io/crates/bundler
MIT License
21 stars 8 forks source link

Update and support Rust 2018 edition #7

Open mariomka opened 3 years ago

mariomka commented 3 years ago

Hello!

Bundler can't work with Rust 2018 edition code because rustfmt doesn't understand new syntax. Related issue: #6

To solve this it's necessary to use new rustfmt-nightly, there are two options:

Then, I opted for adding a feature inner_rustfmt to select the desired option, it calls the CLI by default. (Based on https://github.com/hatoo/cargo-snippet/blob/master/src/writer.rs)

Also, I've updated the project to the 2018 edition and dependencies to the newest versions.

By last, I found a syntax that doesn't work properly. But I'm not sure how to fix it. Taking a modified version of main.rs file from "complicated" test input:

extern crate my_lib;

fn main() {
    my_lib::a::a();
    self::my_lib::b::b();
    self::my_lib::c::d::d();
}

It bundles as:

pub mod a {
    pub fn a() {
        println!("a::a()");
    }
}
pub mod b {
    use crate::a;
    pub fn b() {
        a::a();
    }
}
pub mod c {
    pub mod d {
        pub fn d() {
            println!("c::d::d()");
        }
    }
}
fn main() {
    a::a();
    self::my_lib::b::b();
    self::my_lib::c::d::d();
}

self::my_lib is not replaced by self::.

But it works properly if syntax self::{lib_name} isn't used:

extern crate my_lib;

use my_lib::{a, b, c};

fn main() {
    a::a();
    self::b::b();
    self::c::d::d();
}
Endle commented 3 years ago

Hi,

Edit: I imade a PR to fix the compile error https://github.com/mariomka/rust-bundler/pull/1

Original post:

I was trying to install your fork following this instruction

git clone https://github.com/mariomka/rust-bundler.git
cd rust-bundler
cargo install --path .

However, I failed to install it

lizhenbo@localhost rust-bundler$ cargo --version
cargo 1.49.0 (d00d64df9 2020-12-05)
lizhenbo@localhost rust-bundler$ cargo install --path .
  Installing bundler v0.1.1 (/home/lizhenbo/src/rust-bundler)
    Updating `git://mirrors.ustc.edu.cn/crates.io-index` index
   Compiling bundler v0.1.1 (/home/lizhenbo/src/rust-bundler)
error[E0432]: unresolved import `syn::export`
 --> src/lib.rs:7:10
  |
7 | use syn::export::ToTokens;
  |          ^^^^^^ could not find `export` in `syn`

error[E0599]: no method named `into_token_stream` found for struct `syn::File` in the current scope
  --> src/lib.rs:36:21
   |
36 |     let code = file.into_token_stream().to_string();
   |                     ^^^^^^^^^^^^^^^^^ method not found in `syn::File`
   | 
  ::: /home/lizhenbo/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/quote-1.0.9/src/to_tokens.rs:71:8
   |
71 |     fn into_token_stream(self) -> TokenStream
   |        -----------------
   |        |
   |        the method is available for `std::boxed::Box<syn::File>` here
   |        the method is available for `Rc<syn::File>` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
2  | use syn::__private::ToTokens;
   |

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0432, E0599.
For more information about an error, try `rustc --explain E0432`.
error: failed to compile `bundler v0.1.1 (/home/lizhenbo/src/rust-bundler)`, intermediate artifacts can be found at `/home/lizhenbo/src/rust-bundler/target`

Caused by:
  could not compile `bundler`

To learn more, run the command again with --verbose