Skip to content

Commit 43f33c1

Browse files
committed
refactor: separate product and version config
1 parent 694e5c7 commit 43f33c1

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

rust/patchable/src/main.rs

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,8 @@ struct ProductConfig {
3232

3333
#[derive(Deserialize, Serialize)]
3434
struct ProductVersionConfig {
35-
upstream: Option<String>,
3635
#[serde(with = "utils::oid_serde")]
3736
base: Oid,
38-
mirror: Option<String>,
39-
}
40-
41-
struct MergedProductVersionConfig {
42-
upstream: String,
43-
base: Oid,
44-
mirror: Option<String>,
4537
}
4638

4739
struct ProductVersionContext {
@@ -50,47 +42,30 @@ struct ProductVersionContext {
5042
}
5143

5244
impl ProductVersionContext {
53-
fn load_product_config(&self) -> Result<ProductConfig> {
54-
let product_config_path = &self.product_config_path();
55-
45+
fn load_config<T: for<'de> Deserialize<'de>>(&self, path: &PathBuf) -> Result<T> {
5646
tracing::info!(
57-
config.path = ?product_config_path,
58-
"loading product-level config"
47+
config.path = ?path,
48+
"loading config"
5949
);
6050

61-
toml::from_str::<ProductConfig>(&std::fs::read_to_string(product_config_path).context(
51+
toml::from_str::<T>(&std::fs::read_to_string(path).context(
6252
LoadConfigSnafu {
63-
path: product_config_path,
53+
path,
6454
},
6555
)?)
6656
.context(ParseConfigSnafu {
67-
path: product_config_path,
57+
path,
6858
})
6959
}
7060

71-
fn load_version_config(&self) -> Result<MergedProductVersionConfig> {
72-
// Load product-level config (required)
73-
let product_config = self.load_product_config()?;
61+
fn load_product_config(&self) -> Result<ProductConfig> {
62+
let path = self.product_config_path();
63+
self.load_config(&path)
64+
}
7465

75-
// Load version-level config (optional)
76-
let version_config_path = &self.version_config_path();
77-
let loaded_version_config = toml::from_str::<ProductVersionConfig>(
78-
&std::fs::read_to_string(version_config_path).context(LoadConfigSnafu {
79-
path: version_config_path,
80-
})?,
81-
)
82-
.context(ParseConfigSnafu {
83-
path: version_config_path,
84-
})?;
85-
86-
// Inherit `upstream` and `mirror` from product-level config if not set in loaded version-level config
87-
Ok(MergedProductVersionConfig {
88-
upstream: loaded_version_config
89-
.upstream
90-
.unwrap_or(product_config.upstream),
91-
base: loaded_version_config.base,
92-
mirror: loaded_version_config.mirror.or(product_config.mirror),
93-
})
66+
fn load_version_config(&self) -> Result<ProductVersionConfig> {
67+
let path = self.version_config_path();
68+
self.load_config(&path)
9469
}
9570

9671
/// The root directory for files related to the product (across all versions).
@@ -334,18 +309,19 @@ fn main() -> Result<()> {
334309
pv,
335310
images_repo_root,
336311
};
337-
let config = ctx.load_version_config()?;
312+
let product_config = ctx.load_product_config()?;
313+
let version_config = ctx.load_version_config()?;
338314
let product_repo_root = ctx.product_repo();
339315
let product_repo = repo::ensure_bare_repo(&product_repo_root)
340316
.context(OpenProductRepoForCheckoutSnafu)?;
341317

342318
let base_commit = repo::resolve_and_fetch_commitish(
343319
&product_repo,
344-
&config.base.to_string(),
345-
config
320+
&version_config.base.to_string(),
321+
product_config
346322
.mirror
347323
.as_deref()
348-
.unwrap_or(&config.upstream),
324+
.unwrap_or(&product_config.upstream),
349325
)
350326
.context(FetchBaseCommitSnafu)?;
351327
let base_branch = ctx.base_branch();
@@ -532,8 +508,6 @@ fn main() -> Result<()> {
532508

533509
tracing::info!("saving version-level configuration");
534510
let config = ProductVersionConfig {
535-
upstream: None,
536-
mirror: None,
537511
base: base_commit,
538512
};
539513
let config_path = ctx.version_config_path();

0 commit comments

Comments
 (0)