use std::str::FromStr;
use crate::Error;
#[derive(Debug, PartialEq)]
pub struct Readyz {
pub is_node_ok: bool,
pub is_leader_ok: bool,
pub is_store_ok: bool,
}
const MAX_READYZ_LINE_LEN: usize = 12;
impl FromStr for Readyz {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut readyz = Readyz {
is_node_ok: false,
is_leader_ok: false,
is_store_ok: false,
};
let lines = s.lines();
for line in lines {
if line.len() > MAX_READYZ_LINE_LEN || !line.starts_with('[') {
return Err("parse readyz failed".into());
}
if line.ends_with("node ok") {
readyz.is_node_ok = true;
} else if line.ends_with("leader ok") {
readyz.is_leader_ok = true;
} else if line.ends_with("store ok") {
readyz.is_store_ok = true;
} else {
return Err("readyz unknown line".into());
}
}
Ok(readyz)
}
}