Skip to content

Commit

Permalink
Use a newtype for the serialized PackageId in Proc Macro Server
Browse files Browse the repository at this point in the history
  • Loading branch information
integraledelebesgue committed Mar 4, 2025
1 parent bf30ab7 commit 2955b2b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
7 changes: 4 additions & 3 deletions scarb/src/compiler/plugin/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::HashMap, sync::Arc};
use anyhow::Result;
use cairo_lang_semantic::{inline_macros::get_default_plugin_suite, plugin::PluginSuite};
use itertools::Itertools;
use scarb_proc_macro_server_types::scope::SerializedPackageId;

use crate::{
compiler::{CairoCompilationUnit, CompilationUnitComponentId, CompilationUnitDependency},
Expand Down Expand Up @@ -58,7 +59,7 @@ pub struct WorkspaceProcMacros {
/// A mapping of the form: `package (as a serialized [`PackageId`]) -> plugin`.
/// Contains IDs of all packages from the workspace, each mapped to a [`ProcMacroHostPlugin`] which contains
/// **all proc macro dependencies of the package** collected from **all compilation units it appears in**.
pub macros_for_packages: HashMap<String, Arc<ProcMacroHostPlugin>>,
pub macros_for_packages: HashMap<SerializedPackageId, Arc<ProcMacroHostPlugin>>,
}

impl WorkspaceProcMacros {
Expand Down Expand Up @@ -90,7 +91,7 @@ impl WorkspaceProcMacros {

let plugin = Arc::new(ProcMacroHostPlugin::try_new(deduplicated_instances)?);

Ok((package_id, plugin))
Ok((package_id.into(), plugin))
})
.collect::<Result<HashMap<_, _>>>()?;

Expand All @@ -100,7 +101,7 @@ impl WorkspaceProcMacros {
}

/// Returns a [`ProcMacroHostPlugin`] assigned to the package with `package_id`.
pub fn get(&self, package_id: &str) -> Option<Arc<ProcMacroHostPlugin>> {
pub fn get(&self, package_id: &SerializedPackageId) -> Option<Arc<ProcMacroHostPlugin>> {
self.macros_for_packages.get(package_id).cloned()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use crate::scope::SerializedPackageId;

use super::Method;
use serde::{Deserialize, Serialize};

Expand All @@ -16,7 +18,7 @@ pub struct DefinedMacrosResponse {
/// A mapping of the form: `package (as a serialized `PackageId`) -> macros info`.
/// Contains serialized IDs of all packages from the workspace,
/// mapped to the [`PackageDefinedMacrosInfo`], describing macros available for them.
pub macros_by_package_id: HashMap<String, PackageDefinedMacrosInfo>,
pub macros_by_package_id: HashMap<SerializedPackageId, PackageDefinedMacrosInfo>,
}

/// Response structure containing lists of all defined macros available for one package.
Expand Down
31 changes: 30 additions & 1 deletion utils/scarb-proc-macro-server-types/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

/// A string-serialized `PackageId` from the Scarb workspace.
/// Follows the schema: "crate-name version source-path".
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[repr(transparent)] // Guarantees a backward-compatibility with the previous version.
pub struct SerializedPackageId(pub String);

impl From<String> for SerializedPackageId {
fn from(value: String) -> Self {
Self(value)
}
}

impl Display for SerializedPackageId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl SerializedPackageId {
pub fn name(&self) -> &str {
self.0
.split(" ")
.next()
.expect("SerializedPackageId should follow a format `<name> <version> (<source>)`")
}
}

/// A description of the location in the workspace where particular macro is available.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct ProcMacroScope {
/// Serialized `PackageId` of the package in which context the action occurs.
pub package_id: String,
pub package_id: SerializedPackageId,
}
5 changes: 3 additions & 2 deletions utils/scarb-test-support/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use scarb_proc_macro_server_types::methods::defined_macros::DefinedMacros;
use scarb_proc_macro_server_types::methods::defined_macros::DefinedMacrosParams;
use scarb_proc_macro_server_types::methods::defined_macros::PackageDefinedMacrosInfo;
use scarb_proc_macro_server_types::methods::Method;
use scarb_proc_macro_server_types::scope::SerializedPackageId;
use std::collections::HashMap;
use std::io::BufRead;
use std::io::BufReader;
Expand Down Expand Up @@ -73,7 +74,7 @@ pub struct ProcMacroClient {
/// identified by the `package_id` which is a serialized `PackageId`.
pub struct DefinedMacrosInfo {
/// An ID of the package recognized by PMS.
pub package_id: String,
pub package_id: SerializedPackageId,
/// A proper part of the response, related to the main component of the main CU.
pub defined_macros: PackageDefinedMacrosInfo,
}
Expand Down Expand Up @@ -193,7 +194,7 @@ impl ProcMacroClient {
// Usually, we can't discover the ID of the mock package used in test, so we extract it from the PMS response.
let package_id = response
.keys()
.find(|cu_id| cu_id.starts_with(package_name))
.find(|cu_id| cu_id.0.starts_with(package_name))
.expect("Response from Proc Macro Server should contain the main compilation unit.")
.to_owned();

Expand Down

0 comments on commit 2955b2b

Please sign in to comment.