baobao_ir/
types.rs

1//! Core type definitions.
2
3use serde::Serialize;
4
5use crate::{PoolConfig, SqliteOptions};
6
7/// Database type for context fields.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
9pub enum DatabaseType {
10    Postgres,
11    Mysql,
12    Sqlite,
13}
14
15impl DatabaseType {
16    /// Get the lowercase string representation.
17    pub fn as_str(&self) -> &'static str {
18        match self {
19            DatabaseType::Postgres => "postgres",
20            DatabaseType::Mysql => "mysql",
21            DatabaseType::Sqlite => "sqlite",
22        }
23    }
24}
25
26/// Context field type - language-agnostic representation.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
28pub enum ContextFieldType {
29    /// Database connection pool.
30    Database(DatabaseType),
31    /// HTTP client.
32    Http,
33}
34
35impl ContextFieldType {
36    /// Returns true if this field type requires async initialization.
37    pub fn is_async(&self) -> bool {
38        matches!(self, ContextFieldType::Database(_))
39    }
40}
41
42/// Info about a context field for code generation.
43#[derive(Debug, Clone, Serialize)]
44pub struct ContextFieldInfo {
45    /// Field name in the context struct.
46    pub name: String,
47    /// Language-agnostic field type.
48    pub field_type: ContextFieldType,
49    /// Environment variable for configuration.
50    pub env_var: String,
51    /// Whether initialization is async.
52    pub is_async: bool,
53    /// Connection pool configuration.
54    pub pool: PoolConfig,
55    /// SQLite-specific options.
56    pub sqlite: Option<SqliteOptions>,
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_database_type_as_str() {
65        assert_eq!(DatabaseType::Postgres.as_str(), "postgres");
66        assert_eq!(DatabaseType::Mysql.as_str(), "mysql");
67        assert_eq!(DatabaseType::Sqlite.as_str(), "sqlite");
68    }
69
70    #[test]
71    fn test_context_field_type_is_async() {
72        assert!(ContextFieldType::Database(DatabaseType::Postgres).is_async());
73        assert!(ContextFieldType::Database(DatabaseType::Mysql).is_async());
74        assert!(ContextFieldType::Database(DatabaseType::Sqlite).is_async());
75        assert!(!ContextFieldType::Http.is_async());
76    }
77}