rust_analyzer_proc_macro_srv/
main.rs1#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
4#![cfg_attr(not(feature = "sysroot-abi"), allow(unused_crate_dependencies))]
5#![allow(clippy::print_stdout, clippy::print_stderr)]
6
7#[cfg(feature = "in-rust-tree")]
8extern crate rustc_driver as _;
9
10mod version;
11
12#[cfg(feature = "sysroot-abi")]
13mod main_loop;
14use clap::{Command, ValueEnum};
15#[cfg(feature = "sysroot-abi")]
16use main_loop::run;
17
18fn main() -> std::io::Result<()> {
19 let v = std::env::var("RUST_ANALYZER_INTERNALS_DO_NOT_USE");
20 if v.is_err() {
21 eprintln!(
22 "This is an IDE implementation detail, you can use this tool by exporting RUST_ANALYZER_INTERNALS_DO_NOT_USE."
23 );
24 eprintln!(
25 "Note that this tool's API is highly unstable and may break without prior notice"
26 );
27 std::process::exit(122);
28 }
29 let matches = Command::new("proc-macro-srv")
30 .args(&[
31 clap::Arg::new("format")
32 .long("format")
33 .action(clap::ArgAction::Set)
34 .default_value("json-legacy")
35 .value_parser(clap::builder::EnumValueParser::<ProtocolFormat>::new()),
36 clap::Arg::new("version")
37 .long("version")
38 .action(clap::ArgAction::SetTrue)
39 .help("Prints the version of the proc-macro-srv"),
40 ])
41 .get_matches();
42 if matches.get_flag("version") {
43 println!("rust-analyzer-proc-macro-srv {}", version::version());
44 return Ok(());
45 }
46 let &format =
47 matches.get_one::<ProtocolFormat>("format").expect("format value should always be present");
48 run(format)
49}
50
51#[derive(Copy, Clone)]
52enum ProtocolFormat {
53 JsonLegacy,
54 PostcardLegacy,
55}
56
57impl ValueEnum for ProtocolFormat {
58 fn value_variants<'a>() -> &'a [Self] {
59 &[ProtocolFormat::JsonLegacy, ProtocolFormat::PostcardLegacy]
60 }
61
62 fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
63 match self {
64 ProtocolFormat::JsonLegacy => Some(clap::builder::PossibleValue::new("json-legacy")),
65 ProtocolFormat::PostcardLegacy => {
66 Some(clap::builder::PossibleValue::new("postcard-legacy"))
67 }
68 }
69 }
70 fn from_str(input: &str, _ignore_case: bool) -> Result<Self, String> {
71 match input {
72 "json-legacy" => Ok(ProtocolFormat::JsonLegacy),
73 "postcard-legacy" => Ok(ProtocolFormat::PostcardLegacy),
74 _ => Err(format!("unknown protocol format: {input}")),
75 }
76 }
77}
78
79#[cfg(not(feature = "sysroot-abi"))]
80fn run(_: ProtocolFormat) -> std::io::Result<()> {
81 Err(std::io::Error::new(
82 std::io::ErrorKind::Unsupported,
83 "proc-macro-srv-cli needs to be compiled with the `sysroot-abi` feature to function"
84 .to_owned(),
85 ))
86}