1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Collection of cargo workspace data models.
//!
//! To start parsing types, construct a `CargoWorkspace`, which you
//! can then modify with commands found in [`ops`](../ops/index.html).

mod cargo;
pub use cargo::{CargoCrate, CargoWorkspace};

mod _crate;
pub use _crate::Crate;

mod publish;
pub use publish::{MutationSet, PubMutation};

mod graph;
pub use graph::DepGraph;

pub type CrateId = usize;

use crate::{ops::Op, query::Query};
use std::path::PathBuf;

/// A fully parsed workspace
pub struct Workspace {
    pub root: PathBuf,
    dgraph: DepGraph,
}

impl Workspace {
    /// Create a parsed workspace by passing in the stage1 parse data
    pub fn process(cws: CargoWorkspace) -> Self {
        let CargoWorkspace { root, crates } = cws;

        let mut dgraph = DepGraph::new();
        crates.into_iter().for_each(|cc| dgraph.add_crate(cc));
        dgraph.finalise();

        Self { root, dgraph }
    }

    /// Execute a query on this workspace to find crate IDs
    pub fn query(&self, q: Query) -> Vec<CrateId> {
        q.execute(&self.dgraph)
    }

    /// Execute an operation on a set of crates this in workspace
    pub fn execute(&mut self, op: Op, target: Vec<CrateId>) {
        op.execute(target, self.root.clone(), &mut self.dgraph)
    }
}