Fix needless_borrow warnings

This commit is contained in:
Sosthène Guédon 2022-11-14 21:42:04 +01:00 committed by Vincent Prouillet
parent 02b57b942f
commit 3430d948f7
12 changed files with 27 additions and 26 deletions

View File

@ -57,7 +57,7 @@ impl FileInfo {
let name = path.file_stem().unwrap().to_string_lossy().to_string(); let name = path.file_stem().unwrap().to_string_lossy().to_string();
let canonical = parent.join(&name); let canonical = parent.join(&name);
let mut components = let mut components =
find_content_components(&file_path.strip_prefix(base_path).unwrap_or(&file_path)); find_content_components(file_path.strip_prefix(base_path).unwrap_or(&file_path));
let relative = if !components.is_empty() { let relative = if !components.is_empty() {
format!("{}/{}.md", components.join("/"), name) format!("{}/{}.md", components.join("/"), name)
} else { } else {
@ -91,7 +91,7 @@ impl FileInfo {
let parent = path.parent().expect("Get parent of section").to_path_buf(); let parent = path.parent().expect("Get parent of section").to_path_buf();
let name = path.file_stem().unwrap().to_string_lossy().to_string(); let name = path.file_stem().unwrap().to_string_lossy().to_string();
let components = let components =
find_content_components(&file_path.strip_prefix(base_path).unwrap_or(&file_path)); find_content_components(file_path.strip_prefix(base_path).unwrap_or(&file_path));
let relative = if !components.is_empty() { let relative = if !components.is_empty() {
format!("{}/{}.md", components.join("/"), name) format!("{}/{}.md", components.join("/"), name)
} else { } else {

View File

@ -295,7 +295,7 @@ impl Library {
for (path, page) in self.pages.iter_mut() { for (path, page) in self.pages.iter_mut() {
let parent_filename = &index_filename_by_lang[&page.lang]; let parent_filename = &index_filename_by_lang[&page.lang];
add_translation(&page.file.canonical, path); add_translation(&page.file.canonical, path);
let mut parent_section_path = page.file.parent.join(&parent_filename); let mut parent_section_path = page.file.parent.join(parent_filename);
while let Some(parent_section) = self.sections.get_mut(&parent_section_path) { while let Some(parent_section) = self.sections.get_mut(&parent_section_path) {
let is_transparent = parent_section.meta.transparent; let is_transparent = parent_section.meta.transparent;
@ -323,7 +323,7 @@ impl Library {
// We've added `_index(.{LANG})?.md` so if we are here so we need to go up twice // We've added `_index(.{LANG})?.md` so if we are here so we need to go up twice
match parent_section_path.clone().parent().unwrap().parent() { match parent_section_path.clone().parent().unwrap().parent() {
Some(parent) => parent_section_path = parent.join(&parent_filename), Some(parent) => parent_section_path = parent.join(parent_filename),
None => break, None => break,
} }
} }

View File

@ -31,7 +31,9 @@ static RFC3339_DATE: Lazy<Regex> = Lazy::new(|| {
).unwrap() ).unwrap()
}); });
static FOOTNOTES_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"<sup class="footnote-reference"><a href=\s*.*?>\s*.*?</a></sup>"#).unwrap()); static FOOTNOTES_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"<sup class="footnote-reference"><a href=\s*.*?>\s*.*?</a></sup>"#).unwrap()
});
#[derive(Clone, Debug, Default, PartialEq, Eq)] #[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Page { pub struct Page {
@ -260,7 +262,7 @@ impl Page {
fn serialize_assets(&self, base_path: &Path) -> Vec<String> { fn serialize_assets(&self, base_path: &Path) -> Vec<String> {
self.assets self.assets
.iter() .iter()
.filter_map(|asset| asset.strip_prefix(&self.file.path.parent().unwrap()).ok()) .filter_map(|asset| asset.strip_prefix(self.file.path.parent().unwrap()).ok())
.filter_map(|filename| filename.to_str()) .filter_map(|filename| filename.to_str())
.map(|filename| { .map(|filename| {
let mut path = self.file.path.clone(); let mut path = self.file.path.clone();

View File

@ -205,7 +205,7 @@ impl<'a> Paginator<'a> {
} else { } else {
paginator.insert("next", Value::Null); paginator.insert("next", Value::Null);
} }
paginator.insert("number_pagers", to_value(&self.pagers.len()).unwrap()); paginator.insert("number_pagers", to_value(self.pagers.len()).unwrap());
let base_url = if self.paginate_path.is_empty() { let base_url = if self.paginate_path.is_empty() {
self.permalink.to_string() self.permalink.to_string()
} else { } else {

View File

@ -198,7 +198,7 @@ impl Section {
fn serialize_assets(&self) -> Vec<String> { fn serialize_assets(&self) -> Vec<String> {
self.assets self.assets
.iter() .iter()
.filter_map(|asset| asset.strip_prefix(&self.file.path.parent().unwrap()).ok()) .filter_map(|asset| asset.strip_prefix(self.file.path.parent().unwrap()).ok())
.filter_map(|filename| filename.to_str()) .filter_map(|filename| filename.to_str())
.map(|filename| format!("{}{}", self.path, filename)) .map(|filename| format!("{}{}", self.path, filename))
.collect() .collect()

View File

@ -605,7 +605,7 @@ pub fn read_image_metadata<P: AsRef<Path>>(path: P) -> Result<ImageMetaResponse>
match ext.as_str() { match ext.as_str() {
"svg" => { "svg" => {
let img = SvgMetadata::parse_file(&path).with_context(err_context)?; let img = SvgMetadata::parse_file(path).with_context(err_context)?;
match (img.height(), img.width(), img.view_box()) { match (img.height(), img.width(), img.view_box()) {
(Some(h), Some(w), _) => Ok((h, w)), (Some(h), Some(w), _) => Ok((h, w)),
(_, _, Some(view_box)) => Ok((view_box.height, view_box.width)), (_, _, Some(view_box)) => Ok((view_box.height, view_box.width)),

View File

@ -218,7 +218,7 @@ impl Site {
// if we are processing a section we have to collect // if we are processing a section we have to collect
// index files for all languages and process them simultaneously // index files for all languages and process them simultaneously
// before any of the pages // before any of the pages
let index_files = WalkDir::new(&path) let index_files = WalkDir::new(path)
.follow_links(true) .follow_links(true)
.max_depth(1) .max_depth(1)
.into_iter() .into_iter()
@ -664,7 +664,7 @@ impl Site {
asset_path, asset_path,
&current_path.join( &current_path.join(
asset_path asset_path
.strip_prefix(&page.file.path.parent().unwrap()) .strip_prefix(page.file.path.parent().unwrap())
.expect("Couldn't get filename from page asset"), .expect("Couldn't get filename from page asset"),
), ),
)?; )?;
@ -1071,7 +1071,7 @@ impl Site {
asset_path, asset_path,
&output_path.join( &output_path.join(
asset_path asset_path
.strip_prefix(&section.file.path.parent().unwrap()) .strip_prefix(section.file.path.parent().unwrap())
.expect("Failed to get asset filename for section"), .expect("Failed to get asset filename for section"),
), ),
)?; )?;

View File

@ -51,12 +51,12 @@ fn compile_sass_glob(
for file in files { for file in files {
let css = compile_file(&file, options.clone()).map_err(|e| anyhow!(e))?; let css = compile_file(&file, options.clone()).map_err(|e| anyhow!(e))?;
let path_inside_sass = file.strip_prefix(&sass_path).unwrap(); let path_inside_sass = file.strip_prefix(sass_path).unwrap();
let parent_inside_sass = path_inside_sass.parent(); let parent_inside_sass = path_inside_sass.parent();
let css_output_path = output_path.join(path_inside_sass).with_extension("css"); let css_output_path = output_path.join(path_inside_sass).with_extension("css");
if parent_inside_sass.is_some() { if parent_inside_sass.is_some() {
create_dir_all(&css_output_path.parent().unwrap())?; create_dir_all(css_output_path.parent().unwrap())?;
} }
create_file(&css_output_path, &css)?; create_file(&css_output_path, &css)?;

View File

@ -62,7 +62,7 @@ pub fn load_tera(path: &Path, config: &Config) -> Result<Tera> {
if let Some(ref theme) = config.theme { if let Some(ref theme) = config.theme {
// Test that the templates folder exist for that theme // Test that the templates folder exist for that theme
let theme_path = path.join("themes").join(&theme); let theme_path = path.join("themes").join(theme);
if !theme_path.join("templates").exists() { if !theme_path.join("templates").exists() {
bail!("Theme `{}` is missing a templates folder", theme); bail!("Theme `{}` is missing a templates folder", theme);
} }

View File

@ -21,7 +21,7 @@ pub fn is_path_in_directory(parent: &Path, path: &Path) -> Result<bool> {
/// Create a file with the content given /// Create a file with the content given
pub fn create_file(path: &Path, content: &str) -> Result<()> { pub fn create_file(path: &Path, content: &str) -> Result<()> {
let mut file = let mut file =
File::create(&path).with_context(|| format!("Failed to create file {}", path.display()))?; File::create(path).with_context(|| format!("Failed to create file {}", path.display()))?;
file.write_all(content.as_bytes())?; file.write_all(content.as_bytes())?;
Ok(()) Ok(())
} }
@ -92,19 +92,19 @@ pub fn copy_file_if_needed(src: &Path, dest: &Path, hard_link: bool) -> Result<(
.with_context(|| format!("Failed to get metadata of {}", src.display()))?; .with_context(|| format!("Failed to get metadata of {}", src.display()))?;
let src_mtime = FileTime::from_last_modification_time(&src_metadata); let src_mtime = FileTime::from_last_modification_time(&src_metadata);
if Path::new(&dest).is_file() { if Path::new(&dest).is_file() {
let target_metadata = metadata(&dest)?; let target_metadata = metadata(dest)?;
let target_mtime = FileTime::from_last_modification_time(&target_metadata); let target_mtime = FileTime::from_last_modification_time(&target_metadata);
if !(src_mtime == target_mtime && src_metadata.len() == target_metadata.len()) { if !(src_mtime == target_mtime && src_metadata.len() == target_metadata.len()) {
copy(src, &dest).with_context(|| { copy(src, dest).with_context(|| {
format!("Was not able to copy file {} to {}", src.display(), dest.display()) format!("Was not able to copy file {} to {}", src.display(), dest.display())
})?; })?;
set_file_mtime(&dest, src_mtime)?; set_file_mtime(dest, src_mtime)?;
} }
} else { } else {
copy(src, &dest).with_context(|| { copy(src, dest).with_context(|| {
format!("Was not able to copy directory {} to {}", src.display(), dest.display()) format!("Was not able to copy directory {} to {}", src.display(), dest.display())
})?; })?;
set_file_mtime(&dest, src_mtime)?; set_file_mtime(dest, src_mtime)?;
} }
} }
Ok(()) Ok(())

View File

@ -318,13 +318,12 @@ pub fn serve(
Ok(a) => a, Ok(a) => a,
Err(_) => return Err(anyhow!("Invalid address: {}.", address)), Err(_) => return Err(anyhow!("Invalid address: {}.", address)),
}; };
if (TcpListener::bind(&bind_address)).is_err() { if (TcpListener::bind(bind_address)).is_err() {
return Err(anyhow!("Cannot start server on address {}.", address)); return Err(anyhow!("Cannot start server on address {}.", address));
} }
let config_path = PathBuf::from(config_file); let config_path = PathBuf::from(config_file);
let config_path_rel = let config_path_rel = diff_paths(&config_path, root_dir).unwrap_or_else(|| config_path.clone());
diff_paths(&config_path, &root_dir).unwrap_or_else(|| config_path.clone());
// An array of (path, WatchMode) where the path should be watched for changes, // An array of (path, WatchMode) where the path should be watched for changes,
// and the WatchMode value indicates whether this file/folder must exist for // and the WatchMode value indicates whether this file/folder must exist for

View File

@ -15,12 +15,12 @@ mod prompt;
fn get_config_file_path(dir: &Path, config_path: &Path) -> (PathBuf, PathBuf) { fn get_config_file_path(dir: &Path, config_path: &Path) -> (PathBuf, PathBuf) {
let root_dir = dir let root_dir = dir
.ancestors() .ancestors()
.find(|a| a.join(&config_path).exists()) .find(|a| a.join(config_path).exists())
.unwrap_or_else(|| panic!("could not find directory containing config file")); .unwrap_or_else(|| panic!("could not find directory containing config file"));
// if we got here we found root_dir so config file should exist so we can unwrap safely // if we got here we found root_dir so config file should exist so we can unwrap safely
let config_file = root_dir let config_file = root_dir
.join(&config_path) .join(config_path)
.canonicalize() .canonicalize()
.unwrap_or_else(|_| panic!("could not find directory containing config file")); .unwrap_or_else(|_| panic!("could not find directory containing config file"));
(root_dir.to_path_buf(), config_file) (root_dir.to_path_buf(), config_file)