1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use pyo3::{FromPyObject};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use log::debug;

use crate::codegen::{CodeGen, PythonOptions, CodeGenContext};

#[derive(Clone, Debug, FromPyObject)]
pub struct Alias {
    pub name: String,
    pub asname: Option<String>,
}

#[derive(Clone, Debug, FromPyObject)]
pub struct Import {
    pub names: Vec<Alias>,
}

/// An Import (or FromImport) statement causes 2 things to occur:
/// 1. Declares the imported object within the existing scope.
/// 2. Causes the referenced module to be compiled into the program (only once).

impl CodeGen for Import {
    type Context = CodeGenContext;
    type Options = PythonOptions;

    fn to_rust(self, ctx: Self::Context, options: Self::Options) -> Result<TokenStream, Box<dyn std::error::Error>> {
        let mut tokens = TokenStream::new();
        for alias in self.names.iter() {
            let _mod_path = format_ident!("{}", options.python_namespace);
            let names = alias.name.split(".");
            let full_mod_name_list: Vec<&str> = names.clone().collect();
            let full_mod_name = full_mod_name_list.join("::");
            let code = match &alias.asname {
                None => {
                    options.clone().import(&full_mod_name, &full_mod_name);
                    let name = format_ident!("{}", alias.name);
                    quote!{use #(#names)::*:: ::#name}
                },
                Some(n) => {
                    options.clone().import(&full_mod_name, &String::from(n));

                    let name = format_ident!("{}", alias.name);
                    let alias = format_ident!("{}", n);
                    quote!{use #(#names)::*:: ::#name as #alias}
                },
            };
            tokens.extend(code);
        }
        debug!("context: {:?}", ctx);
        debug!("options: {:?}", options);
        debug!("tokens: {}", tokens);
        Ok(tokens)
    }
}

#[derive(Clone, Debug, FromPyObject)]
pub struct ImportFrom {
    pub module: String,
    pub names: Vec<Alias>,
    pub level: usize,
}

impl CodeGen for ImportFrom {
    type Context = CodeGenContext;
    type Options = PythonOptions;

    fn to_rust(self, ctx: Self::Context, _options: Self::Options) -> Result<TokenStream, Box<dyn std::error::Error>> {
        debug!("ctx: {:?}", ctx);
        Ok(quote!{})
    }
}