use crate::config::{MirrorConfig, MirrorProviderSlot};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Capability {
S3,
Pg,
}
impl Capability {
pub fn wire_name(self) -> &'static str {
match self {
Self::S3 => "s3",
Self::Pg => "pg",
}
}
pub fn for_component_kind(kind: &str) -> &'static [Capability] {
match kind {
"mesofact-static" | "mesofact-spa" | "static-asset" => &[Capability::S3],
_ => &[],
}
}
}
impl MirrorConfig {
pub fn driver(&self, capability: Capability) -> Option<&MirrorProviderSlot> {
self.drivers.get(capability.wire_name())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::{MirrorShape, Provider};
fn mirror_with(toml_src: &str) -> MirrorConfig {
toml::from_str(toml_src).expect("parse mirror")
}
#[test]
fn static_shaped_kinds_imply_s3_and_nothing_else_does() {
for kind in ["mesofact-static", "mesofact-spa", "static-asset"] {
assert_eq!(
Capability::for_component_kind(kind),
&[Capability::S3],
"{kind} should imply s3"
);
}
for kind in [
"cloudflare-worker",
"mesofact-bundle",
"not-a-real-kind",
"",
] {
assert!(
Capability::for_component_kind(kind).is_empty(),
"{kind} should imply nothing"
);
}
}
#[test]
fn drivers_table_parses_and_resolves_by_capability() {
let mirror = mirror_with(
r#"
schema_version = 1
shape = "local"
[drivers.pg]
kind = "local-pg-dev"
"#,
);
assert!(matches!(mirror.shape, MirrorShape::Local));
let slot = mirror.driver(Capability::Pg).expect("pg driver bound");
assert_eq!(slot.inline_kind(), Some(Provider::LocalPgDev));
assert!(mirror.driver(Capability::S3).is_none());
}
#[test]
fn a_mirror_with_no_drivers_table_is_unchanged() {
let mirror = mirror_with(
r#"
schema_version = 1
shape = "local"
[providers.static]
kind = "local-static"
port = 4324
"#,
);
assert!(mirror.drivers.is_empty());
assert!(mirror.driver(Capability::Pg).is_none());
let back = toml::to_string(&mirror).expect("serialize");
assert!(
!back.contains("drivers"),
"unexpected drivers table: {back}"
);
}
}