Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# <future version>

### Changed
- updated the crate to rust 2018 edition

# 0.1.3 (May 9, 2020)

### Added
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ documentation = "https://docs.rs/codegen/0.1.3/codegen"
homepage = "https://github.com/carllerche/codegen"
repository = "https://github.com/carllerche/codegen"
readme = "README.md"
edition = "2018"

[dependencies]
indexmap = "1.0.2"
7 changes: 2 additions & 5 deletions src/associated_type.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use bound::Bound;

use r#type::Type;

use crate::bound::Bound;
use crate::r#type::Type;

/// Defines an associated type.
#[derive(Debug, Clone)]
pub struct AssociatedType(pub Bound);


impl AssociatedType {
/// Add a bound to the associated type.
pub fn bound<T>(&mut self, ty: T) -> &mut Self
Expand Down
8 changes: 3 additions & 5 deletions src/block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::fmt::{self, Write};

use body::Body;
use formatter::Formatter;

use crate::body::Body;
use crate::formatter::Formatter;

/// Defines a code block. This is used to define a function body.
#[derive(Debug, Clone)]
Expand All @@ -12,7 +11,6 @@ pub struct Block {
body: Vec<Body>,
}


impl Block {
/// Returns an empty code block.
pub fn new(before: &str) -> Self {
Expand Down Expand Up @@ -45,7 +43,7 @@ impl Block {
}

/// Formats the block using the given formatter.
pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
if let Some(ref before) = self.before {
write!(fmt, "{}", before)?;
}
Expand Down
14 changes: 6 additions & 8 deletions src/body.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use std::fmt::{self, Write};

use block::Block;
use formatter::Formatter;

use crate::block::Block;
use crate::formatter::Formatter;

#[derive(Debug, Clone)]
pub enum Body {
String(String),
Block(Block),
}


impl Body {
pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match *self {
Body::String(ref s) => write!(fmt, "{}\n", s),
Body::Block(ref b) => b.fmt(fmt),
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
match &self {
Body::String(s) => write!(fmt, "{}\n", s),
Body::Block(b) => b.fmt(fmt),
}
}
}
3 changes: 1 addition & 2 deletions src/bound.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use r#type::Type;

use crate::r#type::Type;

#[derive(Debug, Clone)]
pub struct Bound {
Expand Down
6 changes: 2 additions & 4 deletions src/docs.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use std::fmt::{self, Write};

use formatter::Formatter;

use crate::formatter::Formatter;

#[derive(Debug, Clone)]
pub struct Docs {
docs: String,
}


impl Docs {
pub fn new(docs: &str) -> Self {
Docs {
docs: docs.to_string(),
}
}

pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
for line in self.docs.lines() {
write!(fmt, "/// {}\n", line)?;
}
Expand Down
12 changes: 5 additions & 7 deletions src/enum.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::fmt;

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

use r#type::Type;
use crate::formatter::Formatter;
use crate::type_def::TypeDef;
use crate::variant::Variant;

use crate::r#type::Type;

/// Defines an enumeration.
#[derive(Debug, Clone)]
Expand All @@ -14,7 +13,6 @@ pub struct Enum {
variants: Vec<Variant>,
}


impl Enum {
/// Return a enum definition with the provided name.
pub fn new(name: &str) -> Self {
Expand Down Expand Up @@ -87,7 +85,7 @@ impl Enum {
}

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

fmt.block(|fmt| {
Expand Down
7 changes: 3 additions & 4 deletions src/field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use r#type::Type;

use crate::r#type::Type;

/// Defines a struct field.
#[derive(Debug, Clone)]
Expand All @@ -17,11 +16,11 @@ pub struct Field {
pub annotation: Vec<String>,
}


impl Field {
/// Return a field definition with the provided name and type
pub fn new<T>(name: &str, ty: T) -> Self
where T: Into<Type>,
where
T: Into<Type>,
{
Field {
name: name.into(),
Expand Down
16 changes: 7 additions & 9 deletions src/fields.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::fmt::{self, Write};

use field::Field;
use formatter::Formatter;

use r#type::Type;
use crate::field::Field;
use crate::formatter::Formatter;

use crate::r#type::Type;

/// Defines a set of fields.
#[derive(Debug, Clone)]
Expand All @@ -14,10 +13,8 @@ pub enum Fields {
Named(Vec<Field>),
}


impl Fields {
pub fn push_named(&mut self, field: Field) -> &mut Self
{
pub fn push_named(&mut self, field: Field) -> &mut Self {
match *self {
Fields::Empty => {
*self = Fields::Named(vec![field]);
Expand All @@ -32,7 +29,8 @@ impl Fields {
}

pub fn named<T>(&mut self, name: &str, ty: T) -> &mut Self
where T: Into<Type>,
where
T: Into<Type>,
{
self.push_named(Field {
name: name.to_string(),
Expand All @@ -59,7 +57,7 @@ impl Fields {
self
}

pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
match *self {
Fields::Named(ref fields) => {
assert!(!fields.is_empty());
Expand Down
14 changes: 5 additions & 9 deletions src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::fmt::{self, Write};

use bound::Bound;

use r#type::Type;
use crate::bound::Bound;

use crate::r#type::Type;

const DEFAULT_INDENT: usize = 4;


/// Configures how a scope is formatted.
#[derive(Debug)]
pub struct Formatter<'a> {
Expand All @@ -21,7 +19,6 @@ pub struct Formatter<'a> {
indent: usize,
}


impl<'a> Formatter<'a> {
/// Return a new formatter that writes to the given string.
pub fn new(dst: &'a mut String) -> Self {
Expand Down Expand Up @@ -102,9 +99,8 @@ impl<'a> fmt::Write for Formatter<'a> {
}
}


/// Format generics.
pub fn fmt_generics(generics: &[String], fmt: &mut Formatter) -> fmt::Result {
pub fn fmt_generics(generics: &[String], fmt: &mut Formatter<'_>) -> fmt::Result {
if !generics.is_empty() {
write!(fmt, "<")?;

Expand All @@ -122,7 +118,7 @@ pub fn fmt_generics(generics: &[String], fmt: &mut Formatter) -> fmt::Result {
}

/// Format generic bounds.
pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter) -> fmt::Result {
pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter<'_>) -> fmt::Result {
if !bounds.is_empty() {
write!(fmt, "\n")?;

Expand All @@ -142,7 +138,7 @@ pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter) -> fmt::Result {
}

/// Format multiple generic bounds.
pub fn fmt_bound_rhs(tys: &[Type], fmt: &mut Formatter) -> fmt::Result {
pub fn fmt_bound_rhs(tys: &[Type], fmt: &mut Formatter<'_>) -> fmt::Result {
for (i, ty) in tys.iter().enumerate() {
if i != 0 {
write!(fmt, " + ")?
Expand Down
20 changes: 9 additions & 11 deletions src/function.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::fmt::{self, Write};

use block::Block;
use body::Body;
use bound::Bound;
use docs::Docs;
use field::Field;
use formatter::{fmt_bounds, fmt_generics};
use formatter::Formatter;

use r#type::Type;
use crate::block::Block;
use crate::body::Body;
use crate::bound::Bound;
use crate::docs::Docs;
use crate::field::Field;
use crate::formatter::Formatter;
use crate::formatter::{fmt_bounds, fmt_generics};

use crate::r#type::Type;

/// Defines a function.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -54,7 +53,6 @@ pub struct Function {
r#async: bool,
}


impl Function {
/// Return a new function definition.
pub fn new(name: &str) -> Self {
Expand Down Expand Up @@ -211,7 +209,7 @@ impl Function {
}

/// Formats the function using the given formatter.
pub fn fmt(&self, is_trait: bool, fmt: &mut Formatter) -> fmt::Result {
pub fn fmt(&self, is_trait: bool, fmt: &mut Formatter<'_>) -> fmt::Result {
if let Some(ref docs) = self.docs {
docs.fmt(fmt)?;
}
Expand Down
14 changes: 6 additions & 8 deletions src/impl.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::fmt::{self, Write};

use bound::Bound;
use field::Field;
use formatter::{Formatter, fmt_bounds, fmt_generics};
use function::Function;

use r#type::Type;
use crate::bound::Bound;
use crate::field::Field;
use crate::formatter::{fmt_bounds, fmt_generics, Formatter};
use crate::function::Function;

use crate::r#type::Type;

/// Defines an impl block.
#[derive(Debug, Clone)]
Expand All @@ -31,7 +30,6 @@ pub struct Impl {
macros: Vec<String>,
}


impl Impl {
/// Return a new impl definition
pub fn new<T>(target: T) -> Self
Expand Down Expand Up @@ -121,7 +119,7 @@ impl Impl {
}

/// Formats the impl block using the given formatter.
pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
for m in self.macros.iter() {
write!(fmt, "{}\n", m)?;
}
Expand Down
1 change: 0 additions & 1 deletion src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub struct Import {
pub vis: Option<String>,
}


impl Import {
/// Return a new import.
pub fn new(path: &str, ty: &str) -> Self {
Expand Down
13 changes: 6 additions & 7 deletions src/item.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use function::Function;
use module::Module;

use r#enum::Enum;
use r#impl::Impl;
use r#struct::Struct;
use r#trait::Trait;
use crate::function::Function;
use crate::module::Module;

use crate::r#enum::Enum;
use crate::r#impl::Impl;
use crate::r#struct::Struct;
use crate::r#trait::Trait;

#[derive(Debug, Clone)]
pub enum Item {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(warnings, missing_debug_implementations, missing_docs)]
#![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.
//!
Expand Down
Loading