Skip to main content

knave_expand/project/
mod.rs

1mod crate_set;
2mod dependency_reader;
3mod fields;
4mod name;
5mod problems;
6mod project_editor;
7mod reader;
8
9use std::path::PathBuf;
10
11pub use crate_set::CrateSet;
12pub use dependency_reader::Section;
13pub use name::ProjectName;
14pub use problems::ProjectProblems;
15pub use project_editor::{CrateEntry, ProjectEditor};
16pub use reader::ProjectReader;
17
18use crate::source::Span;
19
20#[derive(Debug)]
21pub struct Project {
22    pub name: String,
23    pub version: String,
24    pub entry: PathBuf,
25    pub library_root: PathBuf,
26    pub edition: i128,
27    pub libraries: Vec<LibraryDependency>,
28    pub dependencies: Vec<CrateDependency>,
29    pub dev_dependencies: Vec<CrateDependency>,
30}
31
32#[derive(Debug)]
33pub struct LibraryDependency {
34    pub name: String,
35    pub name_span: Span,
36    pub source: LibrarySource,
37}
38
39#[derive(Debug)]
40pub enum LibrarySource {
41    Path(PathBuf),
42    Git { url: String, commit: String },
43}
44
45impl LibrarySource {
46    pub const FULL_COMMIT_LENGTH: usize = 40;
47}
48
49#[derive(Clone, Debug)]
50pub struct CrateDependency {
51    pub name: String,
52    pub name_span: Span,
53    pub version: Option<String>,
54    pub features: Vec<String>,
55    pub path: Option<PathBuf>,
56}
57
58impl CrateDependency {
59    pub fn same_request(&self, other: &Self) -> bool {
60        self.version == other.version
61            && self.path == other.path
62            && Self::covers(&self.features, &other.features)
63            && Self::covers(&other.features, &self.features)
64    }
65
66    fn covers(features: &[String], others: &[String]) -> bool {
67        others.iter().all(|feature| features.contains(feature))
68    }
69
70    pub fn resolved_from(&self, root: &std::path::Path) -> Self {
71        Self {
72            path: self.path.as_ref().map(|path| root.join(path)),
73            ..self.clone()
74        }
75    }
76}