use-builder

Crates.iouse-builder
lib.rsuse-builder
version0.1.0
sourcesrc
created_at2022-04-17 19:54:19.071436
updated_at2022-04-17 19:54:19.071436
descriptionA crate to build source code use sections by combining multiple (possibly duplicate) use section inputs
homepage
repositoryhttps://github.com/nu11ptr/flexgen/tree/master/use_builder
max_upload_size
id569563
size20,463
Scott Meeuwsen (nu11ptr)

documentation

https://docs.rs/use-builder

README

use-builder

Crate Docs

A crate to build source code use sections by combining multiple (possibly duplicate) use section inputs.

NOTE: This is a fairly specialized crate. The only likely use case is really that of compiling source code snippets into files, like flexgen does.

Usage

[dependencies]
use-builder = "0.1"

Example

use assert_unordered::assert_eq_unordered;
use quote::quote;
use use_builder::{UseBuilder, UseItems};

fn main() {
    // #1 - Build a two or more use trees and convert into `UseItems` (wrapped `Vec<ItemUse>`)

    let use1 = quote! {
        use crate::Test;
        use std::error::{Error as StdError};
        use std::fmt::Debug;
    };

    let use2 = quote! {
        use syn::ItemUse;
        use std::fmt::Display;
        use crate::*;
    };

    let items1: UseItems = syn::parse2(use1).unwrap();
    let items2: UseItems = syn::parse2(use2).unwrap();

    // #2 - Parse, process, and extract into sections

    let builder = UseBuilder::from_uses(vec![items1, items2]);
    let (std_use, ext_use, crate_use) = builder.into_items_sections().unwrap();

    // #3 - Validate our response matches expectation

    let std_expected = quote! {
        use std::error::Error as StdError;
        use std::fmt::{Debug, Display};
    };
    let std_expected = syn::parse2::<UseItems>(std_expected).unwrap().into_inner();

    let ext_expected = quote! {
        use syn::ItemUse;
    };
    let ext_expected = syn::parse2::<UseItems>(ext_expected).unwrap().into_inner();

    let crate_expected = quote! {
        use crate::*;
    };
    let crate_expected = syn::parse2::<UseItems>(crate_expected).unwrap().into_inner();

    assert_eq_unordered!(std_expected, std_use);
    assert_eq_unordered!(ext_expected, ext_use);
    assert_eq_unordered!(crate_expected, crate_use);
}

License

This project is licensed optionally under either:

Commit count: 66

cargo fmt