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, 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    #[inline]
27    pub const fn new() -> Self {
28        ServerState(AtomicU8::new(State::Uninitialized as u8))
29    }
30
31    #[inline]
32    pub fn set(&self, state: State) {
33        self.0.store(state as u8, Ordering::SeqCst);
34    }
35
36    #[inline]
37    pub fn get(&self) -> State {
38        match self.0.load(Ordering::SeqCst) {
39            0 => State::Uninitialized,
40            1 => State::Initializing,
41            2 => State::Initialized,
42            3 => State::ShutDown,
43            4 => State::Exited,
44            _ => unreachable!(),
45        }
46    }
47}
48
49impl Debug for ServerState {
50    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
51        self.get().fmt(f)
52    }
53}