Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions crates/sourceorigin-standard/src/rules/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use toml::Spanned;
use super::comments::Comment;
use super::fix::{Checked, Fix};
use super::level::Level;
use super::toml_file::TomlFile;
use super::{Error, Finding, Repository};
use crate::config::line_of;

/// Where the budget lives, from the repository's root.
pub(super) const PATH: &str = "budget.toml";
Expand Down Expand Up @@ -53,33 +53,24 @@ struct File {
impl Budget {
/// Loads the budget under `root`.
pub(super) fn load(root: &Path) -> Result<Self, Error> {
let path = root.join(PATH);
let text = match std::fs::read_to_string(&path) {
Ok(text) => text,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(Self::default());
}
Err(source) => return Err(Error::Read { path, source }),
let Some(file) = TomlFile::<File>::load(root, PATH)? else {
return Ok(Self::default());
};
let file: File = toml::from_str(&text).map_err(|error| Error::Toml {
path: PATH.into(),
message: error.to_string(),
})?;
Ok(Self {
expect: Ceilings::of(&text, file.expect),
invariant: Ceilings::of(&text, file.invariant),
expect: Ceilings::of(&file, &file.value.expect),
invariant: Ceilings::of(&file, &file.value.invariant),
})
}
}

impl Ceilings {
fn of(text: &str, table: BTreeMap<Spanned<String>, usize>) -> Self {
fn of(file: &TomlFile<File>, table: &BTreeMap<Spanned<String>, usize>) -> Self {
Self(
table
.into_iter()
.map(|(name, count)| {
let line = line_of(text, name.span().start);
(name.into_inner(), Ceiling { count, line })
.iter()
.map(|(name, &count)| {
let line = file.line(name);
(name.get_ref().clone(), Ceiling { count, line })
})
.collect(),
)
Expand Down
70 changes: 70 additions & 0 deletions crates/sourceorigin-standard/src/rules/manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! The workspace's manifests: the root's `[workspace]`, and each crate's,
//! reduced to what the rules read.

use std::collections::BTreeMap;
use std::path::Path;

use serde::Deserialize;
use toml::Spanned;

use super::Error;
use super::level::Level;
use super::toml_file::TomlFile;
use crate::metadata::Metadata;

/// The root manifest, from the repository's root.
pub(super) const ROOT: &str = "Cargo.toml";

/// The root manifest.
#[derive(Deserialize)]
pub(super) struct RootManifest {
#[serde(default)]
pub(super) workspace: Workspace,
}

/// The root manifest's `[workspace]`.
#[derive(Default, Deserialize)]
pub(super) struct Workspace {
/// Each tool's lints, such as `clippy`'s, each with its setting.
#[serde(default)]
pub(super) lints: BTreeMap<String, BTreeMap<Spanned<String>, Setting>>,
}

/// A lint's setting in a manifest: its level, alone or in a table beside
/// its priority.
#[derive(Deserialize)]
#[serde(untagged)]
pub(super) enum Setting {
Level(Level),
Table { level: Level },
}

/// A workspace crate's manifest.
#[derive(Deserialize)]
pub(super) struct Manifest {
pub(super) lints: Option<Spanned<toml::Table>>,
}

impl TomlFile<RootManifest> {
/// The root manifest under `root`.
pub(super) fn load_root(root: &Path) -> Result<Self, Error> {
Self::require(root, ROOT)
}
}

impl TomlFile<Manifest> {
/// The manifest of every crate `metadata` lists.
pub(super) fn load_all(metadata: &Metadata) -> Result<Vec<Self>, Error> {
metadata
.packages
.iter()
.map(|package| {
let path = &package.manifest_path;
Self::require(
&metadata.workspace_root,
path.strip_prefix(&metadata.workspace_root).unwrap_or(path),
)
})
.collect()
}
}
13 changes: 9 additions & 4 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ mod config_comments;
mod fix;
mod generated_lints;
mod level;
mod manifest;
mod modules;
mod report;
mod source;
#[cfg(test)]
mod tests;
mod toml_file;
mod unsafe_modules;
mod workspace_lints;

Expand All @@ -28,10 +30,12 @@ use budget::Budget;
use config_comments::Commented;
use fix::Checked;
pub use fix::Fix;
use manifest::{Manifest, RootManifest};
use modules::Modules;
pub use report::{Report, RuleReport};
use source::{Source, Unparsed};
use workspace_lints::{LintLevel, Manifest};
use toml_file::TomlFile;
use workspace_lints::LintLevel;

/// Declares the rules: each variant's doc comment is the doc [`Rule::doc`]
/// returns, its string its stable name, and its method the check it runs on
Expand Down Expand Up @@ -257,7 +261,7 @@ struct Repository {
/// The workspace's crates.
metadata: Metadata,
/// The manifests of the workspace's crates.
manifests: Vec<Manifest>,
manifests: Vec<TomlFile<Manifest>>,
/// The lints `[workspace.lints]` sets.
lint_levels: Vec<LintLevel>,
/// Every Rust file git tracks or would track that parses, by path.
Expand All @@ -278,10 +282,11 @@ impl Repository {
fn load(root: &Path) -> Result<Self, Error> {
let metadata = Metadata::load(Workspace::new(root))?;
let (sources, unparsed) = Source::load_all(root)?;
let root_manifest = TomlFile::<RootManifest>::load_root(root)?;
Ok(Self {
config: Config::load(root)?,
manifests: Manifest::load_all(&metadata)?,
lint_levels: LintLevel::load_all(&metadata.workspace_root)?,
manifests: TomlFile::load_all(&metadata)?,
lint_levels: LintLevel::all(&root_manifest),
modules: Modules::of(&metadata, &sources),
budget: Budget::load(root)?,
commented: Commented::load_all(root)?,
Expand Down
55 changes: 55 additions & 0 deletions crates/sourceorigin-standard/src/rules/toml_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! The TOML files the rules read, each deserialized into the shape its rule
//! needs, keeping its text for the lines of what the rule finds.

use std::path::{Path, PathBuf};

use serde::de::DeserializeOwned;
use toml::Spanned;

use super::Error;
use crate::config::line_of;

/// A TOML file, as `T` reads it.
pub(super) struct TomlFile<T> {
/// From the repository's root.
pub(super) path: PathBuf,
text: String,
pub(super) value: T,
}

impl<T: DeserializeOwned> TomlFile<T> {
/// The file at `path`, from `root`, or `None` when there is none.
pub(super) fn load(root: &Path, path: impl Into<PathBuf>) -> Result<Option<Self>, Error> {
let path = path.into();
match std::fs::read_to_string(root.join(&path)) {
Ok(text) => Self::parse(path, text).map(Some),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(source) => Err(Error::Read { path, source }),
}
}

/// The file at `path`, from `root`, which must exist.
pub(super) fn require(root: &Path, path: impl Into<PathBuf>) -> Result<Self, Error> {
let path = path.into();
let text = std::fs::read_to_string(root.join(&path)).map_err(|source| Error::Read {
path: path.clone(),
source,
})?;
Self::parse(path, text)
}

fn parse(path: PathBuf, text: String) -> Result<Self, Error> {
let value = toml::from_str(&text).map_err(|error| Error::Toml {
path: path.clone(),
message: error.to_string(),
})?;
Ok(Self { path, text, value })
}
}

impl<T> TomlFile<T> {
/// The line, from 1, where `spanned` starts.
pub(super) fn line<S>(&self, spanned: &Spanned<S>) -> usize {
line_of(&self.text, spanned.span().start)
}
}
Loading