-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22d03fc
commit 4f83cfe
Showing
1 changed file
with
45 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,85 @@ | ||
use std::collections::HashSet; | ||
|
||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use syn::{Path, Type}; | ||
|
||
pub struct Dependencies { | ||
dependencies: Vec<TokenStream>, | ||
crate_rename: Path, | ||
dependencies: HashSet<Operation>, | ||
pub types: Vec<Type>, | ||
} | ||
|
||
#[derive(Hash, Eq, PartialEq)] | ||
enum Operation { | ||
ExtendByDependencyTypes { crate_rename: Path, ty: Type }, | ||
ExtendGenerics { crate_rename: Path, ty: Type }, | ||
PushType(Type), | ||
} | ||
|
||
impl Dependencies { | ||
pub fn new(crate_rename: Path) -> Self { | ||
Self { | ||
dependencies: Vec::default(), | ||
dependencies: HashSet::default(), | ||
crate_rename, | ||
types: Vec::default(), | ||
} | ||
} | ||
|
||
/// Adds all dependencies from the given type | ||
pub fn append_from(&mut self, ty: &Type) { | ||
let crate_rename = &self.crate_rename; | ||
self.dependencies | ||
.push(quote![.extend(<#ty as #crate_rename::TS>::dependency_types())]); | ||
.insert(Operation::ExtendByDependencyTypes { | ||
crate_rename: self.crate_rename.clone(), | ||
ty: ty.clone(), | ||
}); | ||
|
||
self.types.push(ty.clone()); | ||
} | ||
|
||
/// Adds the given type. | ||
pub fn push(&mut self, ty: &Type) { | ||
let crate_rename = &self.crate_rename; | ||
self.dependencies.push(quote![.push::<#ty>()]); | ||
self.dependencies.push(quote![ | ||
.extend(<#ty as #crate_rename::TS>::generics()) | ||
]); | ||
self.dependencies.insert(Operation::PushType(ty.clone())); | ||
self.dependencies.insert(Operation::ExtendGenerics { | ||
crate_rename: self.crate_rename.clone(), | ||
ty: ty.clone(), | ||
}); | ||
self.types.push(ty.clone()); | ||
} | ||
|
||
pub fn append(&mut self, mut other: Dependencies) { | ||
if !other.dependencies.is_empty() { | ||
self.dependencies.push(quote![.extend(#other)]); | ||
self.dependencies.extend(other.dependencies); | ||
|
||
if !other.types.is_empty() { | ||
self.types.append(&mut other.types); | ||
} | ||
self.types.append(&mut other.types); | ||
} | ||
} | ||
|
||
impl ToTokens for Dependencies { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
let crate_rename = &self.crate_rename; | ||
let lines = &self.dependencies; | ||
let lines = self.dependencies.iter(); | ||
|
||
tokens.extend(quote![{ | ||
use #crate_rename::typelist::TypeList; | ||
()#(#lines)* | ||
}]) | ||
}]); | ||
} | ||
} | ||
|
||
impl ToTokens for Operation { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
tokens.extend(match self { | ||
Operation::PushType(ty) => quote![.push::<#ty>()], | ||
Operation::ExtendByDependencyTypes { crate_rename, ty } => { | ||
quote![.extend(<#ty as #crate_rename::TS>::dependency_types())] | ||
} | ||
Operation::ExtendGenerics { crate_rename, ty } => { | ||
quote![ | ||
.extend(<#ty as #crate_rename::TS>::generics()) | ||
] | ||
} | ||
}); | ||
} | ||
} |