architect_sdk/
lib.rs

1use anyhow::{bail, Result};
2use std::path::{Path, PathBuf};
3
4#[cfg(feature = "grpc")]
5pub mod client;
6pub mod discrete_clock;
7#[cfg(feature = "grpc")]
8pub mod grpc;
9pub mod marketdata;
10pub mod order_id_allocator;
11#[cfg(feature = "grpc")]
12pub mod symbology;
13pub mod synced;
14
15#[cfg(feature = "grpc")]
16pub use client::Architect;
17pub use marketdata::MarketdataSource;
18
19/// Canonical config file location resolution.
20pub fn locate_config(from_arg: Option<impl AsRef<Path>>) -> Result<PathBuf> {
21    const FROM_ENV: &[&str] = &["ARCHITECT_CFG"];
22    const DEFAULT_CONFIG: Option<&str> = Some("~/.architect/config.yml");
23    if let Some(path) = from_arg {
24        return Ok(path.as_ref().to_owned());
25    }
26    for env in FROM_ENV {
27        if let Ok(path) = std::env::var(env) {
28            return Ok(path.into());
29        }
30    }
31    if let Some(path) = DEFAULT_CONFIG {
32        if let Some(path) = path.strip_prefix("~/") {
33            if let Some(home_dir) = dirs::home_dir() {
34                return Ok(home_dir.join(path));
35            }
36        } else {
37            return Ok(path.to_owned().into());
38        }
39    }
40    bail!("config file was required but not specified");
41}
42
43#[cfg(feature = "yaml")]
44pub fn load_config(from_arg: Option<impl AsRef<Path>>) -> Result<architect_api::Config> {
45    use anyhow::Context;
46    let path = locate_config(from_arg)?;
47    let config_s = std::fs::read_to_string(&path)
48        .with_context(|| format!("while reading file: {}", path.display()))?;
49    let config: architect_api::Config = serde_yaml::from_str(&config_s)
50        .with_context(|| format!("while parsing file: {}", path.display()))?;
51    Ok(config)
52}