Skip to content

Commit

Permalink
Add support for imports
Browse files Browse the repository at this point in the history
  • Loading branch information
shadaj committed Jul 22, 2021
1 parent 0607daf commit ebbf0d5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
46 changes: 37 additions & 9 deletions core/src/ast/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use quote::ToTokens;
use syn::{ImplItem, Item, ItemMod};
use syn::{ImplItem, Item, ItemMod, UseTree};

use super::{CustomType, Method, ModSymbol, OpaqueStruct, Path, Struct, TypeName};

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Module {
pub name: String,
pub imports: Vec<(Path, String)>,
pub declared_types: HashMap<String, CustomType>,
pub sub_modules: Vec<Module>,
}
Expand All @@ -35,6 +36,11 @@ impl Module {

fn insert_all_types(&self, in_path: Path, out: &mut HashMap<Path, HashMap<String, ModSymbol>>) {
let mut mod_symbols = HashMap::new();

self.imports.iter().for_each(|(path, name)| {
mod_symbols.insert(name.clone(), ModSymbol::Alias(path.clone()));
});

self.declared_types.iter().for_each(|(k, v)| {
if mod_symbols
.insert(k.clone(), ModSymbol::CustomType(v.clone()))
Expand All @@ -47,10 +53,7 @@ impl Module {
let path_to_self = in_path.sub_path(self.name.clone());
self.sub_modules.iter().for_each(|m| {
m.insert_all_types(path_to_self.clone(), out);
mod_symbols.insert(
m.name.clone(),
ModSymbol::Alias(path_to_self.sub_path(m.name.clone())),
);
mod_symbols.insert(m.name.clone(), ModSymbol::SubModule(m.name.clone()));
});

out.insert(path_to_self, mod_symbols);
Expand All @@ -59,6 +62,7 @@ impl Module {
pub fn from_syn(input: &ItemMod, force_analyze: bool) -> Module {
let mut custom_types_by_name = HashMap::new();
let mut sub_modules = Vec::new();
let mut imports = Vec::new();

let analyze_types = force_analyze
|| input
Expand All @@ -73,6 +77,9 @@ impl Module {
.1
.iter()
.for_each(|a| match a {
Item::Use(u) => {
extract_imports(&Path::empty(), &u.tree, &mut imports);
}
Item::Struct(strct) => {
if analyze_types {
if strct
Expand Down Expand Up @@ -130,12 +137,36 @@ impl Module {

Module {
name: input.ident.to_string(),
imports,
declared_types: custom_types_by_name,
sub_modules,
}
}
}

fn extract_imports(base_path: &Path, use_tree: &UseTree, out: &mut Vec<(Path, String)>) {
match use_tree {
UseTree::Name(name) => out.push((
base_path.sub_path(name.ident.to_string()),
name.ident.to_string(),
)),
UseTree::Path(path) => {
extract_imports(&base_path.sub_path(path.ident.to_string()), &path.tree, out)
}
UseTree::Glob(_) => todo!("Glob imports are not yet supported"),
UseTree::Group(group) => {
group
.items
.iter()
.for_each(|i| extract_imports(base_path, &i, out));
}
UseTree::Rename(rename) => out.push((
base_path.sub_path(rename.ident.to_string()),
rename.rename.to_string(),
)),
}
}

#[derive(Clone, Debug)]
pub struct File {
pub modules: HashMap<String, Module>,
Expand Down Expand Up @@ -163,10 +194,7 @@ impl File {

self.modules.values().for_each(|m| {
m.insert_all_types(Path::empty(), &mut out);
top_symbols.insert(
m.name.clone(),
ModSymbol::Alias(Path::empty().sub_path(m.name.clone())),
);
top_symbols.insert(m.name.clone(), ModSymbol::SubModule(m.name.clone()));
});

out.insert(Path::empty(), top_symbols);
Expand Down
14 changes: 13 additions & 1 deletion core/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ impl CustomType {
pub enum ModSymbol {
/// A symbol that is a pointer to another path.
Alias(Path),
/// A symbol that is a submodule.
SubModule(String),
/// A symbol that is a custom type.
CustomType(CustomType),
}
Expand Down Expand Up @@ -187,7 +189,17 @@ impl TypeName {
"super" => cur_path = cur_path.get_super(),

o => match env.get(&cur_path).and_then(|env| env.get(o)) {
Some(ModSymbol::Alias(p)) => cur_path = p.clone(),
Some(ModSymbol::Alias(p)) => {
let mut remaining_elements: Vec<String> =
local_path.elements.iter().skip(i + 1).cloned().collect();
let mut new_path = p.elements.clone();
new_path.append(&mut remaining_elements);
return TypeName::Named(Path { elements: new_path })
.resolve_with_path(&cur_path.clone(), env);
}
Some(ModSymbol::SubModule(name)) => {
cur_path.elements.push(name.clone());
}
Some(ModSymbol::CustomType(t)) => {
if i == local_path.elements.len() - 1 {
return (cur_path, t);
Expand Down
8 changes: 5 additions & 3 deletions example/src/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod ffi {
use icu_provider::serde::SerdeDeDataProvider;
use writeable::Writeable;

use crate::fixed_decimal::ffi::ICU4XFixedDecimal;

#[diplomat::opaque]
/// An ICU4X Locale, capable of representing strings like `"en-US"`.
/// See [the Rust docs](https://unicode-org.github.io/icu4x-docs/doc/icu/locid/struct.Locale.html) for more information.
Expand All @@ -38,7 +40,7 @@ mod ffi {
}

#[diplomat::opaque]
/// An ICU4X Fixed Decimal Format object, capable of formatting a [`crate::fixed_decimal::ffi::ICU4XFixedDecimal`] as a string.
/// An ICU4X Fixed Decimal Format object, capable of formatting a [`ICU4XFixedDecimal`] as a string.
/// See [the Rust docs](https://unicode-org.github.io/icu4x-docs/doc/icu/decimal/struct.FixedDecimalFormat.html) for more information.
pub struct ICU4XFixedDecimalFormat(pub FixedDecimalFormat<'static, 'static>);

Expand Down Expand Up @@ -106,10 +108,10 @@ mod ffi {
}
}

/// Formats a [`crate::fixed_decimal::ffi::ICU4XFixedDecimal`] to a string. See [the Rust docs](https://unicode-org.github.io/icu4x-docs/doc/icu/decimal/struct.FixedDecimalFormat.html#method.format) for more information.
/// Formats a [`ICU4XFixedDecimal`] to a string. See [the Rust docs](https://unicode-org.github.io/icu4x-docs/doc/icu/decimal/struct.FixedDecimalFormat.html#method.format) for more information.
fn format_write(
&self,
value: &crate::fixed_decimal::ffi::ICU4XFixedDecimal,
value: &ICU4XFixedDecimal,
write: &mut diplomat_runtime::DiplomatWriteable,
) {
self.0.format(&value.0).write_to(write).unwrap();
Expand Down
2 changes: 0 additions & 2 deletions tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ fn main() -> std::io::Result<()> {
panic!();
}

dbg!(&env);

let args: Vec<String> = env::args().collect();
let target = args[1].as_str();

Expand Down

0 comments on commit ebbf0d5

Please sign in to comment.