use std::io::Write as _;
use shep_core::dogs::{SCHEMA_FLAG, SHEP_CHANNEL_KEY, SHEP_PROTOCOL_KEY, VERSION_FLAG};
use super::DogConfig;
#[cfg(feature = "schema")]
use super::{config_schema, config_schema_with_sheep};
#[cfg(feature = "schema")]
pub fn probe<T: DogConfig + schemars::JsonSchema>(name: &str, version: &str) {
Probe::new(name, version).answer::<T>();
}
#[cfg(feature = "schema")]
pub fn probe_with_sheep<T, S>(name: &str, version: &str)
where
T: DogConfig + schemars::JsonSchema,
S: DogConfig + schemars::JsonSchema,
{
Probe::new(name, version).answer_with_sheep::<T, S>();
}
#[cfg(not(feature = "schema"))]
pub fn probe<T: DogConfig>(name: &str, version: &str) {
Probe::new(name, version).answer::<T>();
}
#[cfg(not(feature = "schema"))]
pub fn probe_with_sheep<T: DogConfig, S: DogConfig>(name: &str, version: &str) {
Probe::new(name, version).answer_with_sheep::<T, S>();
}
#[derive(Debug, Clone, Copy)]
#[must_use = "a Probe answers nothing until `answer` is called"]
pub struct Probe<'a> {
name: &'a str,
version: &'a str,
channel: bool,
}
impl<'a> Probe<'a> {
pub const fn new(name: &'a str, version: &'a str) -> Self {
Self {
name,
version,
channel: false,
}
}
pub const fn ask_for_channel(mut self) -> Self {
self.channel = true;
self
}
#[cfg(feature = "schema")]
pub fn answer<T: DogConfig + schemars::JsonSchema>(self) {
self.answering(schema_answer::<T>);
}
#[cfg(feature = "schema")]
pub fn answer_with_sheep<T, S>(self)
where
T: DogConfig + schemars::JsonSchema,
S: DogConfig + schemars::JsonSchema,
{
self.answering(schema_answer_with_sheep::<T, S>);
}
#[cfg(feature = "schema")]
fn answering(self, schema: fn() -> String) {
match first_argument().as_deref() {
Some(VERSION_FLAG) => answer(&self.version_answer()),
Some(SCHEMA_FLAG) => answer(&schema()),
_ => (),
}
}
#[cfg(not(feature = "schema"))]
pub fn answer<T: DogConfig>(self) {
match first_argument().as_deref() {
Some(VERSION_FLAG) => answer(&self.version_answer()),
Some(SCHEMA_FLAG) => std::process::exit(0),
_ => (),
}
}
#[cfg(not(feature = "schema"))]
pub fn answer_with_sheep<T: DogConfig, S: DogConfig>(self) {
self.answer::<T>();
}
fn version_answer(&self) -> String {
let mut text = format!(
"{} {}\n{SHEP_PROTOCOL_KEY}: {}\n",
self.name,
self.version,
crate::PROTOCOL_VERSION
);
if self.channel {
text.push_str(&format!("{SHEP_CHANNEL_KEY}: true\n"));
}
text
}
}
fn first_argument() -> Option<String> {
std::env::args().nth(1)
}
fn answer(text: &str) -> ! {
let mut stdout = std::io::stdout();
let _ = write!(stdout, "{text}");
let _ = stdout.flush();
std::process::exit(0);
}
#[cfg(feature = "schema")]
fn schema_answer<T: DogConfig + schemars::JsonSchema>() -> String {
rendered(&config_schema::<T>())
}
#[cfg(feature = "schema")]
fn rendered(schema: &schemars::Schema) -> String {
let json = serde_json::to_string_pretty(schema).expect("a schemars Schema always serializes");
format!("{json}\n")
}
#[cfg(feature = "schema")]
fn schema_answer_with_sheep<T, S>() -> String
where
T: DogConfig + schemars::JsonSchema,
S: DogConfig + schemars::JsonSchema,
{
rendered(&config_schema_with_sheep::<T, S>())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_version_answer_parses_with_the_shepherds_own_parser() {
let answer = Probe::new("shep-otel", "0.1.3").version_answer();
let parsed = shep_core::dogs::parse_version_answer(&answer)
.expect("the answer shep's own parser cannot read is the bug this pins");
assert_eq!(parsed.version, "0.1.3");
assert_eq!(parsed.protocol, Some(crate::PROTOCOL_VERSION));
assert!(
!parsed.channel,
"a dog that asks for nothing asks for nothing"
);
}
#[test]
fn a_channel_ask_reaches_the_shepherds_own_parser() {
let answer = Probe::new("shep-otel", "0.1.3")
.ask_for_channel()
.version_answer();
let parsed = shep_core::dogs::parse_version_answer(&answer).unwrap();
assert!(parsed.channel, "{answer}");
assert_eq!(parsed.protocol, Some(crate::PROTOCOL_VERSION));
}
}