Skip to content

Removed duplicated code using Traits #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,14 @@ 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)?;
}

Ok(())
})?;

write!(fmt, "}}")?;

if let Some(ref after) = self.after {
write!(fmt, "{}", after)?;
}
Expand Down
61 changes: 10 additions & 51 deletions src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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<T>(&mut self, name: &str, ty: T) -> &mut Self
where
T: Into<Type>,
{
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));
Expand Down
2 changes: 1 addition & 1 deletion src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<'a> Formatter<'a> {
}

/// Call the given function with the indentation level incremented by one.
pub fn indent<F, R>(&mut self, f: F) -> R
fn indent<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! For example:
//!
//! ```rust
//! use codegen::Scope;
//! use codegen::{AbsStruct, Scope};
//!
//! let mut scope = Scope::new();
//!
Expand Down
70 changes: 25 additions & 45 deletions src/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use fields::Fields;
use formatter::Formatter;
use type_def::TypeDef;

use r#trait::AbsTrait;
use r#type::Type;


Expand All @@ -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<T>(&mut self, name: &str, ty: T) -> &mut Self
where
T: Into<Type>,
{
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.
Expand Down
78 changes: 46 additions & 32 deletions src/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(&mut self, name: &str, ty: T) -> &mut Self
where
T: Into<Type>,
{
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<T>(&mut self, name: &str, ty: T) -> &mut Self
where
T: Into<Type>,
{
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 {
Expand All @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> for Type {
impl From<String> 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()
}
Expand Down