aboutsummaryrefslogtreecommitdiff
path: root/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib.rs')
-rw-r--r--lib.rs91
1 files changed, 44 insertions, 47 deletions
diff --git a/lib.rs b/lib.rs
index 5ce0b50..b30ebe7 100644
--- a/lib.rs
+++ b/lib.rs
@@ -83,7 +83,6 @@ optional = true
/**
This comments goes on top
* **`foo`** *(enabled by default)* — The foo feature enables the `foo` functions
-
* **`bar`** — The bar feature enables the bar module
#### Experimental features
@@ -94,9 +93,7 @@ The following features are experimental
#### Optional dependencies
* **`genial`** — Enable this feature to implement the trait for the types from the genial crate
-
* **`awesome`** — This awesome dependency is specified in its own table
-
*/
)]
/*!
@@ -280,7 +277,7 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result<String, String> {
.map(str::trim)
// and skip empty lines and comments that are not docs comments
.filter(|l| {
- !l.is_empty() && (!l.starts_with("#") || l.starts_with("##") || l.starts_with("#!"))
+ !l.is_empty() && (!l.starts_with('#') || l.starts_with("##") || l.starts_with("#!"))
});
let mut top_comment = String::new();
let mut current_comment = String::new();
@@ -289,26 +286,29 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result<String, String> {
let mut current_table = "";
while let Some(line) = lines.next() {
if let Some(x) = line.strip_prefix("#!") {
- if !x.is_empty() && !x.starts_with(" ") {
+ if !x.is_empty() && !x.starts_with(' ') {
continue; // it's not a doc comment
}
if !current_comment.is_empty() {
return Err("Cannot mix ## and #! comments between features.".into());
}
+ if top_comment.is_empty() && !features.is_empty() {
+ top_comment = "\n".into();
+ }
writeln!(top_comment, "{}", x).unwrap();
} else if let Some(x) = line.strip_prefix("##") {
- if !x.is_empty() && !x.starts_with(" ") {
+ if !x.is_empty() && !x.starts_with(' ') {
continue; // it's not a doc comment
}
writeln!(current_comment, " {}", x).unwrap();
- } else if let Some(table) = line.strip_prefix("[") {
+ } else if let Some(table) = line.strip_prefix('[') {
current_table = table
- .split_once("]")
+ .split_once(']')
.map(|(t, _)| t.trim())
.ok_or_else(|| format!("Parse error while parsing line: {}", line))?;
if !current_comment.is_empty() {
let dep = current_table
- .rsplit_once(".")
+ .rsplit_once('.')
.and_then(|(table, dep)| table.trim().ends_with("dependencies").then(|| dep))
.ok_or_else(|| format!("Not a feature: `{}`", line))?;
features.push((
@@ -317,17 +317,17 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result<String, String> {
std::mem::take(&mut current_comment),
));
}
- } else if let Some((dep, rest)) = line.split_once("=") {
+ } else if let Some((dep, rest)) = line.split_once('=') {
let dep = dep.trim().trim_matches('"');
let rest = get_balanced(rest, &mut lines)
.map_err(|e| format!("Parse error while parsing value {}: {}", dep, e))?;
if current_table == "features" && dep == "default" {
let defaults = rest
.trim()
- .strip_prefix("[")
- .and_then(|r| r.strip_suffix("]"))
+ .strip_prefix('[')
+ .and_then(|r| r.strip_suffix(']'))
.ok_or_else(|| format!("Parse error while parsing dependency {}", dep))?
- .split(",")
+ .split(',')
.map(|d| d.trim().trim_matches(|c| c == '"' || c == '\'').trim().to_string())
.filter(|d| !d.is_empty());
default_features.extend(defaults);
@@ -336,15 +336,15 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result<String, String> {
if current_table.ends_with("dependencies") {
if !rest
.split_once("optional")
- .and_then(|(_, r)| r.trim().strip_prefix("="))
+ .and_then(|(_, r)| r.trim().strip_prefix('='))
.map_or(false, |r| r.trim().starts_with("true"))
{
return Err(format!("Dependency {} is not an optional dependency", dep));
}
} else if current_table != "features" {
return Err(format!(
- "Comment cannot be associated with a feature:{}",
- current_comment
+ r#"Comment cannot be associated with a feature: "{}""#,
+ current_comment.trim()
));
}
features.push((
@@ -359,7 +359,7 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result<String, String> {
return Err("Found comment not associated with a feature".into());
}
if features.is_empty() {
- return Err("Could not find documented features in Cargo.toml".into());
+ return Ok("*No documented features in Cargo.toml*".into());
}
let mut result = String::new();
for (f, top, comment) in features {
@@ -372,25 +372,18 @@ fn process_toml(cargo_toml: &str, args: &Args) -> Result<String, String> {
top,
feature_label.replace("{feature}", f),
default,
- comment
+ comment.trim_end(),
)
.unwrap();
} else {
- writeln!(result, "{}* **`{}`**{} —{}", top, f, default, comment).unwrap();
+ writeln!(result, "{}* **`{}`**{} —{}", top, f, default, comment.trim_end())
+ .unwrap();
}
- } else {
- if let Some(feature_label) = &args.feature_label {
- writeln!(
- result,
- "{}* {}{}\n",
- top,
- feature_label.replace("{feature}", f),
- default,
- )
+ } else if let Some(feature_label) = &args.feature_label {
+ writeln!(result, "{}* {}{}", top, feature_label.replace("{feature}", f), default,)
.unwrap();
- } else {
- writeln!(result, "{}* **`{}`**{}\n", top, f, default).unwrap();
- }
+ } else {
+ writeln!(result, "{}* **`{}`**{}", top, f, default).unwrap();
}
}
result += &top_comment;
@@ -408,7 +401,7 @@ fn get_balanced<'a>(
let mut level = 0;
loop {
let mut last_slash = false;
- for (idx, b) in line.as_bytes().into_iter().enumerate() {
+ for (idx, b) in line.as_bytes().iter().enumerate() {
if last_slash {
last_slash = false
} else if in_quote {
@@ -590,26 +583,30 @@ default = ["feat1", "something_else"]
}
#[test]
- fn parse_error1() {
- test_error(
+ fn no_features() {
+ let r = process_toml(
r#"
[features]
[dependencies]
foo = 4;
"#,
- "Could not find documented features",
- );
+ &Args::default(),
+ )
+ .unwrap();
+ assert_eq!(r, "*No documented features in Cargo.toml*");
}
#[test]
- fn parse_error2() {
- test_error(
+ fn no_features2() {
+ let r = process_toml(
r#"
[packages]
[dependencies]
"#,
- "Could not find documented features",
- );
+ &Args::default(),
+ )
+ .unwrap();
+ assert_eq!(r, "*No documented features in Cargo.toml*");
}
#[test]
@@ -698,7 +695,7 @@ bar = []
## hallo
foo = []
"#,
- "Comment cannot be associated with a feature: hallo",
+ "Comment cannot be associated with a feature: \"hallo\"",
);
}
@@ -782,7 +779,7 @@ version = "42"
optional = true
"#;
let parsed = process_toml(toml, &Args::default()).unwrap();
- assert_eq!(parsed, " top\n* **`dep1`** — dep1\n\n yo\n* **`dep3`** — dep3\n\n");
+ assert_eq!(parsed, " top\n* **`dep1`** — dep1\n\n yo\n* **`dep3`** — dep3\n");
let parsed = process_toml(
toml,
&Args {
@@ -792,7 +789,7 @@ optional = true
},
)
.unwrap();
- assert_eq!(parsed, " top\n* <span class=\"stab portability\"><code>dep1</code></span> — dep1\n\n yo\n* <span class=\"stab portability\"><code>dep3</code></span> — dep3\n\n");
+ assert_eq!(parsed, " top\n* <span class=\"stab portability\"><code>dep1</code></span> — dep1\n\n yo\n* <span class=\"stab portability\"><code>dep3</code></span> — dep3\n");
}
#[test]
@@ -830,7 +827,7 @@ bar = [
let parsed = process_toml(toml, &Args::default()).unwrap();
assert_eq!(
parsed,
- "* **`dep1`** — dep1\n\n* **`foo`** — foo\n\n* **`bar`** *(enabled by default)* — bar\n\n"
+ "* **`dep1`** — dep1\n* **`foo`** — foo\n* **`bar`** *(enabled by default)* — bar\n"
);
let parsed = process_toml(
toml,
@@ -843,7 +840,7 @@ bar = [
.unwrap();
assert_eq!(
parsed,
- "* <span class=\"stab portability\"><code>dep1</code></span> — dep1\n\n* <span class=\"stab portability\"><code>foo</code></span> — foo\n\n* <span class=\"stab portability\"><code>bar</code></span> *(enabled by default)* — bar\n\n"
+ "* <span class=\"stab portability\"><code>dep1</code></span> — dep1\n* <span class=\"stab portability\"><code>foo</code></span> — foo\n* <span class=\"stab portability\"><code>bar</code></span> *(enabled by default)* — bar\n"
);
}
@@ -861,7 +858,7 @@ default = ["teßt."]
let parsed = process_toml(toml, &Args::default()).unwrap();
assert_eq!(
parsed,
- "* **`teßt.`** *(enabled by default)* — This is a test\n\n* **`dep`** — A dep\n\n"
+ "* **`teßt.`** *(enabled by default)* — This is a test\n* **`dep`** — A dep\n"
);
let parsed = process_toml(
toml,
@@ -874,7 +871,7 @@ default = ["teßt."]
.unwrap();
assert_eq!(
parsed,
- "* <span class=\"stab portability\"><code>teßt.</code></span> *(enabled by default)* — This is a test\n\n* <span class=\"stab portability\"><code>dep</code></span> — A dep\n\n"
+ "* <span class=\"stab portability\"><code>teßt.</code></span> *(enabled by default)* — This is a test\n* <span class=\"stab portability\"><code>dep</code></span> — A dep\n"
);
}
}