Skip to content

Commit

Permalink
Proc macro server API
Browse files Browse the repository at this point in the history
commit-id:8fdc1e29
  • Loading branch information
Draggu committed Nov 4, 2024
1 parent 5cb0446 commit 0a3324e
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"plugins/cairo-lang-macro-attributes",
"plugins/cairo-lang-macro-stable",
"utils/create-output-dir",
"utils/scarb-proc-macro-server-types",
"utils/scarb-build-metadata",
"utils/scarb-stable-hash",
"utils/scarb-test-support",
Expand Down
14 changes: 14 additions & 0 deletions utils/scarb-proc-macro-server-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "scarb-proc-macro-server-types"
version.workspace = true
edition.workspace = true
authors.workspace = true
homepage.workspace = true
license.workspace = true
readme.workspace = true
repository.workspace = true

[dependencies]
cairo-lang-macro = { path = "../../plugins/cairo-lang-macro", features = ["serde"], version = "0.1.0"}
serde.workspace = true
serde_json.workspace = true
24 changes: 24 additions & 0 deletions utils/scarb-proc-macro-server-types/src/jsonrpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use serde::{Deserialize, Serialize};

pub type RequestId = u64;

#[derive(Serialize, Deserialize)]
pub struct RpcResponse {
pub id: RequestId,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<ResponseError>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ResponseError {
pub message: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RpcRequest {
pub id: RequestId,
pub method: String,
pub value: serde_json::Value,
}
2 changes: 2 additions & 0 deletions utils/scarb-proc-macro-server-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod jsonrpc;
pub mod methods;
22 changes: 22 additions & 0 deletions utils/scarb-proc-macro-server-types/src/methods/defined_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::Method;
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct DefinedMacrosResponse {
pub attributes: Vec<String>,
pub inline_macros: Vec<String>,
pub derives: Vec<String>,
pub executables: Vec<String>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct DefinedMacrosParams {}

pub struct DefinedMacros;

impl Method for DefinedMacros {
const METHOD: &'static str = "definedMacros";

type Params = DefinedMacrosParams;
type Response = DefinedMacrosResponse;
}
50 changes: 50 additions & 0 deletions utils/scarb-proc-macro-server-types/src/methods/expand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::Method;
use super::ProcMacroResult;
use cairo_lang_macro::TokenStream;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct ExpandAttributeParams {
pub attr: String,
pub args: TokenStream,
pub item: TokenStream,
}

pub struct ExpandAttribute;

impl Method for ExpandAttribute {
const METHOD: &'static str = "expandAttribute";

type Params = ExpandAttributeParams;
type Response = ProcMacroResult;
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct ExpandDeriveParams {
pub derives: Vec<String>,
pub item: TokenStream,
}

pub struct ExpandDerive;

impl Method for ExpandDerive {
const METHOD: &'static str = "expandDerive";

type Params = ExpandDeriveParams;
type Response = ProcMacroResult;
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
pub struct ExpandInlineMacroParams {
pub name: String,
pub item: TokenStream,
}

pub struct ExpandInline;

impl Method for ExpandInline {
const METHOD: &'static str = "expandInline";

type Params = ExpandInlineMacroParams;
type Response = ProcMacroResult;
}
18 changes: 18 additions & 0 deletions utils/scarb-proc-macro-server-types/src/methods/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use cairo_lang_macro::TokenStream;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

pub mod defined_macros;
pub mod expand;

pub trait Method {
const METHOD: &str;

type Params: Serialize + DeserializeOwned;
type Response: Serialize + DeserializeOwned;
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ProcMacroResult {
pub token_stream: TokenStream,
pub diagnostics: Vec<cairo_lang_macro::Diagnostic>,
}

0 comments on commit 0a3324e

Please sign in to comment.