stygian_graph/
domain.rs

1//! Domain layer - core business logic
2//!
3//! Contains pure business logic with no infrastructure dependencies.
4//! Domain layer only imports from ports (trait definitions), never from adapters.
5
6/// DAG execution engine using petgraph
7pub mod graph;
8
9/// Pipeline types with typestate pattern
10pub mod pipeline;
11
12/// Worker pool executor with backpressure
13pub mod executor;
14
15/// Idempotency tracking system
16pub mod idempotency;
17
18/// Domain error types
19pub mod error {
20    use thiserror::Error;
21
22    /// Primary error type for the stygian-graph crate
23    ///
24    /// Encompasses all domain-level errors following hexagonal architecture principles.
25    ///
26    /// # Example
27    ///
28    /// ```
29    /// use stygian_graph::domain::error::{StygianError, GraphError};
30    ///
31    /// fn validate_pipeline() -> Result<(), StygianError> {
32    ///     Err(StygianError::Graph(GraphError::CycleDetected))
33    /// }
34    /// ```
35    #[derive(Debug, Error)]
36    pub enum StygianError {
37        /// Graph execution errors
38        #[error("Graph error: {0}")]
39        Graph(#[from] GraphError),
40
41        /// Service interaction errors
42        #[error("Service error: {0}")]
43        Service(#[from] ServiceError),
44
45        /// AI provider errors
46        #[error("Provider error: {0}")]
47        Provider(#[from] ProviderError),
48
49        /// Configuration errors
50        #[error("Config error: {0}")]
51        Config(#[from] ConfigError),
52
53        /// Rate limiting errors
54        #[error("Rate limit error: {0}")]
55        RateLimit(#[from] RateLimitError),
56
57        /// Caching errors
58        #[error("Cache error: {0}")]
59        Cache(#[from] CacheError),
60    }
61
62    /// Graph-specific errors
63    #[derive(Debug, Error)]
64    pub enum GraphError {
65        /// Graph contains a cycle (DAG violation)
66        #[error("Graph contains cycle, cannot execute")]
67        CycleDetected,
68
69        /// Invalid node reference
70        #[error("Node not found: {0}")]
71        NodeNotFound(String),
72
73        /// Invalid edge configuration
74        #[error("Invalid edge: {0}")]
75        InvalidEdge(String),
76
77        /// Pipeline validation failed
78        #[error("Invalid pipeline: {0}")]
79        InvalidPipeline(String),
80
81        /// Execution failed
82        #[error("Execution failed: {0}")]
83        ExecutionFailed(String),
84    }
85
86    /// Service-level errors
87    #[derive(Debug, Error)]
88    pub enum ServiceError {
89        /// Service unavailable
90        #[error("Service unavailable: {0}")]
91        Unavailable(String),
92
93        /// Service timeout
94        #[error("Service timeout after {0}ms")]
95        Timeout(u64),
96
97        /// Service returned invalid response
98        #[error("Invalid response: {0}")]
99        InvalidResponse(String),
100
101        /// Service authentication failed
102        #[error("Authentication failed: {0}")]
103        AuthenticationFailed(String),
104
105        /// Service is rate-limiting; caller should retry after the given delay
106        #[error("Rate limited, retry after {retry_after_ms}ms")]
107        RateLimited {
108            /// Milliseconds to wait before retrying
109            retry_after_ms: u64,
110        },
111    }
112
113    /// AI provider errors
114    #[derive(Debug, Error)]
115    pub enum ProviderError {
116        /// Provider API error
117        #[error("API error: {0}")]
118        ApiError(String),
119
120        /// Invalid API key or credentials
121        #[error("Invalid credentials")]
122        InvalidCredentials,
123
124        /// Token limit exceeded
125        #[error("Token limit exceeded: {0}")]
126        TokenLimitExceeded(String),
127
128        /// Model not available
129        #[error("Model not available: {0}")]
130        ModelUnavailable(String),
131
132        /// Content policy violation
133        #[error("Content policy violation: {0}")]
134        ContentPolicyViolation(String),
135    }
136
137    /// Configuration errors
138    #[derive(Debug, Error)]
139    pub enum ConfigError {
140        /// Missing required configuration
141        #[error("Missing required config: {0}")]
142        MissingConfig(String),
143
144        /// Invalid configuration value
145        #[error("Invalid config value for '{key}': {reason}")]
146        InvalidValue {
147            /// Configuration key
148            key: String,
149            /// Reason for invalidity
150            reason: String,
151        },
152
153        /// Configuration file error
154        #[error("Config file error: {0}")]
155        FileError(String),
156
157        /// TOML parsing error
158        #[error("TOML parse error: {0}")]
159        ParseError(String),
160    }
161
162    /// Rate limiting errors
163    #[derive(Debug, Error)]
164    pub enum RateLimitError {
165        /// Rate limit exceeded
166        #[error("Rate limit exceeded: {0} requests per {1} seconds")]
167        Exceeded(u32, u32),
168
169        /// Quota exhausted
170        #[error("Quota exhausted: {0}")]
171        QuotaExhausted(String),
172
173        /// Retry after duration
174        #[error("Retry after {0} seconds")]
175        RetryAfter(u64),
176    }
177
178    /// Caching errors
179    #[derive(Debug, Error)]
180    pub enum CacheError {
181        /// Cache miss
182        #[error("Cache miss: {0}")]
183        Miss(String),
184
185        /// Cache write failed
186        #[error("Cache write failed: {0}")]
187        WriteFailed(String),
188
189        /// Cache read failed
190        #[error("Cache read failed: {0}")]
191        ReadFailed(String),
192
193        /// Cache eviction failed
194        #[error("Cache eviction failed: {0}")]
195        EvictionFailed(String),
196
197        /// Cache corrupted
198        #[error("Cache corrupted: {0}")]
199        Corrupted(String),
200    }
201
202    /// Domain result type using `StygianError`
203    pub type Result<T> = std::result::Result<T, StygianError>;
204
205    /// Legacy domain error (kept for backward compatibility)
206    #[derive(Debug, Error)]
207    pub enum DomainError {
208        /// Graph contains cycle
209        #[error("Graph contains cycle, cannot execute")]
210        CycleDetected,
211
212        /// Invalid pipeline configuration
213        #[error("Invalid pipeline: {0}")]
214        InvalidPipeline(String),
215
216        /// Execution failed
217        #[error("Execution failed: {0}")]
218        ExecutionFailed(String),
219
220        /// Service error
221        #[error("Service error: {0}")]
222        ServiceError(String),
223    }
224
225    /// Legacy domain result type
226    pub type DomainResult<T> = std::result::Result<T, DomainError>;
227}