dadk_config/common/
task.rs1use anyhow::{Error, Result};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5#[derive(Debug, Serialize, Deserialize, PartialEq)]
6pub struct TaskSource {
7 #[serde(rename = "type")]
8 pub source_type: TaskSourceType,
9 pub source: Source,
10 #[serde(rename = "source-path")]
11 pub source_path: String,
12 pub branch: Option<String>,
14 pub revision: Option<String>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
20pub enum TaskSourceType {
21 #[serde(rename = "build-from-source")]
23 BuildFromSource,
24 #[serde(rename = "install-from-prebuilt")]
26 InstallFromPrebuilt,
27}
28
29#[derive(Debug, Serialize, Deserialize, PartialEq)]
31pub enum Source {
32 #[serde(rename = "git")]
34 Git,
35 #[serde(rename = "local")]
37 Local,
38 #[serde(rename = "archive")]
40 Archive,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
45pub struct BuildConfig {
46 #[serde(rename = "build-command")]
48 pub build_command: Option<String>,
49 #[serde(rename = "pre-build")]
51 pub pre_build: Option<PathBuf>,
52 #[serde(rename = "post-build")]
53 pub post_build: Option<PathBuf>,
55}
56
57impl BuildConfig {
58 #[allow(dead_code)]
59 pub fn new(
60 build_command: Option<String>,
61 pre_build: Option<PathBuf>,
62 post_build: Option<PathBuf>,
63 ) -> Self {
64 Self {
65 build_command,
66 pre_build,
67 post_build,
68 }
69 }
70
71 pub fn validate(&self) -> Result<()> {
72 return Ok(());
73 }
74
75 pub fn trim(&mut self) {
76 if let Some(build_command) = &mut self.build_command {
77 *build_command = build_command.trim().to_string();
78 }
79 }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
83pub struct InstallConfig {
84 #[serde(rename = "in-dragonos-path")]
86 pub in_dragonos_path: Option<PathBuf>,
87}
88
89impl InstallConfig {
90 #[allow(dead_code)]
91 pub fn new(in_dragonos_path: Option<PathBuf>) -> Self {
92 Self { in_dragonos_path }
93 }
94
95 pub fn validate(&self) -> Result<()> {
96 if self.in_dragonos_path.is_none() {
97 return Ok(());
98 }
99 if self.in_dragonos_path.as_ref().unwrap().is_relative() {
100 return Err(Error::msg(
101 "InstallConfig: in_dragonos_path should be an Absolute path",
102 ));
103 }
104 return Ok(());
105 }
106
107 pub fn trim(&mut self) {}
108}
109#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
111pub struct CleanConfig {
112 #[serde(rename = "clean-command")]
114 pub clean_command: Option<String>,
115}
116
117impl CleanConfig {
118 #[allow(dead_code)]
119 pub fn new(clean_command: Option<String>) -> Self {
120 Self { clean_command }
121 }
122
123 pub fn validate(&self) -> Result<()> {
124 return Ok(());
125 }
126
127 pub fn trim(&mut self) {
128 if let Some(clean_command) = &mut self.clean_command {
129 *clean_command = clean_command.trim().to_string();
130 }
131 }
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
136pub struct Dependency {
137 #[serde(default = "default_empty_string")]
138 pub name: String,
139 #[serde(default = "default_empty_string")]
140 pub version: String,
141}
142
143impl Dependency {
144 #[allow(dead_code)]
145 pub fn new(name: String, version: String) -> Self {
146 Self { name, version }
147 }
148
149 pub fn validate(&self) -> Result<()> {
150 if self.name.is_empty() {
151 return Err(Error::msg("name is empty"));
152 }
153 if self.version.is_empty() {
154 return Err(Error::msg("version is empty"));
155 }
156 return Ok(());
157 }
158
159 pub fn trim(&mut self) {
160 self.name = self.name.trim().to_string();
161 self.version = self.version.trim().to_string();
162 }
163
164 pub fn name_version(&self) -> String {
165 return format!("{}-{}", self.name, self.version);
166 }
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
173pub struct TaskEnv {
174 #[serde(default = "default_empty_string")]
175 pub key: String,
176 #[serde(default = "default_empty_string")]
177 pub value: String,
178}
179
180impl TaskEnv {
181 #[allow(dead_code)]
182 pub fn new(key: String, value: String) -> Self {
183 Self { key, value }
184 }
185
186 pub fn key(&self) -> &str {
187 &self.key
188 }
189
190 pub fn value(&self) -> &str {
191 &self.value
192 }
193
194 pub fn trim(&mut self) {
195 self.key = self.key.trim().to_string();
196 self.value = self.value.trim().to_string();
197 }
198
199 pub fn validate(&self) -> Result<()> {
200 if self.key.is_empty() {
201 return Err(Error::msg("Env: key is empty"));
202 }
203 return Ok(());
204 }
205}
206
207fn default_empty_string() -> String {
208 "".to_string()
209}