1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub enum SemanticBackendAvailability {
6 Available,
7 Unavailable,
8}
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct SemanticBackendStatus {
12 pub availability: SemanticBackendAvailability,
13 pub runtime: String,
14 pub reason: Option<String>,
15}
16
17impl SemanticBackendStatus {
18 #[must_use]
19 pub fn available(runtime: &str) -> Self {
20 Self {
21 availability: SemanticBackendAvailability::Available,
22 runtime: runtime.to_string(),
23 reason: None,
24 }
25 }
26
27 #[must_use]
28 pub fn unavailable(runtime: &str, reason: impl Into<String>) -> Self {
29 Self {
30 availability: SemanticBackendAvailability::Unavailable,
31 runtime: runtime.to_string(),
32 reason: Some(reason.into()),
33 }
34 }
35
36 #[must_use]
37 pub fn probe() -> Self {
38 Self::probe_for_runtime("current")
39 }
40
41 #[must_use]
42 pub fn probe_for_runtime(runtime: &str) -> Self {
43 Self {
44 availability: SemanticBackendAvailability::Unavailable,
45 runtime: runtime.to_string(),
46 reason: Some(format!(
47 "semantic backend unavailable on `{runtime}` because rscheck-cli was built without a nightly semantic implementation"
48 )),
49 }
50 }
51
52 #[must_use]
53 pub fn is_available(&self) -> bool {
54 self.availability == SemanticBackendAvailability::Available
55 }
56}