Skip to main content

routa_core/models/
codebase.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use std::str::FromStr;
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum CodebaseSourceType {
8    Local,
9    Github,
10}
11
12impl CodebaseSourceType {
13    pub fn as_str(&self) -> &'static str {
14        match self {
15            Self::Local => "local",
16            Self::Github => "github",
17        }
18    }
19}
20
21impl FromStr for CodebaseSourceType {
22    type Err = ();
23
24    fn from_str(value: &str) -> Result<Self, Self::Err> {
25        match value {
26            "local" => Ok(Self::Local),
27            "github" => Ok(Self::Github),
28            _ => Err(()),
29        }
30    }
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(rename_all = "camelCase")]
35pub struct Codebase {
36    pub id: String,
37    pub workspace_id: String,
38    pub repo_path: String,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub branch: Option<String>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub label: Option<String>,
43    pub is_default: bool,
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub source_type: Option<CodebaseSourceType>,
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub source_url: Option<String>,
48    pub created_at: DateTime<Utc>,
49    pub updated_at: DateTime<Utc>,
50}
51
52impl Codebase {
53    #[allow(clippy::too_many_arguments)]
54    pub fn new(
55        id: String,
56        workspace_id: String,
57        repo_path: String,
58        branch: Option<String>,
59        label: Option<String>,
60        is_default: bool,
61        source_type: Option<CodebaseSourceType>,
62        source_url: Option<String>,
63    ) -> Self {
64        let now = Utc::now();
65        Self {
66            id,
67            workspace_id,
68            repo_path,
69            branch,
70            label,
71            is_default,
72            source_type,
73            source_url,
74            created_at: now,
75            updated_at: now,
76        }
77    }
78}