dhan_rs/lib.rs
1//! # dhan-rs
2//!
3//! An **unofficial** Rust client library for the
4//! [DhanHQ Broker API v2](https://dhanhq.co/docs/v2/).
5//!
6//! ## ⚠️ AI-Generated Code Disclaimer
7//!
8//! **This entire crate was generated by AI.** While it compiles and follows the
9//! DhanHQ API v2 specification, it has **not been extensively tested** against
10//! the live API. Before using this in production or with real money, please:
11//!
12//! - Review the source code thoroughly
13//! - Write your own integration tests
14//! - Validate all order placement, modification, and cancellation flows
15//! - Verify WebSocket market feed parsing against real data
16//!
17//! **The authors accept no responsibility for financial losses incurred through
18//! the use of this library.**
19//!
20//! ## Overview
21//!
22//! `dhan-rs` provides a complete, strongly-typed async Rust client for all
23//! DhanHQ v2 REST endpoints and WebSocket streams:
24//!
25//! - **55+ REST API methods** covering orders, portfolio, market data,
26//! historical data, option chains, funds, statements, and more
27//! - **Live Market Feed** via WebSocket with zero-copy binary packet parsing
28//! - **Live Order Updates** via WebSocket with JSON message streaming
29//! - **Rich error handling** with [`DhanError`] covering API errors, HTTP
30//! errors, JSON deserialization errors, and WebSocket errors
31//!
32//! ## Quick Start
33//!
34//! ```no_run
35//! use dhan_rs::DhanClient;
36//! use dhan_rs::types::orders::PlaceOrderRequest;
37//! use dhan_rs::types::enums::*;
38//!
39//! #[tokio::main]
40//! async fn main() -> dhan_rs::Result<()> {
41//! // Create a client with your DhanHQ credentials
42//! let client = DhanClient::new("your-client-id", "your-access-token");
43//!
44//! // Place an order
45//! let req = PlaceOrderRequest {
46//! dhan_client_id: "your-client-id".into(),
47//! correlation_id: None,
48//! transaction_type: TransactionType::BUY,
49//! exchange_segment: ExchangeSegment::NSE_EQ,
50//! product_type: ProductType::INTRADAY,
51//! order_type: OrderType::LIMIT,
52//! validity: Validity::DAY,
53//! security_id: "1333".into(),
54//! quantity: 1,
55//! price: Some(1500.0),
56//! disclosed_quantity: None,
57//! trigger_price: None,
58//! after_market_order: None,
59//! amo_time: None,
60//! bo_profit_value: None,
61//! bo_stop_loss_value: None,
62//! };
63//! let response = client.place_order(&req).await?;
64//! println!("Order placed: {:?}", response);
65//!
66//! // Fetch holdings
67//! let holdings = client.get_holdings().await?;
68//! println!("Holdings: {} instruments", holdings.len());
69//!
70//! Ok(())
71//! }
72//! ```
73//!
74//! ## WebSocket Streaming
75//!
76//! ### Market Feed (Binary)
77//!
78//! ```no_run
79//! use dhan_rs::ws::market_feed::{MarketFeedStream, Instrument};
80//! use dhan_rs::types::enums::FeedRequestCode;
81//! use futures_util::StreamExt;
82//!
83//! # #[tokio::main]
84//! # async fn main() -> dhan_rs::Result<()> {
85//! let mut stream = MarketFeedStream::connect("client-id", "token").await?;
86//!
87//! let instruments = vec![Instrument::new("NSE_EQ", "1333")];
88//! stream.subscribe(FeedRequestCode::SubscribeTicker, &instruments).await?;
89//!
90//! while let Some(event) = stream.next().await {
91//! println!("{event:?}");
92//! }
93//! # Ok(())
94//! # }
95//! ```
96//!
97//! ### Order Updates (JSON)
98//!
99//! ```no_run
100//! use dhan_rs::ws::order_update::OrderUpdateStream;
101//! use futures_util::StreamExt;
102//!
103//! # #[tokio::main]
104//! # async fn main() -> dhan_rs::Result<()> {
105//! let mut stream = OrderUpdateStream::connect("client-id", "token").await?;
106//!
107//! while let Some(msg) = stream.next().await {
108//! match msg {
109//! Ok(update) => println!("Order update: {:?}", update.Data.Status),
110//! Err(e) => eprintln!("Error: {e}"),
111//! }
112//! }
113//! # Ok(())
114//! # }
115//! ```
116//!
117//! ## Module Organization
118//!
119//! - [`client`] — The [`DhanClient`] HTTP client with authentication
120//! - [`error`] — [`DhanError`] enum and [`Result`] alias
121//! - [`constants`] — Base URLs, WebSocket URLs, rate limit values
122//! - [`types`] — Request/response structs and shared enums
123//! - [`api`] — REST endpoint implementations (methods on `DhanClient`)
124//! - [`ws`] — WebSocket streaming (market feed + order updates)
125//!
126//! ## Feature Flags
127//!
128//! This crate currently has no optional feature flags. All functionality is
129//! included by default.
130
131#![warn(missing_docs)]
132#![allow(clippy::doc_markdown)]
133#![doc(html_root_url = "https://docs.rs/dhan-rs/0.1.6")]
134
135pub mod api;
136pub mod client;
137pub mod constants;
138pub mod error;
139pub mod types;
140pub mod ws;
141
142/// Re-export the main client type at crate root for convenience.
143pub use client::DhanClient;
144/// Re-export the error type and Result alias.
145pub use error::{DhanError, Result};