Skip to content

Commit

Permalink
Run Clippy on docs builder (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda-fairy authored Nov 11, 2021
1 parent 982c9f4 commit 12cec48
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ jobs:
toolchain: nightly
profile: minimal
override: true
components: clippy

- name: Build documentation
run: cd docs && make -j$(nproc)

- name: Check Clippy
run: cd docs && cargo clippy --all-targets -- -D warnings

examples:
name: Examples
runs-on: ubuntu-latest
Expand Down
7 changes: 3 additions & 4 deletions docs/src/bin/build_nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ use docs::{
page::{Page, COMRAK_OPTIONS},
string_writer::StringWriter,
};
use serde_json;
use std::{env, error::Error, fs, io, path::Path, str};

fn main() -> Result<(), Box<dyn Error>> {
let args = env::args().collect::<Vec<_>>();
if args.len() < 2 || !args[2..].iter().all(|arg| arg.contains(":")) {
if args.len() < 2 || !args[2..].iter().all(|arg| arg.contains(':')) {
return Err("invalid arguments".into());
}
let entries = args[2..]
.iter()
.map(|arg| {
let mut splits = arg.splitn(2, ":");
let mut splits = arg.splitn(2, ':');
let slug = splits.next().unwrap();
let input_path = splits.next().unwrap();
(slug, input_path)
Expand All @@ -35,7 +34,7 @@ fn build_nav(entries: &[(&str, &str)], nav_path: &str) -> Result<(), Box<dyn Err
.collect::<io::Result<Vec<_>>>()?;

// Only write if different to avoid spurious rebuilds
let old_string = fs::read_to_string(nav_path).unwrap_or(String::new());
let old_string = fs::read_to_string(nav_path).unwrap_or_default();
let new_string = serde_json::to_string_pretty(&nav)?;
if old_string != new_string {
fs::create_dir_all(Path::new(nav_path).parent().unwrap())?;
Expand Down
12 changes: 5 additions & 7 deletions docs/src/bin/build_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ use docs::{
page::{Page, COMRAK_OPTIONS},
views,
};
use serde_json;
use std::{
env,
error::Error,
fs::{self, File},
io::BufReader,
mem,
path::Path,
str::{self, Utf8Error},
string::FromUtf8Error,
Expand Down Expand Up @@ -90,7 +88,7 @@ fn rewrite_md_links<'a>(root: &'a AstNode<'a>) -> Result<(), FromUtf8Error> {
for node in root.descendants() {
let mut data = node.data.borrow_mut();
if let NodeValue::Link(NodeLink { url, .. }) = &mut data.value {
let mut url_string = String::from_utf8(mem::replace(url, Vec::new()))?;
let mut url_string = String::from_utf8(std::mem::take(url))?;
if url_string.ends_with(".md") {
url_string.truncate(url_string.len() - ".md".len());
url_string.push_str(".html");
Expand All @@ -117,7 +115,7 @@ fn strip_hidden_code<'a>(root: &'a AstNode<'a>) -> Result<(), Box<dyn Error>> {

fn strip_hidden_code_inner(literal: &str) -> String {
let lines = literal
.split("\n")
.split('\n')
.filter(|line| {
let line = line.trim();
line != "#" && !line.starts_with("# ")
Expand All @@ -142,10 +140,10 @@ fn highlight_code<'a>(root: &'a AstNode<'a>) -> Result<(), Box<dyn Error>> {
let info = parse_code_block_info(info)?;
let syntax = info
.into_iter()
.filter_map(|token| ss.find_syntax_by_token(&token))
.filter_map(|token| ss.find_syntax_by_token(token))
.next()
.unwrap_or_else(|| ss.find_syntax_plain_text());
let mut literal = String::from_utf8(mem::replace(literal, Vec::new()))?;
let mut literal = String::from_utf8(std::mem::take(literal))?;
if !literal.ends_with('\n') {
// Syntect expects a trailing newline
literal.push('\n');
Expand All @@ -160,5 +158,5 @@ fn highlight_code<'a>(root: &'a AstNode<'a>) -> Result<(), Box<dyn Error>> {
}

fn parse_code_block_info(info: &[u8]) -> Result<Vec<&str>, Utf8Error> {
str::from_utf8(info).map(|info| info.split(",").map(str::trim).collect())
str::from_utf8(info).map(|info| info.split(',').map(str::trim).collect())
}

0 comments on commit 12cec48

Please sign in to comment.