use std::error::Error;
use std::fmt;
use he_di_internals::error::Error as DIError;
use quote::{ Tokens, ToTokens };
use syn::{ self, DeriveInput, Field, Ident };
use super::ParsingContext;
use parser::Parser;
#[derive(Clone, Debug)]
pub struct ComponentContainer {
pub metadata: MetaData,
pub identifier: Identifier,
pub properties: Vec<Property>,
pub _body: syn::Body,
}
impl ComponentContainer {
#![allow(dead_code)]
pub fn from_derive_input(ctxt: &ParsingContext, input: &DeriveInput) -> Self {
ComponentContainer {
metadata: input.parse_into()
.or_else::<DIError, _>(|di_err| {
ctxt.error(di_err.description());
Ok(MetaData { interface: None })
}).unwrap(),
identifier: input.parse_into()
.or_else::<DIError, _>(|di_err| {
ctxt.error(di_err.description());
Ok(Identifier::Null)
}).unwrap(),
properties: input.parse_into()
.or_else::<DIError, _>(|di_err| {
ctxt.error(di_err.description());
Ok(Vec::new())
}).unwrap(),
_body: input.body.clone(),
}
}
pub fn is_struct(&self) -> bool {
self.identifier.is_struct()
}
pub fn is_enum(&self) -> bool {
self.identifier.is_enum()
}
}
#[derive(Clone, Debug)]
pub struct MetaData {
pub interface: Option<syn::Ident>,
}
#[derive(Clone, Debug)]
pub enum Identifier {
Null,
Enum(syn::Ident),
Struct(syn::Ident),
}
impl Identifier {
#![allow(dead_code)]
pub fn get_name(&self) -> &syn::Ident {
match *self {
Identifier::Enum(ref ident) => ident,
Identifier::Struct(ref ident) => ident,
Identifier::Null => panic!("trying to get name from an empty Identifier"),
}
}
pub fn is_struct(&self) -> bool {
match *self {
Identifier::Enum(_) => false,
Identifier::Struct(_) => true,
Identifier::Null => false,
}
}
pub fn is_enum(&self) -> bool {
match *self {
Identifier::Enum(_) => true,
Identifier::Struct(_) => false,
Identifier::Null => false,
}
}
}
#[derive(Clone)]
pub struct Property {
pub property_name: Option<Ident>,
pub traits: Option<Vec<syn::Path>>,
pub is_parsed: bool,
pub is_boxed: bool,
pub is_injected: bool,
pub _field: Field,
}
impl fmt::Debug for Property {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "Property {{ property_name: {:?}, traits: {:?}, is_parsed: {:?}, is_boxed: {:?}, is_injected: {:?} }}", self.property_name, self.traits, self.is_parsed, self.is_boxed, self.is_injected)
}
}
impl Property {
pub fn is_component(&self) -> bool {
self.is_parsed &&
self.is_injected &&
self.property_name.is_some() &&
self.traits.is_some() &&
self.traits.as_ref().unwrap().len() == 1
}
pub fn name_to_tokens(&self, tokens: &mut Tokens) {
if self.property_name.is_some() {
self.property_name.as_ref().unwrap().to_tokens(tokens)
}
}
pub fn type_to_tokens(&self, tokens: &mut Tokens) {
if self.is_parsed && self.traits.is_some() && self.traits.as_ref().unwrap().len() > 0 {
if self.traits.as_ref().unwrap().len() > 1 {
warn!("warning: {} traits entries for property {:?} while expecting only 1 > traits = {:?}", self.traits.as_ref().unwrap().len(), self.property_name, self.traits.as_ref().unwrap());
}
self.traits.as_ref().unwrap().get(0).to_tokens(tokens);
} else {
self._field.ty.to_tokens(tokens);
}
}
pub fn get_name_without_quotes(&self) -> String {
self.get_name().replace("\"", "")
}
pub fn get_name(&self) -> String {
self.property_name.as_ref()
.map(|ident| ident.to_string())
.unwrap_or("".to_string())
}
}