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 Oct 24, 2024
1 parent 7e4347c commit 0b9efa9
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
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/proc-macro-server-api",
"utils/scarb-build-metadata",
"utils/scarb-stable-hash",
"utils/scarb-test-support",
Expand Down
14 changes: 14 additions & 0 deletions utils/proc-macro-server-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "proc-macro-server-api"
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" }
serde.workspace = true
serde_json.workspace = true
25 changes: 25 additions & 0 deletions utils/proc-macro-server-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use serde::{de::DeserializeOwned, Deserialize, Serialize};

pub mod methods;

pub type RequestId = u64;

#[derive(Serialize, Deserialize)]
pub struct RpcResponse {
pub id: RequestId,
pub value: serde_json::Value,
}

#[derive(Serialize, Deserialize)]
pub struct RpcRequest {
pub id: RequestId,
pub method: String,
pub value: serde_json::Value,
}

pub trait Method {
const METHOD: &str;

type Params: Serialize + DeserializeOwned;
type Response: Serialize + DeserializeOwned;
}
53 changes: 53 additions & 0 deletions utils/proc-macro-server-api/src/methods/defined_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::Method;
use serde::{Deserialize, Serialize};
use std::iter::Sum;

#[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 {}

impl Sum for DefinedMacrosResponse {
fn sum<T: IntoIterator<Item = Self>>(iter: T) -> Self {
let mut iter = iter.into_iter();

let first = iter.next();

let Some(mut base) = first else {
return Default::default();
};

for other in iter {
base.attributes.extend(other.attributes);
base.inline_macros.extend(other.inline_macros);
base.derives.extend(other.derives);
base.executables.extend(other.executables);
}

base.attributes.sort();
base.attributes.dedup();
base.inline_macros.sort();
base.inline_macros.dedup();
base.derives.sort();
base.derives.dedup();
base.executables.sort();
base.executables.dedup();

base
}
}

pub struct DefinedMacros;

impl Method for DefinedMacros {
const METHOD: &'static str = "defined-macros";

type Params = DefinedMacrosParams;
type Response = DefinedMacrosResponse;
}
50 changes: 50 additions & 0 deletions utils/proc-macro-server-api/src/methods/expand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::ProcMacroResult;
use crate::Method;
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 = "expand-attribute";

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 = "expand-derive";

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 = "expand-inline";

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

pub mod defined_macros;
pub mod expand;

#[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 0b9efa9

Please sign in to comment.