Skip to content

Commit 98e7e89

Browse files
authored
Merge pull request #58 from mgeisler/rust-2018
Update to Rust 2018
2 parents 207d6df + 24dfaa3 commit 98e7e89

File tree

7 files changed

+36
-64
lines changed

7 files changed

+36
-64
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: rust
22

33
rust:
4-
- 1.27.2 # ~4 releases behind latest stable
4+
- 1.31.0 # Rust 2018
55
- stable
66
- beta
77
- nightly

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ keywords = ["version"]
1313
categories = ["development-tools", "rust-patterns"]
1414
license = "MIT"
1515
exclude = [".dir-locals.el"]
16+
edition = "2018"
1617

1718
[badges]
1819
travis-ci = { repository = "mgeisler/version-sync" }
1920
appveyor = { repository = "mgeisler/version-sync" }
2021
codecov = { repository = "mgeisler/version-sync" }
2122

2223
[dependencies]
23-
pulldown-cmark = { version = "0.2", default-features = false }
24+
pulldown-cmark = { version = "0.4", default-features = false }
2425
semver-parser = "0.9"
2526
syn = { version = "0.15", features = ["full"] }
26-
toml = "0.4"
27+
toml = "0.5"
2728
url = "1.5.1"
2829
itertools = "0.8"
2930
regex = "1.1"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ your_crate = "0.1.2"
111111

112112
This is a changelog describing the most important changes per release.
113113

114+
### Unreleased ###
115+
116+
We now use Rust 2018, which means we reqire Rust version 1.31.0 or
117+
later.
118+
114119
### Version 0.7.0 — January 14th, 2019
115120

116121
Special characters are now correctly escaped in the `{name}` and

src/contains_regex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use regex::{escape, Regex};
22

3-
use helpers::{read_file, Result};
3+
use crate::helpers::{read_file, Result};
44

55
/// Check that `path` contain the regular expression given by
66
/// `template`.

src/html_root_url.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use semver_parser::version::Version;
44
use syn;
55
use url::Url;
66

7-
use helpers::{indent, read_file, version_matches_request, Result};
7+
use crate::helpers::{indent, read_file, version_matches_request, Result};
88

99
fn url_matches(value: &str, pkg_name: &str, version: &Version) -> Result<()> {
1010
let url = Url::parse(value).map_err(|err| format!("parse error: {}", err))?;

src/lib.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,14 @@
5252
#![doc(html_root_url = "https://docs.rs/version-sync/0.7.0")]
5353
#![deny(missing_docs)]
5454

55-
extern crate itertools;
56-
extern crate pulldown_cmark;
57-
extern crate regex;
58-
extern crate semver_parser;
59-
extern crate syn;
60-
extern crate toml;
61-
extern crate url;
62-
6355
mod contains_regex;
6456
mod helpers;
6557
mod html_root_url;
6658
mod markdown_deps;
6759

68-
pub use contains_regex::check_contains_regex;
69-
pub use html_root_url::check_html_root_url;
70-
pub use markdown_deps::check_markdown_deps;
60+
pub use crate::contains_regex::check_contains_regex;
61+
pub use crate::html_root_url::check_html_root_url;
62+
pub use crate::markdown_deps::check_markdown_deps;
7163

7264
/// Assert that dependencies on the current package are up to date.
7365
///

src/markdown_deps.rs

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use semver_parser::range::VersionReq;
44
use semver_parser::version::parse as parse_version;
55
use toml::Value;
66

7-
use helpers::{indent, read_file, version_matches_request, Result};
7+
use crate::helpers::{indent, read_file, version_matches_request, Result};
88

99
/// A fenced code block.
1010
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -15,27 +15,6 @@ struct CodeBlock<'a> {
1515
first_line: usize,
1616
}
1717

18-
impl<'a> CodeBlock<'a> {
19-
/// Contruct a new code block from text[start..end]. This only
20-
/// works for fenced code blocks. The `start` index must be the
21-
/// first line of data in the code block, `end` must be right
22-
/// after the final newline of a fenced code block.
23-
fn new(text: &'a str, start: usize, end: usize) -> CodeBlock {
24-
// A code block with no closing fence is reported as being
25-
// closed at the end of the file. In that case, we cannot be
26-
// sure to find a final newline.
27-
let last_nl = match text[..end - 1].rfind('\n') {
28-
Some(i) => i + 1,
29-
None => start,
30-
};
31-
let first_line = 1 + text[..start].lines().count();
32-
CodeBlock {
33-
content: &text[start..last_nl],
34-
first_line: first_line,
35-
}
36-
}
37-
}
38-
3918
/// Extract a dependency on the given package from a TOML code block.
4019
fn extract_version_request(pkg_name: &str, block: &str) -> Result<VersionReq> {
4120
match block.parse::<Value>() {
@@ -79,24 +58,21 @@ fn is_toml_block(lang: &str) -> bool {
7958
}
8059

8160
/// Find all TOML code blocks in a Markdown text.
82-
fn find_toml_blocks(text: &str) -> Vec<CodeBlock> {
83-
let mut parser = Parser::new(text);
61+
fn find_toml_blocks(text: &str) -> Vec<CodeBlock<'_>> {
62+
let parser = Parser::new(text);
8463
let mut code_blocks = Vec::new();
85-
let mut start = 0;
86-
// A normal for-loop doesn't work since that would borrow the
87-
// parser mutably for the duration of the loop body, preventing us
88-
// from calling get_offset later.
89-
while let Some(event) = parser.next() {
64+
for (event, range) in parser.into_offset_iter() {
9065
match event {
91-
Event::Start(Tag::CodeBlock(_)) => {
92-
start = parser.get_offset();
93-
}
94-
Event::End(Tag::CodeBlock(lang)) => {
95-
// Only fenced code blocks have language information.
96-
if is_toml_block(&lang) {
97-
let end = parser.get_offset();
98-
code_blocks.push(CodeBlock::new(text, start, end));
99-
}
66+
Event::Start(Tag::CodeBlock(ref lang)) if is_toml_block(lang) => {
67+
let line_count = text[..range.start].lines().count();
68+
let code_block = &text[range];
69+
let start = 1 + code_block.find('\n').unwrap_or(0);
70+
let end = 1 + code_block.rfind('\n').unwrap_or(0);
71+
72+
code_blocks.push(CodeBlock {
73+
content: &code_block[start..end],
74+
first_line: 2 + line_count,
75+
});
10076
}
10177
_ => {}
10278
}
@@ -172,17 +148,17 @@ mod tests {
172148
use super::*;
173149

174150
#[test]
175-
fn empty_toml_block() {
151+
fn empty_markdown_file() {
176152
assert_eq!(find_toml_blocks(""), vec![]);
177153
}
178154

179155
#[test]
180-
fn indented_toml_block() {
156+
fn indented_code_block() {
181157
assert_eq!(find_toml_blocks(" code block\n"), vec![]);
182158
}
183159

184160
#[test]
185-
fn single_line_toml_block() {
161+
fn empty_toml_block() {
186162
assert_eq!(
187163
find_toml_blocks("```toml\n```"),
188164
vec![CodeBlock {
@@ -204,20 +180,18 @@ mod tests {
204180
}
205181

206182
#[test]
207-
fn code_block_new() {
183+
fn nonempty_toml_block() {
208184
let text = "Preceding text.\n\
209-
```\n\
185+
```toml\n\
210186
foo\n\
211187
```\n\
212188
Trailing text";
213-
let start = text.find("```\n").unwrap() + 4;
214-
let end = text.rfind("```\n").unwrap() + 4;
215189
assert_eq!(
216-
CodeBlock::new(text, start, end),
217-
CodeBlock {
190+
find_toml_blocks(&text),
191+
vec![CodeBlock {
218192
content: "foo\n",
219193
first_line: 3
220-
}
194+
}]
221195
);
222196
}
223197

0 commit comments

Comments
 (0)