]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lib.rs
Reduce visibility
[rust.git] / crates / rust-analyzer / src / lib.rs
1 //! Implementation of the LSP for rust-analyzer.
2 //!
3 //! This crate takes Rust-specific analysis results from ra_ide and translates
4 //! into LSP types.
5 //!
6 //! It also is the root of all state. `world` module defines the bulk of the
7 //! state, and `main_loop` module defines the rules for modifying it.
8 //!
9 //! The `cli` submodule implements some batch-processing analysis, primarily as
10 //! a debugging aid.
11 #![recursion_limit = "512"]
12
13 pub mod cli;
14
15 #[allow(unused)]
16 macro_rules! eprintln {
17     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
18 }
19
20 mod global_state;
21 mod main_loop;
22 mod handlers;
23 mod caps;
24 mod cargo_target_spec;
25 mod to_proto;
26 mod from_proto;
27 mod semantic_tokens;
28 mod markdown;
29 mod diagnostics;
30 mod line_endings;
31 mod request_metrics;
32 pub mod lsp_ext;
33 pub mod config;
34
35 use serde::de::DeserializeOwned;
36
37 pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
38 pub use crate::{
39     caps::server_capabilities,
40     main_loop::{main_loop, show_message},
41 };
42 use std::fmt;
43
44 pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
45     let res = T::deserialize(&json)
46         .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?;
47     Ok(res)
48 }
49
50 #[derive(Debug)]
51 struct LspError {
52     code: i32,
53     message: String,
54 }
55
56 impl LspError {
57     fn new(code: i32, message: String) -> LspError {
58         LspError { code, message }
59     }
60 }
61
62 impl fmt::Display for LspError {
63     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64         write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
65     }
66 }
67
68 impl std::error::Error for LspError {}