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.
|
2021-08-28 20:36:41 +03:00
|
|
|
pub(crate) mod flat;
|
2020-03-27 04:26:34 +08:00
|
|
|
|
2021-08-28 22:03:06 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
2020-08-12 16:46:20 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-28 20:36:41 +03:00
|
|
|
|
|
|
|
|
use crate::rpc::flat::FlatTree;
|
2020-03-27 04:26:34 +08:00
|
|
|
|
|
|
|
|
#[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct ListMacrosTask {
|
2021-08-28 22:03:06 +03:00
|
|
|
pub lib: PathBuf,
|
2020-03-27 04:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-07 17:06:14 +01: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)>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-28 20:36:41 +03:00
|
|
|
#[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.
|
2021-08-28 20:36:41 +03:00
|
|
|
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.
|
2021-08-28 20:36:41 +03:00
|
|
|
pub attributes: Option<FlatTree>,
|
2020-03-27 04:26:34 +08:00
|
|
|
|
2021-08-28 22:03:06 +03: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
|
|
|
}
|
|
|
|
|
|
2021-08-28 20:36:41 +03:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2020-03-27 04:26:34 +08:00
|
|
|
pub struct ExpansionResult {
|
2021-08-28 20:36:41 +03:00
|
|
|
pub expansion: FlatTree,
|
2020-03-27 04:26:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2021-08-28 20:36:41 +03:00
|
|
|
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()));
|
2021-08-28 20:36:41 +03:00
|
|
|
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,
|
|
|
|
|
})));
|
2021-03-17 01:27:56 +01:00
|
|
|
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 {
|
2021-08-28 20:36:41 +03:00
|
|
|
macro_body: FlatTree::new(&tt),
|
2020-03-27 04:26:34 +08:00
|
|
|
macro_name: Default::default(),
|
|
|
|
|
attributes: None,
|
2021-08-28 22:03:06 +03:00
|
|
|
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();
|
2021-08-28 20:36:41 +03:00
|
|
|
println!("{}", json);
|
2020-03-27 04:26:34 +08:00
|
|
|
let back: ExpansionTask = serde_json::from_str(&json).unwrap();
|
|
|
|
|
|
2021-08-28 20:36:41 +03:00
|
|
|
assert_eq!(tt, back.macro_body.to_subtree());
|
2020-03-27 04:26:34 +08:00
|
|
|
}
|
|
|
|
|
}
|