Skip to main content

Inventory

Struct Inventory 

Source
pub struct Inventory {
    pub items: Vec<Item>,
    pub in_file_tests: BTreeMap<String, InFileTests>,
    pub named_by_tests: BTreeSet<String>,
    pub test_files: Vec<String>,
    pub reexported: BTreeSet<String>,
    pub reexport_aliases: BTreeMap<String, String>,
}
Expand description

Everything the walk found: the items, the in-file tests per module, and what the crate’s integration tests name.

Fields§

§items: Vec<Item>

Every item found, exported or not, in declaration order of the walk.

§in_file_tests: BTreeMap<String, InFileTests>

Module path to the in-file tests declared in that module’s file.

§named_by_tests: BTreeSet<String>

Module paths named by the crate’s integration tests and benches: every majordomus_cli::a::b path those files mention, expanded to every prefix.

§test_files: Vec<String>

The test files that were read, crate-relative, so a report can say what it looked at.

§reexported: BTreeSet<String>

The crate paths a pub use in an exported module makes reachable, resolved to the item they name. An item in a private module that a public module re-exports is part of the crate’s surface, which is what missing_docs also thinks, so the inventory has to think it too.

§reexport_aliases: BTreeMap<String, String>

The name a re-export offers an item under, mapped to the item it names: majordomus_cli::capability::builtin::GetInput to majordomus_cli::capability::builtin::objects::GetInput.

A consumer writes the alias, so a test that exercises a module through one would otherwise look like a test that names no module at all. This is what carries the credit back to the module that declares the item. pub use x as y offers y; a glob, pub use m::*, offers every item of m in the re-exporting module and is recorded as <module>::* to m.

The same map is where rustdoc documents a re-exported item whose own module is private: under the alias, which is why the offered name is the one as chose.

Implementations§

Source§

impl Inventory

Source

pub fn of_crate(crate_dir: &Path) -> Result<Self, Error>

Walk a crate directory: src/lib.rs and every file its module graph reaches, then tests/ and benches/ for the module paths they name.

The directory is the crate’s, the one holding Cargo.toml. A crate whose root is missing is an error rather than an empty inventory: a quality report over nothing is the one result that must never be mistaken for a clean one.

use majordomus_cli::quality::source::Inventory;
let dir = tempfile::tempdir().unwrap();
assert!(Inventory::of_crate(dir.path()).is_err(), "no crate root, no inventory");
Source

pub fn item(&self, path: &str) -> Option<&Item>

One item by its full path.

use majordomus_cli::quality::source::Inventory;
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("src");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(src.join("lib.rs"), "//! Root.\n/// A type.\npub struct T;\n").unwrap();
let inv = Inventory::of_crate(dir.path()).unwrap();
assert!(inv.item("majordomus_cli::T").is_some());
assert!(inv.item("majordomus_cli::Nope").is_none());
Source

pub fn item_of(&self, path: &str, kind: ItemKind) -> Option<&Item>

One item by its path and its kind.

A struct’s field and one of its methods can share a path — T::n names both — because Rust keeps them in different namespaces and a path does not say which. Every caller that cares says so.

use majordomus_cli::quality::source::{Inventory, ItemKind};
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("src");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(src.join("lib.rs"), concat!(
    "//! Root.\n",
    "/// A counter.\n",
    "pub struct T { n: usize }\n",
    "impl T { /// The count.\n pub fn n(&self) -> usize { self.n } }\n",
)).unwrap();
let inv = Inventory::of_crate(dir.path()).unwrap();
let method = inv.item_of("majordomus_cli::T::n", ItemKind::Method).unwrap();
assert!(method.trivial_accessor);
assert!(inv.item_of("majordomus_cli::T::n", ItemKind::Field).is_some());
Source

pub fn exported(&self) -> Vec<&Item>

Every exported item, in walk order.

use majordomus_cli::quality::source::Inventory;
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("src");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(src.join("lib.rs"), "//! Root.\nmod hidden;\n").unwrap();
std::fs::write(src.join("hidden.rs"), "/// Not reachable.\npub fn inner() {}\n").unwrap();
let inv = Inventory::of_crate(dir.path()).unwrap();
assert!(inv.item("majordomus_cli::hidden::inner").unwrap().exported == false);
assert!(inv.exported().iter().all(|i| i.path != "majordomus_cli::hidden::inner"));
Source

pub fn exercised_by_a_test(&self, path: &str) -> bool

Does any test of this crate exercise the module at path?

A test counts when it names the module, names something under it, or names an item through the alias a pub use offers it under — which is how these tests are actually written: use majordomus_cli::capability::builtin::GetInput exercises capability::builtin::objects, and nothing in the text says so.

use majordomus_cli::quality::source::Inventory;
let dir = tempfile::tempdir().unwrap();
let src = dir.path().join("src");
std::fs::create_dir_all(src.join("outer")).unwrap();
std::fs::write(src.join("lib.rs"), "//! Root.\npub mod outer;\n").unwrap();
std::fs::write(src.join("outer/mod.rs"),
    "//! Outer.\npub mod inner;\npub use inner::Thing;\n").unwrap();
std::fs::write(src.join("outer/inner.rs"), "//! Inner.\n/// A thing.\npub struct Thing;\n").unwrap();
std::fs::create_dir_all(dir.path().join("tests")).unwrap();
std::fs::write(dir.path().join("tests/it.rs"),
    "use majordomus_cli::outer::Thing;\n#[test]\nfn t() { let _ = Thing; }\n").unwrap();

let inv = Inventory::of_crate(dir.path()).unwrap();
assert!(inv.exercised_by_a_test("majordomus_cli::outer::inner"),
        "the test reaches inner through the alias outer::Thing");
Source

pub fn exported_modules(&self) -> Vec<&Item>

Every exported module, in walk order. The crate root is one of them.

Trait Implementations§

Source§

impl Clone for Inventory

Source§

fn clone(&self) -> Inventory

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Inventory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Inventory

Source§

fn default() -> Inventory

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Inventory

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Inventory

Source§

impl JsonSchema for Inventory

Source§

fn schema_name() -> Cow<'static, str>

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn inline_schema() -> bool

Whether JSON Schemas generated for this type should be included directly in parent schemas, rather than being re-used where possible using the $ref keyword. Read more
Source§

impl PartialEq for Inventory

Source§

fn eq(&self, other: &Inventory) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Inventory

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Inventory

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more