use crate::rfc3339;
use crate::types::{
HttpEndpoint, HttpEndpointProtocol, KbDescription, KbEndpoint, KbRead, KbTarget, KnowledgeBase,
};
use std::time::SystemTime;
impl HttpEndpointProtocol {
pub fn of_host(host: &str) -> HttpEndpointProtocol {
if host == "localhost" || host == "127.0.0.1" {
HttpEndpointProtocol::Http
} else {
HttpEndpointProtocol::Https
}
}
}
impl HttpEndpoint {
pub fn gateway_url(&self) -> Result<String, String> {
if !is_valid_hostname(&self.host) {
return Err(format!("Invalid KB hostname: \"{}\"", self.host));
}
Ok(format!(
"{}://{}:{}",
self.protocol.as_str(),
self.host.to_ascii_lowercase(),
self.port
))
}
pub fn same_address(&self, other: &HttpEndpoint) -> bool {
self.protocol == other.protocol
&& self.host.eq_ignore_ascii_case(&other.host)
&& self.port == other.port
}
}
pub fn is_valid_hostname(host: &str) -> bool {
!host.is_empty()
&& host.split('.').all(|label| {
(1..=63).contains(&label.len())
&& label
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-')
&& !label.starts_with('-')
&& !label.ends_with('-')
})
}
impl KbEndpoint {
pub fn http(&self) -> Option<&HttpEndpoint> {
match self {
KbEndpoint::HttpEndpoint(endpoint) => Some(endpoint),
KbEndpoint::LocalEndpoint(_) => None,
}
}
pub const fn kind(&self) -> &'static str {
match self {
KbEndpoint::HttpEndpoint(endpoint) => endpoint.kind.as_str(),
KbEndpoint::LocalEndpoint(endpoint) => endpoint.kind.as_str(),
}
}
}
impl KbTarget {
pub fn http(
id: &str,
label: &str,
host: &str,
port: u16,
protocol: HttpEndpointProtocol,
) -> KbTarget {
KbTarget {
id: id.to_owned(),
label: label.to_owned(),
endpoint: KbEndpoint::HttpEndpoint(HttpEndpoint::new(host, port, protocol)),
}
}
}
impl KbRead {
pub fn of(description: &KbDescription, at: SystemTime) -> KbRead {
KbRead {
at: rfc3339::to_the_millisecond(at),
git_branch: description.git_branch.clone(),
}
}
}
impl KnowledgeBase {
pub fn target(&self) -> KbTarget {
KbTarget {
id: self.id.clone(),
label: self.label.clone(),
endpoint: self.endpoint.clone(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KbSessionStatus {
Authenticated,
Expired,
SignedOut,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::Value;
use std::time::{Duration, UNIX_EPOCH};
const GATEWAY_URL_CASES: &str = include_str!("../../specs/session/gateway-url-cases.json");
#[test]
fn a_gateways_origin_is_as_the_shared_table_states() {
let table: Value = serde_json::from_str(GATEWAY_URL_CASES).expect("the table is JSON");
let cases = table["cases"].as_array().expect("the table has cases");
assert!(!cases.is_empty());
for case in cases {
let why = case["why"].as_str().expect("a case says why");
let endpoint: HttpEndpoint =
serde_json::from_value(case["endpoint"].clone()).expect("an HTTP endpoint");
let expected = match case["origin"].as_str() {
Some(origin) => Ok(origin.to_owned()),
None => Err(format!("Invalid KB hostname: \"{}\"", endpoint.host)),
};
assert_eq!(endpoint.gateway_url(), expected, "{why}");
}
}
#[test]
fn a_read_is_written_with_its_time_as_typescript_writes_one() {
let description: KbDescription = serde_json::from_value(serde_json::json!({
"name": "A knowledge base",
"domain": "example.org",
"gitBranch": "main",
}))
.expect("a description");
let read = KbRead::of(
&description,
UNIX_EPOCH + Duration::new(1_800_000_000, 123_456_789),
);
assert_eq!(read.at, "2027-01-15T08:00:00.123Z");
assert_eq!(read.git_branch.as_deref(), Some("main"));
}
}