Files
rust/crates/proc_macro_api/src/rpc.rs

108 lines
3.3 KiB
Rust
Raw Normal View History

2020-08-13 12:07:28 +02:00
//! Data structure serialization related stuff for RPC
2020-03-28 18:25:19 +08:00
//!
2020-04-20 21:26:10 +03:00
//! Defines all necessary rpc serialization data structures,
2020-08-12 16:46:20 +02:00
//! which includes `tt` related data and some task messages.
//! Although adding `Serialize` and `Deserialize` traits to `tt` directly seems
//! to be much easier, we deliberately duplicate `tt` structs with `#[serde(with = "XXDef")]`
2020-03-28 18:12:51 +08:00
//! for separation of code responsibility.
pub(crate) mod flat;
2020-03-27 04:26:34 +08:00
use std::path::PathBuf;
2020-08-12 16:46:20 +02:00
use serde::{Deserialize, Serialize};
use crate::rpc::flat::FlatTree;
2020-03-27 04:26:34 +08:00
#[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub struct ListMacrosTask {
pub lib: PathBuf,
2020-03-27 04:26:34 +08:00
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
2020-03-27 04:26:34 +08:00
pub enum ProcMacroKind {
CustomDerive,
FuncLike,
Attr,
}
#[derive(Clone, Eq, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct ListMacrosResult {
pub macros: Vec<(String, ProcMacroKind)>,
}
#[derive(Debug, Serialize, Deserialize)]
2020-03-27 04:26:34 +08:00
pub struct ExpansionTask {
/// Argument of macro call.
///
2020-04-20 21:26:10 +03:00
/// In custom derive this will be a struct or enum; in attribute-like macro - underlying
2020-03-27 04:26:34 +08:00
/// item; in function-like macro - the macro body.
pub macro_body: FlatTree,
2020-03-27 04:26:34 +08:00
2020-04-20 22:24:10 +03:00
/// Name of macro to expand.
2020-03-27 04:26:34 +08:00
///
2020-04-20 22:24:10 +03:00
/// In custom derive this is the name of the derived trait (`Serialize`, `Getters`, etc.).
/// In attribute-like and function-like macros - single name of macro itself (`show_streams`).
2020-03-27 04:26:34 +08:00
pub macro_name: String,
/// Possible attributes for the attribute-like macros.
pub attributes: Option<FlatTree>,
2020-03-27 04:26:34 +08:00
pub lib: PathBuf,
2020-12-11 14:57:50 +01:00
/// Environment variables to set during macro expansion.
pub env: Vec<(String, String)>,
2020-03-27 04:26:34 +08:00
}
#[derive(Debug, Serialize, Deserialize)]
2020-03-27 04:26:34 +08:00
pub struct ExpansionResult {
pub expansion: FlatTree,
2020-03-27 04:26:34 +08:00
}
#[cfg(test)]
mod tests {
use super::*;
use tt::*;
2020-03-27 04:26:34 +08:00
fn fixture_token_tree() -> Subtree {
let mut subtree = Subtree::default();
subtree
.token_trees
.push(TokenTree::Leaf(Ident { text: "struct".into(), id: TokenId(0) }.into()));
subtree
.token_trees
.push(TokenTree::Leaf(Ident { text: "Foo".into(), id: TokenId(1) }.into()));
subtree.token_trees.push(TokenTree::Leaf(Leaf::Literal(Literal {
text: "Foo".into(),
id: TokenId::unspecified(),
})));
subtree.token_trees.push(TokenTree::Leaf(Leaf::Punct(Punct {
char: '@',
id: TokenId::unspecified(),
spacing: Spacing::Joint,
})));
subtree.token_trees.push(TokenTree::Subtree(Subtree {
delimiter: Some(Delimiter { id: TokenId(2), kind: DelimiterKind::Brace }),
token_trees: vec![],
}));
2020-03-27 04:26:34 +08:00
subtree
}
#[test]
fn test_proc_macro_rpc_works() {
let tt = fixture_token_tree();
let task = ExpansionTask {
macro_body: FlatTree::new(&tt),
2020-03-27 04:26:34 +08:00
macro_name: Default::default(),
attributes: None,
lib: std::env::current_dir().unwrap(),
2020-12-11 14:57:50 +01:00
env: Default::default(),
2020-03-27 04:26:34 +08:00
};
let json = serde_json::to_string(&task).unwrap();
println!("{}", json);
2020-03-27 04:26:34 +08:00
let back: ExpansionTask = serde_json::from_str(&json).unwrap();
assert_eq!(tt, back.macro_body.to_subtree());
2020-03-27 04:26:34 +08:00
}
}