tower_lsp/service/
state.rs

1//! Types representing the current state of the language server.
2
3use std::fmt::{self, Debug, Formatter};
4use std::sync::atomic::{AtomicU8, Ordering};
5
6/// A list of possible states the language server can be in.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8#[repr(u8)]
9pub enum State {
10    /// Server has not received an `initialize` request.
11    Uninitialized = 0,
12    /// Server received an `initialize` request, but has not yet responded.
13    Initializing = 1,
14    /// Server received and responded success to an `initialize` request.
15    Initialized = 2,
16    /// Server received a `shutdown` request.
17    ShutDown = 3,
18    /// Server received an `exit` notification.
19    Exited = 4,
20}
21
22/// Atomic value which represents the current state of the server.
23pub struct ServerState(AtomicU8);
24
25impl ServerState {
26    pub const fn new() -> Self {
27        ServerState(AtomicU8::new(State::Uninitialized as u8))
28    }
29
30    pub fn set(&self, state: State) {
31        self.0.store(state as u8, Ordering::SeqCst);
32    }
33
34    pub fn get(&self) -> State {
35        match self.0.load(Ordering::SeqCst) {
36            0 => State::Uninitialized,
37            1 => State::Initializing,
38            2 => State::Initialized,
39            3 => State::ShutDown,
40            4 => State::Exited,
41            _ => unreachable!(),
42        }
43    }
44}
45
46impl Debug for ServerState {
47    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
48        self.get().fmt(f)
49    }
50}