Skip to content

Commit

Permalink
path type into ModulePath #2 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
adjivas committed Oct 23, 2017
1 parent b93a038 commit faaa464
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 58 deletions.
15 changes: 8 additions & 7 deletions src/core/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ pub mod state;
pub use self::state::ItemState;

use std::{slice, iter};
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::{ptr, ast};
use ::itertools::Itertools;

use ::module::path::ModulePath;

/// The structure Item is a iterable collection of abstract elements.

#[derive(Debug, Clone)]
pub struct Item <'a> {
/// Iterator.
it: iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<Vec<OsString>>)>>,
it: iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<ModulePath>)>>,
}

impl <'a>From<iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<Vec<OsString>>)>>> for Item<'a> {
impl <'a>From<iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<ModulePath>)>>> for Item<'a> {

/// The constructor method `from` returns a typed and iterable collection of abstract element.
fn from(iter: iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<Vec<OsString>>)>>) -> Item {
fn from(iter: iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<ModulePath>)>>) -> Item {
Item {
it: iter,
}
Expand All @@ -35,16 +36,16 @@ impl <'a>Iterator for Item<'a> {
/// enumeration or trait.
fn next(&mut self) -> Option<ItemState<'a>> {
self.it.next().and_then(|item| {
let mut list: Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)> = vec!(item);
let mut list: Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)> = vec!(item);

list.extend(self.it.peeking_take_while(|&&(ref item, _): (&&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>))| {
list.extend(self.it.peeking_take_while(|&&(ref item, _): (&&'a (ptr::P<ast::Item>, Rc<ModulePath>))| {
if let ast::ItemKind::Impl(..) = item.node {
true
} else {
false
}
})
.collect::<Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>>());
.collect::<Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>>());
Some(ItemState::from(list))
})
}
Expand Down
9 changes: 5 additions & 4 deletions src/core/item/state/abstraction/enumerate.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
use std::fmt;
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::print::pprust::ty_to_string;
use ::syntex_syntax::{codemap, symbol, ast};

use ::module::path::ModulePath;

use ::dot::escape_html;

/// The structure `Enum` is a enumerate abstract element.

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Enum<'a> {
pub path: Rc<Vec<OsString>>,
pub path: Rc<ModulePath>,
/// Visibility
pub vis: &'a ast::Visibility,
pub name: symbol::InternedString,
pub params: Vec<symbol::InternedString>,
pub variants: Vec<(symbol::InternedString, Vec<String>)>,
}

impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<Vec<OsString>>)> for Enum<'a> {
fn from(((item, ty_params, variants), path): ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<Vec<OsString>>)) -> Enum<'a> {
impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<ModulePath>)> for Enum<'a> {
fn from(((item, ty_params, variants), path): ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<ModulePath>)) -> Enum<'a> {
Enum {
path: path,
vis: &item.vis,
Expand Down
9 changes: 5 additions & 4 deletions src/core/item/state/abstraction/extend.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use std::fmt;
use std::ops::Deref;
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::print::pprust::ty_to_string;
use ::syntex_syntax::{symbol, ast};

use ::module::path::ModulePath;

use ::dot::escape_html;

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Trait<'a> {
pub path: Rc<Vec<OsString>>,
pub path: Rc<ModulePath>,
/// Visibility
pub vis: &'a ast::Visibility,
pub name: symbol::InternedString,
pub params: Vec<symbol::InternedString>,
pub items: Vec<(symbol::InternedString, Vec<String>, String)>,
}

impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<Vec<OsString>>)> for Trait<'a> {
fn from(((item, ty_params, trait_item), path): ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<Vec<OsString>>)) -> Trait<'a> {
impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<ModulePath>)> for Trait<'a> {
fn from(((item, ty_params, trait_item), path): ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<ModulePath>)) -> Trait<'a> {
Trait {
path: path,
vis: &item.vis,
Expand Down
15 changes: 8 additions & 7 deletions src/core/item/state/abstraction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ pub mod enumerate;

use std::fmt;
use std::vec;
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::symbol;
use ::core::ast;

use ::module::path::ModulePath;

use self::extend::Trait;
use self::structure::Struct;
use self::enumerate::Enum;
Expand Down Expand Up @@ -73,20 +74,20 @@ impl <'a> Default for Abstract<'a> {
}
}

impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<Vec<OsString>>)> for Abstract<'a> {
fn from(arguments: ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<Vec<OsString>>)) -> Abstract<'a> {
impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<ModulePath>)> for Abstract<'a> {
fn from(arguments: ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>), Rc<ModulePath>)) -> Abstract<'a> {
Abstract::Trait(Trait::from(arguments))
}
}

impl <'a>From<((&'a ast::Item, &'a Vec<ast::StructField>), Rc<Vec<OsString>>)> for Abstract<'a> {
fn from(arguments: ((&'a ast::Item, &'a Vec<ast::StructField>), Rc<Vec<OsString>>)) -> Abstract<'a> {
impl <'a>From<((&'a ast::Item, &'a Vec<ast::StructField>), Rc<ModulePath>)> for Abstract<'a> {
fn from(arguments: ((&'a ast::Item, &'a Vec<ast::StructField>), Rc<ModulePath>)) -> Abstract<'a> {
Abstract::Struct(Struct::from(arguments))
}
}

impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<Vec<OsString>>)> for Abstract<'a> {
fn from(arguments: ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<Vec<OsString>>)) -> Abstract<'a> {
impl <'a>From<((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<ModulePath>)> for Abstract<'a> {
fn from(arguments: ((&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>), Rc<ModulePath>)) -> Abstract<'a> {
Abstract::Enum(Enum::from(arguments))
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/core/item/state/abstraction/structure.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use std::fmt;
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::print::pprust::ty_to_string;
use ::syntex_syntax::{symbol, ast};

use ::module::path::ModulePath;

use ::dot::escape_html;

/// The structure `Struct` is a structure abstract element.

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Struct<'a> {
pub path: Rc<Vec<OsString>>,
pub path: Rc<ModulePath>,
/// Visibility
pub vis: &'a ast::Visibility,
pub name: symbol::InternedString,
pub fields: Vec<(&'a ast::Visibility, symbol::InternedString, String)>,
}

impl <'a>From<((&'a ast::Item, &'a Vec<ast::StructField>), Rc<Vec<OsString>>)> for Struct<'a> {
fn from(((item, struct_field), path): ((&'a ast::Item, &'a Vec<ast::StructField>), Rc<Vec<OsString>>)) -> Struct<'a> {
impl <'a>From<((&'a ast::Item, &'a Vec<ast::StructField>), Rc<ModulePath>)> for Struct<'a> {
fn from(((item, struct_field), path): ((&'a ast::Item, &'a Vec<ast::StructField>), Rc<ModulePath>)) -> Struct<'a> {
Struct {
path: path,
vis: &item.vis,
Expand Down
13 changes: 7 additions & 6 deletions src/core/item/state/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use super::DEFAULT_FUNC;

use std::ops::Deref;
use std::fmt;
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::print::pprust::{ty_to_string, arg_to_string};
use ::syntex_syntax::symbol::InternedString;
use ::syntex_syntax::ast;

use ::module::path::ModulePath;

use ::dot::escape_html;

/// The structure `Method` is a collection of methods from a abstract element.
Expand All @@ -17,7 +18,7 @@ use ::dot::escape_html;
pub struct Method <'a> {
/// visibility, method's name, arguments, result.
func: Vec<(&'a ast::Visibility, InternedString, Vec<String>, Option<String>)>,
path: Rc<Vec<OsString>>,
path: Rc<ModulePath>,
}

impl <'a> Method <'a> {
Expand All @@ -40,17 +41,17 @@ impl <'a> Method <'a> {
}
}

impl <'a> From<(Vec<(&'a ast::Visibility, InternedString, Vec<String>, Option<String>)>, Rc<Vec<OsString>>)> for Method<'a> {
fn from((func, path): (Vec<(&'a ast::Visibility, InternedString, Vec<String>, Option<String>)>, Rc<Vec<OsString>>)) -> Method<'a> {
impl <'a> From<(Vec<(&'a ast::Visibility, InternedString, Vec<String>, Option<String>)>, Rc<ModulePath>)> for Method<'a> {
fn from((func, path): (Vec<(&'a ast::Visibility, InternedString, Vec<String>, Option<String>)>, Rc<ModulePath>)) -> Method<'a> {
Method {
func: func,
path: path,
}
}
}

impl <'a> From<(&'a Vec<ast::ImplItem>, Rc<Vec<OsString>>)> for Method<'a> {
fn from((impl_item, path): (&'a Vec<ast::ImplItem>, Rc<Vec<OsString>>)) -> Method<'a> {
impl <'a> From<(&'a Vec<ast::ImplItem>, Rc<ModulePath>)> for Method<'a> {
fn from((impl_item, path): (&'a Vec<ast::ImplItem>, Rc<ModulePath>)) -> Method<'a> {
Method::from((impl_item.iter()
.filter_map(|&ast::ImplItem {id: _, ident: ast::Ident { name, ..}, ref vis, defaultness: _, attrs: _, ref node, .. }| {
if let &ast::ImplItemKind::Method(ast::MethodSig {unsafety: _, constness: _, abi: _, ref decl, ..}, _) = node {
Expand Down
23 changes: 12 additions & 11 deletions src/core/item/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use super::relation::Relation;

use std::ops::BitOr;
use std::fmt;
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::symbol::InternedString;
use ::syntex_syntax::{ptr, ast};

use ::module::path::ModulePath;

/// The structure `ItemState` describes an abstract element with a collections of methodes
/// and implementations.
#[derive(Default, Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -122,12 +123,12 @@ impl <'a> ItemState <'a> {
}
}

impl <'a>From<(Abstract<'a>, Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>)> for ItemState<'a> {
fn from((node, properties): (Abstract<'a>, Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>)) -> ItemState<'a> {
impl <'a>From<(Abstract<'a>, Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>)> for ItemState<'a> {
fn from((node, properties): (Abstract<'a>, Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>)) -> ItemState<'a> {
ItemState {
node: node,
method: properties.iter()
.filter_map(|&&(ref item, ref path): (&&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>))|
.filter_map(|&&(ref item, ref path): (&&'a (ptr::P<ast::Item>, Rc<ModulePath>))|
if let ast::ItemKind::Impl(_, _, _, _, None, _, ref impl_item) = item.node {
Some(Method::from((impl_item, Rc::clone(path))))
} else {
Expand All @@ -136,7 +137,7 @@ impl <'a>From<(Abstract<'a>, Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>)> f
)
.collect::<Vec<Method>>(),
implem: properties.iter()
.filter_map(|&&(ref item, _): (&&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>))|
.filter_map(|&&(ref item, _): (&&'a (ptr::P<ast::Item>, Rc<ModulePath>))|
if let ast::ItemKind::Impl(_, _, _, _, Some(ast::TraitRef {path: ast::Path {span: _, ref segments}, ..}), _, ref impl_item) = item.node {
Some(Implem::from((segments, impl_item)))
} else {
Expand All @@ -148,26 +149,26 @@ impl <'a>From<(Abstract<'a>, Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>)> f
}
}

impl <'a>From<Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>> for ItemState<'a> {
fn from(state: Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>) -> ItemState<'a> {
state.split_first().and_then(|(&&(ref item, ref path), properties): (&&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>), &[&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)])| {
impl <'a>From<Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>> for ItemState<'a> {
fn from(state: Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>) -> ItemState<'a> {
state.split_first().and_then(|(&&(ref item, ref path), properties): (&&'a (ptr::P<ast::Item>, Rc<ModulePath>), &[&'a (ptr::P<ast::Item>, Rc<ModulePath>)])| {
match &item.node {
/// Trait.
&ast::ItemKind::Trait(_, ast::Generics {lifetimes: _, ref ty_params, ..}, _, ref trait_item) => {
let kind: (&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::TraitItem>) = (item, ty_params, trait_item);
let kind: (Abstract, Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>) = (Abstract::from((kind, Rc::clone(path))), properties.to_vec());
let kind: (Abstract, Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>) = (Abstract::from((kind, Rc::clone(path))), properties.to_vec());
Some(ItemState::from(kind))
},
/// Structure with variables.
&ast::ItemKind::Struct(ast::VariantData::Struct(ref struct_field, _), ..) => {
let kind: (&'a ast::Item, &'a Vec<ast::StructField>) = (item, struct_field);
let kind: (Abstract, Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>) = (Abstract::from((kind, Rc::clone(path))), properties.to_vec());
let kind: (Abstract, Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>) = (Abstract::from((kind, Rc::clone(path))), properties.to_vec());
Some(ItemState::from(kind))
},
/// Enumeration with variables.
&ast::ItemKind::Enum(ast::EnumDef {ref variants}, ast::Generics {lifetimes: _, ref ty_params, ..}) => {
let kind: (&'a ast::Item, &'a Vec<ast::TyParam>, &'a Vec<ast::Variant>) = (item, ty_params, variants);
let kind: (Abstract, Vec<&'a (ptr::P<ast::Item>, Rc<Vec<OsString>>)>) = (Abstract::from((kind, Rc::clone(path))), properties.to_vec());
let kind: (Abstract, Vec<&'a (ptr::P<ast::Item>, Rc<ModulePath>)>) = (Abstract::from((kind, Rc::clone(path))), properties.to_vec());
Some(ItemState::from(kind))
},
_ => None,
Expand Down
7 changes: 4 additions & 3 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ use self::item::relation::Relation;
use std::{slice, iter};
use std::borrow::Cow;
use std::ops::BitOr;
use std::ffi::OsString;
use std::rc::Rc;

use ::syntex_syntax::{ptr, ast};
use ::dot::{Nodes, Edges, Arrow, Style, GraphWalk, Labeller, LabelText, Id};
use ::itertools::Itertools;

use ::module::path::ModulePath;

#[derive(Debug, Clone)]
pub struct ListItem <'a> {
parse: Item<'a>,
Expand All @@ -30,8 +31,8 @@ impl <'a> From<Item<'a>> for ListItem <'a> {
}
}

impl <'a> From<iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<Vec<OsString>>)>>> for ListItem <'a> {
fn from(list: iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<Vec<OsString>>)>>) -> ListItem <'a> {
impl <'a> From<iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<ModulePath>)>>> for ListItem <'a> {
fn from(list: iter::Peekable<slice::Iter<'a, (ptr::P<ast::Item>, Rc<ModulePath>)>>) -> ListItem <'a> {
ListItem::from(Item::from(list))
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use std::process::{Command, Stdio};
use std::io::{self, Write, Read};
use std::path::Path;
use std::fs::{self, File};
use std::ffi::{OsStr, OsString};
use std::ffi::OsStr;
use std::rc::Rc;

use syntex_errors::emitter::ColorConfig;
Expand All @@ -46,6 +46,7 @@ use syntex_syntax::{ast, ptr};
use walkdir::WalkDir;
use core::ListItem;
use module::Module;
use module::path::ModulePath;

/// The default name of *graph/dot* file.
pub const DEFAULT_NAME_DOT: &'static str = "ml.dot";
Expand All @@ -67,10 +68,10 @@ fn file2crate<P: AsRef<Path>>(path: P) -> io::Result<ast::Crate> {
/// The function `items2chars` returns a graph formated for *Graphiz/Dot*.
fn items2chars<'a>(modules: Vec<Module>) -> io::Result<Vec<u8>> {
let mut f: Vec<u8> = Vec::new();
let itt: Vec<(ptr::P<ast::Item>, Rc<Vec<OsString>>)> =
let itt: Vec<(ptr::P<ast::Item>, Rc<ModulePath>)> =
modules.into_iter()
.flat_map(|s: Module| s.into_iter())
.collect::<Vec<(ptr::P<ast::Item>, Rc<Vec<OsString>>)>>();
.collect::<Vec<(ptr::P<ast::Item>, Rc<ModulePath>)>>();
let it: ListItem = ListItem::from(itt.as_slice().into_iter().peekable());

dot::render(&it, &mut f).and_then(|()| Ok(f))
Expand Down
Loading

0 comments on commit faaa464

Please sign in to comment.