na_paper_account/
lib.rs

1//! # Paper Account
2//! 
3//! `paper_account` is a Rust library for simulating trading accounts without using real money.
4//! It provides functionality for managing a paper trading account, including:
5//! 
6//! - Account creation and management
7//! - Order placement (market, limit)
8//! - Position tracking
9//! - Portfolio valuation
10//! - Trade history
11
12extern crate log;
13extern crate env_logger;
14
15use std::sync::Once;
16use log::{debug, info, LevelFilter};
17
18pub mod account;
19pub mod order;
20pub mod position;
21pub mod error;
22pub mod types;
23pub mod market;
24pub mod config;
25pub mod manager;
26
27// Re-export commonly used types
28pub use account::Account;
29pub use order::{Order, OrderType, OrderSide, OrderStatus};
30pub use position::Position;
31pub use error::Error;
32pub use types::{Symbol, Quantity, Price, TradeId, OrderId, AccountId};
33pub use config::Config;
34pub use manager::AccountManager;
35
36// Initialize configuration when the library is loaded
37#[allow(unused_variables)]
38static INIT: Once = Once::new();
39static LOGGER_INIT: Once = Once::new();
40
41/// Initialize the library with default configuration
42pub fn init() {
43    INIT.call_once(|| {
44        debug!("Library initialization with default config");
45        config::init();
46    });
47}
48
49/// Initialize the library with custom configuration
50pub fn init_with_config(config: Config) {
51    INIT.call_once(|| {
52        info!("Library initialization with custom config");
53        config::init_with_config(config);
54    });
55}
56
57/// Initialize the logger with a specific log level
58/// 
59/// This function initializes the env_logger with the specified log level.
60/// If not called, logging will not be enabled.
61/// 
62/// # Examples
63/// 
64/// ```
65/// use na_paper_account::init_logger;
66/// 
67/// // Initialize with debug level
68/// init_logger("debug");
69/// ```
70pub fn init_logger(level: &str) {
71    LOGGER_INIT.call_once(|| {
72        // Convert string level to LevelFilter
73        let level_filter = match level.to_lowercase().as_str() {
74            "trace" => LevelFilter::Trace,
75            "debug" => LevelFilter::Debug,
76            "info" => LevelFilter::Info,
77            "warn" => LevelFilter::Warn,
78            "error" => LevelFilter::Error,
79            _ => {
80                eprintln!("Invalid log level: '{}', defaulting to 'info'", level);
81                LevelFilter::Info
82            }
83        };
84        
85        let env = env_logger::Env::default()
86            .filter_or("RUST_LOG", format!("na_paper_account={}", level));
87        
88        env_logger::Builder::from_env(env)
89            .format_timestamp_millis()
90            .filter_module("na_paper_account", level_filter)
91            .init();
92        
93        info!("Logger initialized with level: {}", level);
94    });
95}