diff --git a/src/block.rs b/src/block.rs index 57d720e..45e0593 100644 --- a/src/block.rs +++ b/src/block.rs @@ -51,14 +51,7 @@ impl Block { } // Inlined `Formatter::fmt` - - if !fmt.is_start_of_line() { - write!(fmt, " ")?; - } - - write!(fmt, "{{\n")?; - - fmt.indent(|fmt| { + fmt.block(|fmt| { for b in &self.body { b.fmt(fmt)?; } @@ -66,8 +59,6 @@ impl Block { Ok(()) })?; - write!(fmt, "}}")?; - if let Some(ref after) = self.after { write!(fmt, "{}", after)?; } diff --git a/src/enum.rs b/src/enum.rs index 6e08906..f0f77d6 100644 --- a/src/enum.rs +++ b/src/enum.rs @@ -4,7 +4,8 @@ use formatter::Formatter; use type_def::TypeDef; use variant::Variant; -use r#type::Type; +use r#trait::AbsTrait; +use r#struct::AbsStruct; /// Defines an enumeration. @@ -15,6 +16,14 @@ pub struct Enum { } +impl AbsTrait for Enum{ + fn type_def(&mut self) -> &mut TypeDef { + &mut self.type_def + } +} +impl AbsStruct for Enum{} + + impl Enum { /// Return a enum definition with the provided name. pub fn new(name: &str) -> Self { @@ -24,56 +33,6 @@ impl Enum { } } - /// Returns a reference to the type. - pub fn ty(&self) -> &Type { - &self.type_def.ty - } - - /// Set the enum visibility. - pub fn vis(&mut self, vis: &str) -> &mut Self { - self.type_def.vis(vis); - self - } - - /// Add a generic to the enum. - pub fn generic(&mut self, name: &str) -> &mut Self { - self.type_def.ty.generic(name); - self - } - - /// Add a `where` bound to the enum. - pub fn bound(&mut self, name: &str, ty: T) -> &mut Self - where - T: Into, - { - self.type_def.bound(name, ty); - self - } - - /// Set the enum documentation. - pub fn doc(&mut self, docs: &str) -> &mut Self { - self.type_def.doc(docs); - self - } - - /// Add a new type that the struct should derive. - pub fn derive(&mut self, name: &str) -> &mut Self { - self.type_def.derive(name); - self - } - - /// Specify lint attribute to supress a warning or error. - pub fn allow(&mut self, allow: &str) -> &mut Self { - self.type_def.allow(allow); - self - } - - /// Specify representation. - pub fn repr(&mut self, repr: &str) -> &mut Self { - self.type_def.repr(repr); - self - } - /// Push a variant to the enum, returning a mutable reference to it. pub fn new_variant(&mut self, name: &str) -> &mut Variant { self.push_variant(Variant::new(name)); diff --git a/src/formatter.rs b/src/formatter.rs index aa06354..adda808 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -48,7 +48,7 @@ impl<'a> Formatter<'a> { } /// Call the given function with the indentation level incremented by one. - pub fn indent(&mut self, f: F) -> R + fn indent(&mut self, f: F) -> R where F: FnOnce(&mut Self) -> R, { diff --git a/src/lib.rs b/src/lib.rs index b722f4b..ef6f8e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ //! For example: //! //! ```rust -//! use codegen::Scope; +//! use codegen::{AbsStruct, Scope}; //! //! let mut scope = Scope::new(); //! diff --git a/src/struct.rs b/src/struct.rs index ea50916..75d113c 100644 --- a/src/struct.rs +++ b/src/struct.rs @@ -5,6 +5,7 @@ use fields::Fields; use formatter::Formatter; use type_def::TypeDef; +use r#trait::AbsTrait; use r#type::Type; @@ -18,63 +19,42 @@ pub struct Struct { } -impl Struct { - /// Return a structure definition with the provided name - pub fn new(name: &str) -> Self { - Struct { - type_def: TypeDef::new(name), - fields: Fields::Empty, - } - } - - /// Returns a reference to the type - pub fn ty(&self) -> &Type { - &self.type_def.ty - } - - /// Set the structure visibility. - pub fn vis(&mut self, vis: &str) -> &mut Self { - self.type_def.vis(vis); - self - } - - /// Add a generic to the struct. - pub fn generic(&mut self, name: &str) -> &mut Self { - self.type_def.ty.generic(name); +/// AbsStruct +pub trait AbsStruct : AbsTrait { + /// Specify lint attribute to supress a warning or error. + fn allow(&mut self, allow: &str) -> &mut Self { + self.type_def().allow(allow); self } - /// Add a `where` bound to the struct. - pub fn bound(&mut self, name: &str, ty: T) -> &mut Self - where - T: Into, - { - self.type_def.bound(name, ty); + /// Add a new type that the struct should derive. + fn derive(&mut self, name: &str) -> &mut Self { + self.type_def().derive(name); self } - /// Set the structure documentation. - pub fn doc(&mut self, docs: &str) -> &mut Self { - self.type_def.doc(docs); + /// Specify representation. + fn repr(&mut self, repr: &str) -> &mut Self { + self.type_def().repr(repr); self } +} - /// Add a new type that the struct should derive. - pub fn derive(&mut self, name: &str) -> &mut Self { - self.type_def.derive(name); - self - } - /// Specify lint attribute to supress a warning or error. - pub fn allow(&mut self, allow: &str) -> &mut Self { - self.type_def.allow(allow); - self +impl AbsTrait for Struct{ + fn type_def(&mut self) -> &mut TypeDef { + &mut self.type_def } +} +impl AbsStruct for Struct{} - /// Specify representation. - pub fn repr(&mut self, repr: &str) -> &mut Self { - self.type_def.repr(repr); - self +impl Struct { + /// Return a structure definition with the provided name + pub fn new(name: &str) -> Self { + Struct { + type_def: TypeDef::new(name), + fields: Fields::Empty, + } } /// Push a named field to the struct. diff --git a/src/trait.rs b/src/trait.rs index 1a100b1..55ce2c8 100644 --- a/src/trait.rs +++ b/src/trait.rs @@ -20,43 +20,63 @@ pub struct Trait { } -impl Trait { - /// Return a trait definition with the provided name - pub fn new(name: &str) -> Self { - Trait { - type_def: TypeDef::new(name), - parents: vec![], - associated_tys: vec![], - fns: vec![], - macros: vec![], - } - } +/// AbsTrait +pub trait AbsTrait { + /// Get type_def instance. + fn type_def(&mut self) -> &mut TypeDef; - /// Returns a reference to the type - pub fn ty(&self) -> &Type { - &self.type_def.ty + /// Add a `where` bound. + fn bound(&mut self, name: &str, ty: T) -> &mut Self + where + T: Into, + { + self.type_def().bound(name, ty); + self } - /// Set the trait visibility. - pub fn vis(&mut self, vis: &str) -> &mut Self { - self.type_def.vis(vis); + /// Set the documentation. + fn doc(&mut self, docs: &str) -> &mut Self { + self.type_def().doc(docs); self } - /// Add a generic to the trait - pub fn generic(&mut self, name: &str) -> &mut Self { - self.type_def.ty.generic(name); + /// Add a generic. + fn generic(&mut self, name: &str) -> &mut Self { + self.type_def().ty.generic(name); self } - /// Add a `where` bound to the trait. - pub fn bound(&mut self, name: &str, ty: T) -> &mut Self - where - T: Into, - { - self.type_def.bound(name, ty); + /// Returns a reference to the type + fn ty(&mut self) -> &Type { + &self.type_def().ty + } + + /// Set the visibility. + fn vis(&mut self, vis: &str) -> &mut Self { + self.type_def().vis(vis); self } +} + + +impl AbsTrait for Trait +{ + fn type_def(&mut self) -> &mut TypeDef { + &mut self.type_def + } +} + +impl Trait { + /// Return a trait definition with the provided name + pub fn new(name: &str) -> Self { + Trait { + type_def: TypeDef::new(name), + parents: vec![], + associated_tys: vec![], + fns: vec![], + macros: vec![], + } + } /// Add a macro to the trait def (e.g. `"#[async_trait]"`) pub fn r#macro(&mut self, r#macro: &str) -> &mut Self { @@ -73,12 +93,6 @@ impl Trait { self } - /// Set the trait documentation. - pub fn doc(&mut self, docs: &str) -> &mut Self { - self.type_def.doc(docs); - self - } - /// Add an associated type. Returns a mutable reference to the new /// associated type for futher configuration. pub fn associated_type(&mut self, name: &str) -> &mut AssociatedType { diff --git a/src/type.rs b/src/type.rs index 61c4637..79469f7 100644 --- a/src/type.rs +++ b/src/type.rs @@ -74,30 +74,30 @@ impl Type { Ok(()) } - } +} - impl<'a> From<&'a str> for Type { +impl<'a> From<&'a str> for Type { fn from(src: &'a str) -> Self { Type::new(src) } - } +} - impl From for Type { +impl From for Type { fn from(src: String) -> Self { Type { name: src, generics: vec![], } } - } +} - impl<'a> From<&'a String> for Type { +impl<'a> From<&'a String> for Type { fn from(src: &'a String) -> Self { Type::new(src) } - } +} - impl<'a> From<&'a Type> for Type { +impl<'a> From<&'a Type> for Type { fn from(src: &'a Type) -> Self { src.clone() }