]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/lib.rs
Merge #5063
[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 mod lsp_utils;
33 mod thread_pool;
34 pub mod lsp_ext;
35 pub mod config;
36
37 use serde::de::DeserializeOwned;
38
39 pub type Result<T, E = Box<dyn std::error::Error + Send + Sync>> = std::result::Result<T, E>;
40 pub use crate::{caps::server_capabilities, lsp_utils::show_message, main_loop::main_loop};
41 use std::fmt;
42
43 pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
44     let res = T::deserialize(&json)
45         .map_err(|e| format!("Failed to deserialize {}: {}; {}", what, e, json))?;
46     Ok(res)
47 }
48
49 #[derive(Debug)]
50 struct LspError {
51     code: i32,
52     message: String,
53 }
54
55 impl LspError {
56     fn new(code: i32, message: String) -> LspError {
57         LspError { code, message }
58     }
59 }
60
61 impl fmt::Display for LspError {
62     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63         write!(f, "Language Server request failed with {}. ({})", self.code, self.message)
64     }
65 }
66
67 impl std::error::Error for LspError {}