fv-connect 0.1.0

FusionVault Conduit: read relational, file and REST sources into rows with mandatory access control pushed into the read, write results back with atomic promotion, and drive incremental base-ingest scans, changelogs and label normalisation checked against the published conformance vectors.
Documentation
//! Connector config → DSN. Mirrors the control plane's connector registry record
//! (type + config{host,port,database,user,schema}) plus the vaulted secret (resolved out-of-band via
//! `FV_CONNECTOR_<NAME>_<KEY>`, never returned by a read API). The DSN builders are PURE +
//! unit-tested; fetching the record and looking up the secret is the caller's IO.

/// A connector's non-secret configuration, as stored in the control plane's connector registry. Relational drivers use the
/// host/port/database/user fields; non-relational drivers (e.g. the generic REST connector) carry their
/// shape-specific settings in `options` (url, recordsPath, fields, pollIntervalSec, …).
#[derive(Debug, Clone, Default)]
pub struct ConnectorConfig {
    pub kind: String, // POSTGRES | MYSQL | REST | REST_POLL | ...
    pub host: String,
    pub port: u16,
    pub database: String,
    pub user: String,
    /// Free-form driver-specific config (the connector record's `config` object). Null for the
    /// relational drivers; the REST driver reads `url`/`recordsPath`/`fields`/… from here.
    pub options: serde_json::Value,
}

/// Minimal percent-encoding for the userinfo of a URL DSN (so a password with `@ : / %` is safe).
fn enc(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => out.push(b as char),
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

impl ConnectorConfig {
    /// A tokio-postgres URL DSN. Plaintext for local dev (`sslmode=disable`).
    pub fn postgres_dsn(&self, password: &str) -> String {
        format!(
            "postgresql://{}:{}@{}:{}/{}?sslmode=disable",
            enc(&self.user),
            enc(password),
            self.host,
            self.port,
            self.database
        )
    }

    /// A mysql_async URL.
    pub fn mysql_url(&self, password: &str) -> String {
        format!(
            "mysql://{}:{}@{}:{}/{}",
            enc(&self.user),
            enc(password),
            self.host,
            self.port,
            self.database
        )
    }
}

/// The env var a connector's secret is resolved from (the vault stand-in): `FV_CONNECTOR_<NAME>_<KEY>`
/// upper-cased, non-alphanumerics → `_`.
pub fn secret_env_var(connector_name: &str, key: &str) -> String {
    let up = |s: &str| {
        s.chars()
            .map(|c| {
                if c.is_ascii_alphanumeric() {
                    c.to_ascii_uppercase()
                } else {
                    '_'
                }
            })
            .collect::<String>()
    };
    format!("FV_CONNECTOR_{}_{}", up(connector_name), up(key))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn pg() -> ConnectorConfig {
        ConnectorConfig {
            kind: "POSTGRES".into(),
            host: "localhost".into(),
            port: 5432,
            database: "shop".into(),
            user: "app".into(),
            ..Default::default()
        }
    }

    #[test]
    fn postgres_dsn_shape() {
        assert_eq!(
            pg().postgres_dsn("app-secret"),
            "postgresql://app:app-secret@localhost:5432/shop?sslmode=disable"
        );
    }

    #[test]
    fn mysql_url_shape() {
        let c = ConnectorConfig {
            kind: "MYSQL".into(),
            host: "127.0.0.1".into(),
            port: 3307,
            database: "billing".into(),
            user: "app".into(),
            ..Default::default()
        };
        assert_eq!(c.mysql_url("mysqlpw"), "mysql://app:mysqlpw@127.0.0.1:3307/billing");
    }

    #[test]
    fn password_special_chars_are_encoded() {
        assert_eq!(
            pg().postgres_dsn("p@ss:w/rd"),
            "postgresql://app:p%40ss%3Aw%2Frd@localhost:5432/shop?sslmode=disable"
        );
    }

    #[test]
    fn secret_env_var_matches_the_funnel_convention() {
        assert_eq!(
            secret_env_var("shop_orders_pg", "password"),
            "FV_CONNECTOR_SHOP_ORDERS_PG_PASSWORD"
        );
    }
}