Skip to content

Add annotations for structs, enums and variants. #34

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 2 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
204 changes: 105 additions & 99 deletions src/enum.rs
Original file line number Diff line number Diff line change
@@ -1,99 +1,105 @@
use std::fmt;

use crate::formatter::Formatter;
use crate::type_def::TypeDef;
use crate::variant::Variant;

use crate::r#type::Type;

/// Defines an enumeration.
#[derive(Debug, Clone)]
pub struct Enum {
type_def: TypeDef,
variants: Vec<Variant>,
}

impl Enum {
/// Return a enum definition with the provided name.
pub fn new(name: &str) -> Self {
Enum {
type_def: TypeDef::new(name),
variants: vec![],
}
}

/// 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));
self.variants.last_mut().unwrap()
}

/// Push a variant to the enum.
pub fn push_variant(&mut self, item: Variant) -> &mut Self {
self.variants.push(item);
self
}

/// Formats the enum using the given formatter.
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.type_def.fmt_head("enum", &[], fmt)?;

fmt.block(|fmt| {
for variant in &self.variants {
variant.fmt(fmt)?;
}

Ok(())
})
}
}
use std::fmt;

use crate::formatter::Formatter;
use crate::type_def::TypeDef;
use crate::variant::Variant;

use crate::r#type::Type;

/// Defines an enumeration.
#[derive(Debug, Clone)]
pub struct Enum {
type_def: TypeDef,
variants: Vec<Variant>,
}

impl Enum {
/// Return a enum definition with the provided name.
pub fn new(name: &str) -> Self {
Enum {
type_def: TypeDef::new(name),
variants: vec![],
}
}

/// 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
}

/// Add an arbitrary macro.
pub fn r#macro(&mut self, r#macro: &str) -> &mut Self {
self.type_def.r#macro(r#macro);
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));
self.variants.last_mut().unwrap()
}

/// Push a variant to the enum.
pub fn push_variant(&mut self, item: Variant) -> &mut Self {
self.variants.push(item);
self
}

/// Formats the enum using the given formatter.
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.type_def.fmt_head("enum", &[], fmt)?;

fmt.block(|fmt| {
for variant in &self.variants {
variant.fmt(fmt)?;
}

Ok(())
})
}
}
132 changes: 67 additions & 65 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,67 @@
#![deny(missing_debug_implementations, missing_docs)]
#![doc(html_root_url = "https://docs.rs/codegen/0.1.1")]
#![warn(rust_2018_idioms)]

//! Provides a builder API for generating Rust code.
//!
//! The general strategy for using the crate is as follows:
//!
//! 1. Create a `Scope` instance.
//! 2. Use the builder API to add elements to the scope.
//! 3. Call `Scope::to_string()` to get the generated code.
//!
//! For example:
//!
//! ```rust
//! use codegen::Scope;
//!
//! let mut scope = Scope::new();
//!
//! scope.new_struct("Foo")
//! .derive("Debug")
//! .field("one", "usize")
//! .field("two", "String");
//!
//! println!("{}", scope.to_string());
//! ```

mod associated_type;
mod block;
mod body;
mod bound;
mod docs;
mod field;
mod fields;
mod formatter;
mod function;
mod import;
mod item;
mod module;
mod scope;
mod type_def;
mod variant;

mod r#enum;
mod r#impl;
mod r#struct;
mod r#trait;
mod r#type;


pub use associated_type::*;
pub use block::*;
pub use field::*;
pub use formatter::*;
pub use function::*;
pub use import::*;
pub use module::*;
pub use scope::*;
pub use variant::*;

pub use r#enum::*;
pub use r#impl::*;
pub use r#struct::*;
pub use r#trait::*;
pub use r#type::*;
#![deny(missing_debug_implementations)]
//#![deny(missing_debug_implementations, missing_docs)]
#![doc(html_root_url = "https://docs.rs/codegen/0.1.1")]
#![warn(rust_2018_idioms)]

//! Provides a builder API for generating Rust code.
//!
//! The general strategy for using the crate is as follows:
//!
//! 1. Create a `Scope` instance.
//! 2. Use the builder API to add elements to the scope.
//! 3. Call `Scope::to_string()` to get the generated code.
//!
//! For example:
//!
//! ```rust
//! use codegen::Scope;
//!
//! let mut scope = Scope::new();
//!
//! scope.new_struct("Foo")
//! .derive("Debug")
//! .field("one", "usize")
//! .field("two", "String");
//!
//! println!("{}", scope.to_string());
//! ```

mod associated_type;
mod block;
mod body;
mod bound;
mod docs;
mod field;
mod fields;
mod formatter;
mod function;
mod import;
mod item;
mod module;
mod scope;
mod type_def;
mod variant;

mod r#enum;
mod r#impl;
mod r#struct;
mod r#trait;
mod r#type;


pub use associated_type::*;
pub use block::*;
pub use field::*;
pub use formatter::*;
pub use function::*;
pub use import::*;
pub use item::*;
pub use module::*;
pub use scope::*;
pub use variant::*;

pub use r#enum::*;
pub use r#impl::*;
pub use r#struct::*;
pub use r#trait::*;
pub use r#type::*;
Loading