Skip to content

Commit

Permalink
Fmt (#94)
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
  • Loading branch information
ggwpez authored Apr 9, 2024
1 parent 4158cc6 commit c45dc78
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/autofix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ impl AutoFixer {
pub fn from_manifest<P: AsRef<Path>>(manifest: P) -> Result<Self, String> {
let raw = std::fs::read_to_string(&manifest)
.map_err(|e| format!("Failed to read manifest: {e}"))?;
let doc = raw.parse::<DocumentMut>().map_err(|e| format!("Failed to parse manifest: {e}"))?;
let doc = raw
.parse::<DocumentMut>()
.map_err(|e| format!("Failed to parse manifest: {e}"))?;
Ok(Self { raw, manifest: Some(manifest.as_ref().to_path_buf()), doc: Some(doc) })
}

pub fn from_raw(raw: &str) -> Result<Self, String> {
let doc = raw.parse::<DocumentMut>().map_err(|e| format!("Failed to parse manifest: {e}"))?;
let doc = raw
.parse::<DocumentMut>()
.map_err(|e| format!("Failed to parse manifest: {e}"))?;
Ok(Self { raw: raw.into(), manifest: None, doc: Some(doc) })
}

Expand Down
45 changes: 27 additions & 18 deletions src/cmd/lint/nostd.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Oliver Tale-Yazdi <[email protected]>

use std::collections::BTreeMap;
use crate::cmd::{lint::AutoFixer, CargoArgs};
use crate::cmd::GlobalArgs;
use crate::{
cmd::{lint::AutoFixer, resolve_dep, CargoArgs, GlobalArgs},
grammar::plural,
log,
};
use cargo_metadata::{DependencyKind, Package};
use crate::cmd::resolve_dep;
use crate::grammar::plural;
use std::collections::btree_map::Entry;
use std::fs::canonicalize;
use crate::log;
use std::{
collections::{btree_map::Entry, BTreeMap},
fs::canonicalize,
};

#[derive(Debug, clap::Parser)]
pub struct NoStdCmd {
Expand Down Expand Up @@ -64,9 +65,7 @@ impl DefaultFeaturesDisabledCmd {
continue;
}

let Some(rhs) = resolve_dep(lhs, dep, &meta) else {
continue
};
let Some(rhs) = resolve_dep(lhs, dep, &meta) else { continue };

if !Self::supports_nostd(&rhs.pkg, &mut cache)? {
continue;
Expand All @@ -76,12 +75,16 @@ impl DefaultFeaturesDisabledCmd {
continue;
}

println!("Default features not disabled for dependency: {} -> {}", lhs.name, rhs.pkg.name);

println!(
"Default features not disabled for dependency: {} -> {}",
lhs.name, rhs.pkg.name
);

let fixer = match autofixer.entry(lhs.manifest_path.clone()) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
let krate_path = canonicalize(lhs.manifest_path.clone().into_std_path_buf()).unwrap();
let krate_path =
canonicalize(lhs.manifest_path.clone().into_std_path_buf()).unwrap();

if !krate_path.starts_with(&allowed_dir) {
return Err(format!("Cannot write to path: {}", krate_path.display()))
Expand All @@ -96,7 +99,7 @@ impl DefaultFeaturesDisabledCmd {
}

let s = plural(autofixer.len());
print!("Found {} issue{} in {} crate{s} ", issues, plural(issues), autofixer.len());
print!("Found {} issue{} in {} crate{s} ", issues, plural(issues), autofixer.len());
if self.fix {
for (_, fixer) in autofixer.iter_mut() {
fixer.save()?;
Expand All @@ -116,15 +119,21 @@ impl DefaultFeaturesDisabledCmd {
}

// try to find the lib.rs
let krate_root = krate.manifest_path.parent().ok_or_else(|| format!("Could not find parent of manifest: {}", krate.manifest_path))?;
let krate_root = krate
.manifest_path
.parent()
.ok_or_else(|| format!("Could not find parent of manifest: {}", krate.manifest_path))?;
let lib_rs = krate_root.join("src/lib.rs");

if !lib_rs.exists() {
return Ok(false)
}
let content = std::fs::read_to_string(&lib_rs).map_err(|e| format!("Could not read lib.rs: {}", e))?;
let content = std::fs::read_to_string(&lib_rs)
.map_err(|e| format!("Could not read lib.rs: {}", e))?;

let ret = if content.contains("#![cfg_attr(not(feature = \"std\"), no_std)]") || content.contains("#![no_std]") {
let ret = if content.contains("#![cfg_attr(not(feature = \"std\"), no_std)]") ||
content.contains("#![no_std]")
{
log::debug!("Crate supports no-std: {} (path={})", krate.name, krate.manifest_path);
true
} else {
Expand Down

0 comments on commit c45dc78

Please sign in to comment.