use std::sync::Weak;
use crate::debug::{Diagnostic, DiagnosticRegistry};
use bytes::Bytes;
use conjure_error::Error;
use conjure_serde::json;
use http::HeaderValue;
const DIAGNOSTIC_TYPES_V1: &str = "diagnostic.types.v1";
pub struct DiagnosticTypesDiagnostic {
registry: Weak<DiagnosticRegistry>,
}
impl DiagnosticTypesDiagnostic {
pub fn new(registry: Weak<DiagnosticRegistry>) -> Self {
DiagnosticTypesDiagnostic { registry }
}
}
impl Diagnostic for DiagnosticTypesDiagnostic {
fn type_(&self) -> &str {
DIAGNOSTIC_TYPES_V1
}
fn content_type(&self) -> HeaderValue {
HeaderValue::from_static("application/json")
}
fn safe_loggable(&self) -> bool {
true
}
fn result(&self) -> Result<Bytes, Error> {
let mut types: Vec<String> = Vec::new();
types.push(DIAGNOSTIC_TYPES_V1.to_string());
if let Some(registry) = self.registry.upgrade() {
types.extend(registry.diagnostics.lock().keys().cloned());
}
types.sort_unstable();
Ok(Bytes::from(json::to_vec(&types).unwrap()).clone())
}
}