1#![no_std]
2
3#[macro_use]
4extern crate alloc;
5
6#[cfg(any(test, feature = "std"))]
7extern crate std;
8
9#[cfg(feature = "serde")]
10pub mod ast;
11mod dependencies;
12mod linkage;
13mod package;
14mod profile;
15mod target;
16#[cfg(all(test, feature = "std", feature = "serde"))]
17mod tests;
18mod workspace;
19
20use alloc::{sync::Arc, vec::Vec};
21
22#[cfg(feature = "serde")]
23use miden_assembly_syntax::{
24 Report,
25 debuginfo::{SourceFile, SourceId},
26 diagnostics::{Label, RelatedError, RelatedLabel},
27};
28pub use miden_assembly_syntax::{Word, debuginfo::Uri, semver};
30use miden_assembly_syntax::{
31 debuginfo::{SourceSpan, Span},
32 diagnostics::{Diagnostic, miette},
33};
34pub use miden_mast_package::TargetType;
35#[cfg(feature = "serde")]
36use serde::{Deserialize, Serialize};
37pub use toml::Value;
38
39pub use self::{
40 dependencies::*, linkage::Linkage, package::Package, profile::Profile, target::Target,
41 workspace::Workspace,
42};
43
44pub type Map<K, V> = alloc::collections::BTreeMap<K, V>;
46
47pub type Metadata = Map<Span<Arc<str>>, Span<Value>>;
51
52pub type MetadataSet = Map<Span<Arc<str>>, Metadata>;
56
57#[derive(Debug, Clone)]
59pub enum Project {
60 WorkspacePackage {
62 package: Arc<Package>,
64 workspace: Arc<Workspace>,
66 },
67 Package(Arc<Package>),
69}
70
71impl From<alloc::boxed::Box<Package>> for Project {
72 fn from(value: alloc::boxed::Box<Package>) -> Self {
73 Self::Package(value.into())
74 }
75}
76
77impl From<Arc<Package>> for Project {
78 fn from(value: Arc<Package>) -> Self {
79 Self::Package(value)
80 }
81}
82
83impl Project {
84 pub fn is_workspace_member(&self) -> bool {
86 matches!(self, Self::WorkspacePackage { .. })
87 }
88
89 pub fn package(&self) -> Arc<Package> {
91 match self {
92 Self::WorkspacePackage { package, .. } | Self::Package(package) => Arc::clone(package),
93 }
94 }
95
96 #[cfg(feature = "std")]
98 pub fn manifest_path(&self) -> Option<&std::path::Path> {
99 match self {
100 Self::WorkspacePackage { package, .. } | Self::Package(package) => {
101 package.manifest_path()
102 },
103 }
104 }
105}
106
107#[cfg(all(feature = "std", feature = "serde"))]
109impl Project {
110 pub fn load(
115 path: impl AsRef<std::path::Path>,
116 source_manager: &dyn miden_assembly_syntax::debuginfo::SourceManager,
117 ) -> Result<Self, Report> {
118 let path = path.as_ref();
119 let manifest_path = if path.is_dir() {
120 path.join("miden-project.toml").canonicalize().map_err(Report::msg)?
121 } else {
122 path.canonicalize().map_err(Report::msg)?
123 };
124
125 Self::try_load_as_workspace_member(None, &manifest_path, source_manager)
126 }
127
128 pub fn load_project_reference(
133 name: &str,
134 path: impl AsRef<std::path::Path>,
135 source_manager: &dyn miden_assembly_syntax::debuginfo::SourceManager,
136 ) -> Result<Self, Report> {
137 let path = path.as_ref();
138 let manifest_path = if path.is_dir() {
139 path.join("miden-project.toml").canonicalize().map_err(Report::msg)?
140 } else {
141 path.canonicalize().map_err(Report::msg)?
142 };
143
144 Self::try_load_as_workspace_member(Some(name), &manifest_path, source_manager)
145 }
146
147 fn try_load_as_workspace_member(
148 name: Option<&str>,
149 manifest_path: impl AsRef<std::path::Path>,
150 source_manager: &dyn miden_assembly_syntax::debuginfo::SourceManager,
151 ) -> Result<Self, Report> {
152 use miden_assembly_syntax::debuginfo::SourceManagerExt;
153
154 let manifest_path = manifest_path.as_ref();
155 let ancestors = manifest_path
156 .parent()
157 .ok_or_else(|| {
158 Report::msg(format!(
159 "manifest '{}' has no parent directory",
160 manifest_path.display()
161 ))
162 })?
163 .ancestors();
164
165 let initial_package_dir = manifest_path.parent();
166 for ancestor in ancestors {
167 let workspace_manifest = ancestor.join("miden-project.toml");
168 if !workspace_manifest.exists() {
169 continue;
170 }
171
172 let source = source_manager.load_file(&workspace_manifest).map_err(Report::msg)?;
173
174 let contents = toml::from_str::<toml::Table>(source.as_str()).map_err(|err| {
175 Report::msg(format!("could not parse {}: {err}", workspace_manifest.display()))
176 })?;
177 if contents.contains_key("workspace") {
178 let workspace = Workspace::load(source, source_manager)?;
179 let package = if let Some(package) = workspace
180 .members()
181 .iter()
182 .find(|member| member.manifest_path().is_some_and(|path| path == manifest_path))
183 .cloned()
184 {
185 package
186 } else if manifest_path == workspace_manifest {
187 let Some(name) = name else {
188 break;
189 };
190 workspace.get_member_by_name(name).ok_or_else(|| {
191 Report::msg(format!(
192 "workspace '{}' does not contain a member named '{name}'",
193 workspace_manifest.display(),
194 ))
195 })?
196 } else {
197 break;
198 };
199
200 return Ok(Self::WorkspacePackage { package, workspace: workspace.into() });
201 } else if Some(ancestor) != initial_package_dir {
202 break;
203 }
204 }
205
206 let source = source_manager.load_file(manifest_path).map_err(Report::msg)?;
207 let package = Package::load(source)?;
208 Ok(Self::Package(package.into()))
209 }
210}
211
212#[cfg(all(feature = "std", feature = "serde"))]
216pub(crate) fn absolutize_path(
217 path: &std::path::Path,
218 workspace_root: &std::path::Path,
219) -> Result<std::path::PathBuf, std::io::Error> {
220 if path.is_absolute() {
221 path.canonicalize()
222 } else {
223 workspace_root.join(path).canonicalize()
224 }
225}