fauna-api 1.0.765

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
use std::sync::{LazyLock, PoisonError, RwLock};

/// The `PREFIX`, `IP` and `PORT` every TypeScript namespace keeps as module
/// state. `set` follows the same split rules as `setRoute`: a missing port
/// keeps the previous one, and a missing scheme keeps the previous prefix.
#[derive(Clone)]
pub(crate) struct Route {
    prefix: String,
    ip: String,
    port: u16,
}

impl Default for Route {
    fn default() -> Self {
        Route {
            prefix: "https://".to_string(),
            ip: "api.faunakit.com".to_string(),
            port: 443,
        }
    }
}

impl Route {
    fn set(&mut self, route: &str) {
        let parts_0: Vec<&str> = route.split("//").collect();
        let host = if parts_0.len() > 1 {
            self.prefix = format!("{}//", parts_0[0]);
            parts_0[1]
        } else {
            parts_0[0]
        };
        let parts_1: Vec<&str> = host.split(':').collect();
        self.ip = parts_1[0].to_string();
        if parts_1.len() > 1 {
            self.port = parts_1[1].parse().unwrap_or(0);
        }
    }
}

pub(crate) type Cell = LazyLock<RwLock<Route>>;

pub(crate) const fn cell() -> Cell {
    LazyLock::new(|| RwLock::new(Route::default()))
}

pub(crate) fn set(cell: &Cell, route: &str) {
    cell.write()
        .unwrap_or_else(PoisonError::into_inner)
        .set(route);
}

fn read(cell: &Cell) -> Route {
    cell.read().unwrap_or_else(PoisonError::into_inner).clone()
}

pub(crate) fn ip(cell: &Cell) -> String {
    read(cell).ip
}

pub(crate) fn port(cell: &Cell) -> u16 {
    read(cell).port
}

pub(crate) fn route(cell: &Cell) -> String {
    let route = read(cell);
    format!("{}:{}", route.ip, route.port)
}

pub(crate) fn url(cell: &Cell) -> String {
    let route = read(cell);
    format!("{}{}:{}", route.prefix, route.ip, route.port)
}