#![allow(missing_docs)]
pub mod cmd;
pub mod config;
pub mod envelope;
pub mod exit;
pub mod state_store;
pub mod tempo_session;
pub mod trace;
pub mod wallet_lock;
pub use config::Config;
pub use envelope::{Envelope, ErrorPayload};
pub use exit::ExitCode;
pub use state_store::{StateAsset, StateStore};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, clap::ValueEnum)]
pub enum Environment {
Prod,
Staging,
Local,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Network {
Mainnet,
Testnet,
}
impl Network {
pub fn as_str(self) -> &'static str {
match self {
Self::Mainnet => "mainnet",
Self::Testnet => "testnet",
}
}
}
impl Environment {
pub const ALL: &'static [Environment] = &[Self::Prod, Self::Staging, Self::Local];
pub fn as_str(self) -> &'static str {
match self {
Self::Prod => "production",
Self::Staging => "staging",
Self::Local => "local",
}
}
pub fn api_base(self) -> &'static str {
match self {
Self::Prod => "https://api.taskfast.app",
Self::Staging => "https://staging.api.taskfast.app",
Self::Local => "http://127.0.0.1:4000",
}
}
pub fn network(self) -> Network {
match self {
Self::Prod => Network::Mainnet,
Self::Staging => Network::Testnet,
Self::Local => Network::Testnet,
}
}
}
pub fn well_known_api_bases() -> impl Iterator<Item = &'static str> {
Environment::ALL.iter().map(|e| e.api_base())
}
pub fn is_well_known_api_base(url: &str) -> bool {
let trimmed = url.trim_end_matches('/');
well_known_api_bases().any(|w| w.trim_end_matches('/') == trimmed)
}
pub fn accounts_url(api_base: &str) -> String {
match url::Url::parse(api_base) {
Ok(mut u) => {
let rewritten = u.host_str().and_then(strip_api_label);
if let Some(h) = rewritten {
let _ = u.set_host(Some(&h));
}
u.set_path("/accounts");
u.set_query(None);
u.set_fragment(Some("personal-api-key"));
u.to_string()
}
Err(_) => format!(
"{}/accounts#personal-api-key",
api_base.trim_end_matches('/')
),
}
}
fn strip_api_label(host: &str) -> Option<String> {
if let Some(rest) = host.strip_prefix("api.") {
return Some(rest.to_owned());
}
if host.contains(".api.") {
return Some(host.replacen(".api.", ".", 1));
}
None
}
#[cfg(test)]
mod tests {
use super::{accounts_url, is_well_known_api_base, well_known_api_bases, Environment, Network};
use std::collections::HashSet;
#[test]
fn all_covers_every_variant() {
let n = Environment::ALL.len();
let unique: HashSet<_> = Environment::ALL.iter().copied().collect();
assert_eq!(unique.len(), n, "Environment::ALL has duplicates");
for env in Environment::ALL {
let _ = env.as_str();
let _ = env.api_base();
let _ = env.network();
}
}
#[test]
fn each_env_has_unique_api_base() {
let bases: HashSet<_> = Environment::ALL.iter().map(|e| e.api_base()).collect();
assert_eq!(
bases.len(),
Environment::ALL.len(),
"two envs share an api_base"
);
}
#[test]
fn env_api_base_table_is_frozen() {
assert_eq!(Environment::Prod.api_base(), "https://api.taskfast.app");
assert_eq!(
Environment::Staging.api_base(),
"https://staging.api.taskfast.app"
);
assert_eq!(Environment::Local.api_base(), "http://127.0.0.1:4000");
}
#[test]
fn env_network_table_is_frozen() {
assert_eq!(Environment::Prod.network(), Network::Mainnet);
assert_eq!(Environment::Staging.network(), Network::Testnet);
assert_eq!(Environment::Local.network(), Network::Testnet);
}
#[test]
fn well_known_api_bases_cover_every_environment_default() {
for env in Environment::ALL {
let url = env.api_base();
assert!(
is_well_known_api_base(url),
"api_base for {env:?} ({url}) must be in well_known_api_bases"
);
}
}
#[test]
fn is_well_known_api_base_accepts_exact_defaults() {
for url in well_known_api_bases() {
assert!(is_well_known_api_base(url), "expected well-known: {url}");
}
}
#[test]
fn is_well_known_api_base_tolerates_trailing_slash() {
assert!(is_well_known_api_base("https://api.taskfast.app/"));
assert!(is_well_known_api_base("http://127.0.0.1:4000/"));
}
#[test]
fn is_well_known_api_base_rejects_attacker_hosts() {
assert!(!is_well_known_api_base("https://evil.example"));
assert!(!is_well_known_api_base(
"https://api.taskfast.app.evil.example"
));
assert!(!is_well_known_api_base("http://api.taskfast.app"));
assert!(!is_well_known_api_base("https://staging.taskfast.app"));
}
#[test]
fn accounts_url_strips_api_prefix_prod() {
assert_eq!(
accounts_url("https://api.taskfast.app"),
"https://taskfast.app/accounts#personal-api-key"
);
}
#[test]
fn accounts_url_strips_api_prefix_staging() {
assert_eq!(
accounts_url("https://staging.api.taskfast.app"),
"https://staging.taskfast.app/accounts#personal-api-key"
);
}
#[test]
fn accounts_url_passthrough_localhost_with_port() {
assert_eq!(
accounts_url("http://127.0.0.1:4000"),
"http://127.0.0.1:4000/accounts#personal-api-key"
);
}
#[test]
fn accounts_url_passthrough_bare_domain() {
assert_eq!(
accounts_url("https://taskfast.app"),
"https://taskfast.app/accounts#personal-api-key"
);
}
#[test]
fn accounts_url_replaces_existing_path_query_and_fragment() {
assert_eq!(
accounts_url("https://api.taskfast.app/v1?x=1#old-section"),
"https://taskfast.app/accounts#personal-api-key"
);
}
#[test]
fn accounts_url_malformed_falls_back_to_concat() {
assert_eq!(
accounts_url("not a url"),
"not a url/accounts#personal-api-key"
);
}
}