Function genco::lang::rust::import

source ·
pub fn import<M, N>(module: M, name: N) -> Import
where M: Into<ItemStr>, N: Into<ItemStr>,
Expand description

The import of a Rust type use std::collections::HashMap.

§Examples

use genco::prelude::*;

let a = rust::import("std::fmt", "Debug").qualified();
let b = rust::import("std::fmt", "Debug").with_module_alias("fmt2");
let c = rust::import("std::fmt", "Debug");
let d = rust::import("std::fmt", "Debug").with_alias("FmtDebug");

let toks = quote!{
    $a
    $b
    $c
    $d
};

assert_eq!(
    vec![
        "use std::fmt::{self, self as fmt2, Debug, Debug as FmtDebug};",
        "",
        "fmt::Debug",
        "fmt2::Debug",
        "Debug",
        "FmtDebug",
    ],
    toks.to_file_vec()?
);

§Example with an alias

use genco::prelude::*;

let ty = rust::import("std::fmt", "Debug").with_alias("FmtDebug");

let toks = quote!{
    $ty
};

assert_eq!(
    vec![
        "use std::fmt::Debug as FmtDebug;",
        "",
        "FmtDebug",
    ],
    toks.to_file_vec()?
);

§Example with a module alias

use genco::prelude::*;

let ty = rust::import("std::fmt", "Debug").with_module_alias("fmt2");

let toks = quote!{
    $ty
};

assert_eq!(
    vec![
        "use std::fmt as fmt2;",
        "",
        "fmt2::Debug",
    ],
    toks.to_file_vec()?
);

§Example with multiple aliases

use genco::prelude::*;

let a = rust::import("std::fmt", "Debug").with_alias("FmtDebug");
let b = rust::import("std::fmt", "Debug").with_alias("FmtDebug2");

let toks = quote!{
    $a
    $b
};

assert_eq!(
    vec![
        "use std::fmt::{Debug as FmtDebug, Debug as FmtDebug2};",
        "",
        "FmtDebug",
        "FmtDebug2",
    ],
    toks.to_file_vec()?
);