Skip to main content

Context

Struct Context 

Source
pub struct Context<'a> {
    pub project_root: PathBuf,
    pub start_dir: PathBuf,
    pub matched: Vec<String>,
    pub config_files: Vec<PathBuf>,
    pub pins: BTreeMap<String, String>,
    pub reason: SelectionReason,
    pub score: u32,
    /* private fields */
}
Expand description

The context the host passes to the plugin. Read-only.

Fields§

§project_root: PathBuf

Project root: where the command runs unless the answer names a cwd of its own.

§start_dir: PathBuf

The directory the person ran pmpx from (its -C, or the process’s own directory).

It differs from project_root whenever the root was found by walking up, which makes this the only way to tell which package of a monorepo the command is for. Not where the command will run: that is the cwd of the answer, and it defaults to project_root.

Empty when the host had nothing to say about it.

§matched: Vec<String>

The files this detection matched, relative to project_root.

This is the plugin’s main channel for learning “what the project looks like”. Example: the yarn plugin distinguishes classic from berry via has_matched(".yarnrc.yml") without reading a single file.

§config_files: Vec<PathBuf>

The project config files that were read, nearest first.

§pins: BTreeMap<String, String>

[plugin] pins from the project config: family → plugin name.

The whole map, not just this plugin’s family: it is how a plugin can notice that the project pins something for its family which is not installed, and say so.

§reason: SelectionReason

Why this plugin was selected.

§score: u32

The evidence score it won with. 0 when it was pinned or named outright, since neither needed evidence.

Implementations§

Source§

impl<'a> Context<'a>

Source

pub fn builder() -> ContextBuilder

A context for a plugin’s own test: hand-built, with no host behind it.

ⓘ
let ctx = Context::builder()
    .project_root("/work")
    .matched(["pnpm-lock.yaml"])
    .file("package.json", "{\"name\":\"x\"}")
    .build();
Source

pub fn has_matched(&self, file: &str) -> bool

Whether one of the matched files is this one – the standard way for a plugin to branch on shape.

Source

pub fn was_pinned(&self) -> bool

Whether the project config is why this plugin is the one being asked.

Source

pub fn pinned_for(&self, family: &str) -> Option<&str>

What the project pins this plugin’s family to, if anything.

Source

pub fn file(&self, name: &str) -> Option<ContextFile>

The contents of one file this plugin declared in its manifest’s [context] files.

The host reads it now if it has not been read yet. None means it could not be handed over – a missing file, one that cannot be read, or a name the manifest did not declare. Whether that matters is the plugin’s call: a missing lockfile and a missing optional config are different things.

Source

pub fn file_str(&self, name: &str) -> Option<String>

The same, as text: None when the file was not handed over or is not UTF-8.

Trait Implementations§

Source§

impl<'a> Clone for Context<'a>

Source§

fn clone(&self) -> Self

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<'a> Debug for Context<'a>

Source§

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

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

impl Default for Context<'_>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'a> !Send for Context<'a>

§

impl<'a> !Sync for Context<'a>

§

impl<'a> Freeze for Context<'a>

§

impl<'a> RefUnwindSafe for Context<'a>

§

impl<'a> Unpin for Context<'a>

§

impl<'a> UnsafeUnpin for Context<'a>

§

impl<'a> UnwindSafe for Context<'a>

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
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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.