Skip to content

Instantly share code, notes, and snippets.

@Jared-Sprague
Last active July 28, 2022 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jared-Sprague/aa22d9253dcec35347fd27ee93f64eca to your computer and use it in GitHub Desktop.
Save Jared-Sprague/aa22d9253dcec35347fd27ee93f64eca to your computer and use it in GitHub Desktop.
How to push to Vec inside closure
use std::{
path::Path,
process::{Command, Stdio},
};
use async_trait::async_trait;
use downloader::download_files;
use downloader::Asset;
use extract::{ExtractInput, Extractor};
use libs::owo_colors::{colors, OwoColorize};
use libs::rayon::prelude::*;
use libs::wax::{Glob, WalkEntry, WalkError};
use regex::Regex;
use std::fs;
...
let glob = Glob::new("**/docs_assets_pv1/**/*.html").unwrap();
let files: Vec<Result<WalkEntry, WalkError>> = glob.walk(args.outdir).collect();
let webassets_regex =
Regex::new(r#"(?P<weba>webassets/avalon/d/.*?)["'\)\]\s]"#).unwrap();
let src_prefix = "https://access.redhat.com/";
let dest_prefix = format!("{}/static/", args.outdir);
let assets: Vec<Asset> = vec![];
// Parallel interate over all the docs files and find file link matches e.g. webassets/avalon/d/*
files.into_par_iter().for_each(move |entry| {
let entry = entry.unwrap();
// Read the file
let content = fs::read_to_string(entry.path())
.expect("Something went wrong reading the file");
for capture in webassets_regex.captures_iter(content.as_str()) {
let src = format!("{}{}", src_prefix, &capture["weba"]);
let dest = format!("{}{}", dest_prefix, &capture["weba"]);
let asset = Asset { src, dest };
assets.push(asset);
// println!("src: {}", src);
// println!("dest: {}", dest);
}
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment