Skip to content

Commit

Permalink
applied rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
livioribeiro committed Jun 18, 2017
1 parent 22da5f6 commit 9322903
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 134 deletions.
4 changes: 2 additions & 2 deletions src/doc/cargo_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ pub fn get_cargo_info(project_root: &Path) -> Result<Cargo, String> {

match toml::from_str(&buf) {
Err(e) => return Err(format!("{}", e)),
Ok(cargo) => Ok(cargo)
Ok(cargo) => Ok(cargo),
}
}
}
28 changes: 12 additions & 16 deletions src/doc/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ impl<R: Read> DocExtractor<R> {
fn extract_style_none(&mut self, line: String) -> Option<String> {
if line.starts_with("//!") {
self.style = DocStyle::SingleLine;
return Some(self.normalize_line(line))
return Some(self.normalize_line(line));
} else if line.starts_with("/*!") {
self.style = DocStyle::MultiLine;
let line = self.normalize_line(line);
if line.len() > 0 {
return Some(line)
return Some(line);
}
}
None
}

fn extract_style_single_line(&mut self, line: String) -> Option<String> {
if line.starts_with("//!") {
return Some(self.normalize_line(line))
return Some(self.normalize_line(line));
} else if line.starts_with("/*!") {
self.style = DocStyle::MultiLine;
let line = self.normalize_line(line);
if line.len() > 0 {
return Some(line)
return Some(line);
}
}
None
Expand All @@ -68,9 +68,9 @@ impl<R: Read> DocExtractor<R> {
self.style = DocStyle::NoDoc;
let ref_line = line.split_at(line.rfind("*/").unwrap()).0.trim_right();
if ref_line.len() == 0 {
return None
return None;
}
return Some(ref_line.to_owned())
return Some(ref_line.to_owned());
}
Some(line.trim_right().to_owned())
}
Expand All @@ -92,7 +92,7 @@ impl<R: Read> Iterator for DocExtractor<R> {
};

if bytes_read == 0 {
return None
return None;
}

result = match self.style {
Expand All @@ -112,8 +112,7 @@ mod tests {
use super::DocExtractor;
use super::DocModify;

const INPUT: &'static str =
r#"//! first line
const INPUT: &'static str = r#"//! first line
//! ```
//! let rust_code = "will show";
//! # let binding = "won't show";
Expand All @@ -136,8 +135,7 @@ use std::any::Any;
fn main() {}"#;

const INPUT_MULTILINE: &'static str =
r#"/*!
const INPUT_MULTILINE: &'static str = r#"/*!
first line
```
let rust_code = "will show";
Expand All @@ -162,8 +160,7 @@ use std::any::Any;
fn main() {}"#;

const EXPECT_INDENT_HEADING: &str =
r#"first line
const EXPECT_INDENT_HEADING: &str = r#"first line
```rust
let rust_code = "will show";
```
Expand All @@ -182,8 +179,7 @@ let should_panic = true;
int i = 0; // no rust code
```"#;

const EXPECT_NO_INDENT_HEADING: &str =
r#"first line
const EXPECT_NO_INDENT_HEADING: &str = r#"first line
```rust
let rust_code = "will show";
```
Expand Down Expand Up @@ -251,4 +247,4 @@ int i = 0; // no rust code

assert_eq!(doc_data, expected);
}
}
}
51 changes: 35 additions & 16 deletions src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ use self::modifier::DocModify;
pub use self::cargo_info::{Cargo, get_cargo_info};

/// Generates readme data from `source` file
pub fn generate_readme<T: Read>(project_root: &Path,
source: &mut T,
template: Option<&mut T>,
add_title: bool,
add_license: bool,
indent_headings: bool)
-> Result<String, String> {
pub fn generate_readme<T: Read>(
project_root: &Path,
source: &mut T,
template: Option<&mut T>,
add_title: bool,
add_license: bool,
indent_headings: bool,
) -> Result<String, String> {

// ceate doc extractor
let doc_iter = DocExtractor::new(source)
.modify_doc(indent_headings);
let doc_iter = DocExtractor::new(source).modify_doc(indent_headings);

let mut doc_data = Vec::new();
for line in doc_iter {
let line = line.map_err(|e| format!("{}", e))?;
doc_data.push(line);
}

// fold doc_data (Vec<String>) into single String
let readme = fold_doc_data(doc_data);

Expand All @@ -55,7 +55,12 @@ fn fold_doc_data(data: Vec<String>) -> String {
} else if data.len() < 2 {
data[0].to_owned()
} else {
data[1..].into_iter().fold(data[0].to_owned(), |acc, line| format!("{}\n{}", acc, line))
data[1..].into_iter().fold(
data[0].to_owned(),
|acc, line| {
format!("{}\n{}", acc, line)
},
)
}
}

Expand All @@ -70,22 +75,36 @@ fn get_template<T: Read>(template: &mut T) -> Result<String, String> {
Ok(template_string)
}

fn render(template: Option<String>, mut readme: String, cargo: Cargo, add_title: bool, add_license: bool) -> Result<String, String> {
fn render(
template: Option<String>,
mut readme: String,
cargo: Cargo,
add_title: bool,
add_license: bool,
) -> Result<String, String> {
let title = cargo.package.name.as_ref();
let license = cargo.package.license.as_ref();

match template {
Some(template) => {
if template.contains("{{license}}") && !add_license {
return Err("`{{license}}` was found in template but should not be rendered".to_owned());
return Err(
"`{{license}}` was found in template but should not be rendered".to_owned(),
);
}

if template.contains("{{crate}}") && !add_title {
return Err("`{{crate}}` was found in template but title should not be rendered".to_owned());
return Err(
"`{{crate}}` was found in template but title should not be rendered".to_owned(),
);
}

let title = if add_title { Some(title) } else { None };
let license = if add_license { Some(license.unwrap().as_ref()) } else { None };
let license = if add_license {
Some(license.unwrap().as_ref())
} else {
None
};
template::process_template(template, readme, title, license)
}
None => {
Expand Down Expand Up @@ -134,4 +153,4 @@ mod tests {

assert_eq!("# first line\nsecond line\nthird line", result);
}
}
}
15 changes: 11 additions & 4 deletions src/doc/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use regex::Regex;

pub trait DocModify<I: Iterator> {
fn modify_doc(self, indent_headings: bool) -> DocModifier<Self>
where Self: Sized + Iterator<Item=io::Result<String>>
where
Self: Sized + Iterator<Item = io::Result<String>>,
{
DocModifier::new(self, indent_headings)
}
Expand All @@ -26,7 +27,10 @@ pub struct DocModifier<I: Iterator> {
re_code_other: Regex,
}

impl<I> DocModifier<I> where I: Iterator<Item=io::Result<String>> {
impl<I> DocModifier<I>
where
I: Iterator<Item = io::Result<String>>,
{
pub fn new(iter: I, indent_headings: bool) -> Self {
// Is this code block rust?
let re_code_rust = Regex::new(r"^```(no_run|ignore|should_panic)?$").unwrap();
Expand All @@ -43,7 +47,10 @@ impl<I> DocModifier<I> where I: Iterator<Item=io::Result<String>> {
}
}

impl<I> Iterator for DocModifier<I> where I: Iterator<Item=io::Result<String>> {
impl<I> Iterator for DocModifier<I>
where
I: Iterator<Item = io::Result<String>>,
{
type Item = io::Result<String>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -76,4 +83,4 @@ impl<I> Iterator for DocModifier<I> where I: Iterator<Item=io::Result<String>> {

Some(Ok(line))
}
}
}
21 changes: 13 additions & 8 deletions src/doc/template.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/// Renders the template
///
/// This is not a real template engine, it just processes a few substitutions.
pub fn process_template(mut template: String,
mut readme: String,
title: Option<&str>,
license: Option<&str>)
-> Result<String, String> {
pub fn process_template(
mut template: String,
mut readme: String,
title: Option<&str>,
license: Option<&str>,
) -> Result<String, String> {

template = template.trim_right_matches("\n").to_owned();

Expand All @@ -14,11 +15,15 @@ pub fn process_template(mut template: String,
}

if template.contains("{{license}}") && license.is_none() {
return Err("`{{license}}` was found in template but no license was provided".to_owned());
return Err(
"`{{license}}` was found in template but no license was provided".to_owned(),
);
}

if template.contains("{{crate}}") && title.is_none() {
return Err("`{{crate}}` was found in template but no crate name was provided".to_owned());
return Err(
"`{{crate}}` was found in template but no crate name was provided".to_owned(),
);
}

if let Some(title) = title {
Expand Down Expand Up @@ -269,4 +274,4 @@ mod tests {
with_license => false,
panic => "`{{license}}` was found in template but no license was provided"
);
}
}
Loading

0 comments on commit 9322903

Please sign in to comment.