use bson::Document;
use crate::error::KeyAccessError;
use crate::error::ValueAccessResultExt;
#[derive(Debug, Clone)]
pub struct Metadata {
pub host: String,
pub process: String,
pub version: String,
}
impl Metadata {
const COMMON_KEY: &str = "common";
const SERVER_STATUS_KEY: &str = "serverStatus";
const HOST_KEY: &str = "host";
const PROCESS_KEY: &str = "process";
const VERSION_KEY: &str = "version";
pub(crate) fn from_reference_document(doc: &Document) -> Result<Metadata, KeyAccessError> {
let common = match doc.get_document(Self::COMMON_KEY) {
Ok(common) => common,
Err(_) => doc,
};
let server_status = common
.get_document(Self::SERVER_STATUS_KEY)
.map_value_access_err(Self::SERVER_STATUS_KEY)?;
let metadata = Self {
host: server_status
.get_str(Self::HOST_KEY)
.map_value_access_err(Self::HOST_KEY)?
.to_owned(),
process: server_status
.get_str(Self::PROCESS_KEY)
.map_value_access_err(Self::PROCESS_KEY)?
.to_owned(),
version: server_status
.get_str(Self::VERSION_KEY)
.map_value_access_err(Self::VERSION_KEY)?
.to_owned(),
};
Ok(metadata)
}
}