conduit_cli/core/filesystem/
io.rs1use crate::core::error::{CoreError, CoreResult};
2use crate::core::filesystem::config::ConduitConfig;
3use crate::core::filesystem::lock::ConduitLock;
4use crate::core::paths::CorePaths;
5use std::collections::BTreeMap;
6use std::fs;
7
8impl ConduitLock {
9 pub fn load_config(paths: &CorePaths) -> CoreResult<ConduitConfig> {
10 let config_content =
11 fs::read_to_string(paths.config_path()).map_err(|_| CoreError::MissingConfig)?;
12 Ok(serde_json::from_str(&config_content)?)
13 }
14
15 pub fn save_config(paths: &CorePaths, config: &ConduitConfig) -> CoreResult<()> {
16 fs::write(paths.config_path(), serde_json::to_string_pretty(config)?)?;
17 Ok(())
18 }
19
20 pub fn load_lock(paths: &CorePaths) -> CoreResult<ConduitLock> {
21 let content = fs::read_to_string(paths.lock_path()).ok();
22 if let Some(c) = content
23 && let Ok(lock) = toml::from_str(&c)
24 {
25 return Ok(lock);
26 }
27
28 Ok(ConduitLock {
29 version: 1,
30 locked_mods: std::collections::HashMap::new(),
31 loader_version: None,
32 })
33 }
34
35 #[rustfmt::skip]
36 pub fn save_lock(paths: &CorePaths, lock: &ConduitLock) -> CoreResult<()> {
37 let mut content = String::new();
38
39 content.push_str("# ---------------------------------------------------------------------------- #\n");
40 content.push_str("# #\n");
41 content.push_str("# THIS FILE IS AUTOMATICALLY GENERATED #\n");
42 content.push_str("# DO NOT EDIT THIS FILE MANUALLY #\n");
43 content.push_str("# #\n");
44 content.push_str("# ---------------------------------------------------------------------------- #\n");
45 content.push_str("# #\n");
46 content.push_str("# This file contains the precise reproduction information for the project's #\n");
47 content.push_str("# dependencies. Editing it could break your environment synchronization. #\n");
48 content.push_str("# #\n");
49 content.push_str("# ---------------------------------------------------------------------------- #\n\n");
50
51 content.push_str(&toml::to_string_pretty(lock)?);
52
53 fs::write(paths.lock_path(), content)?;
54 Ok(())
55 }
56}
57
58impl Default for ConduitConfig {
59 fn default() -> Self {
60 Self {
61 name: "conduit-server".to_string(),
62 mc_version: "1.21.1".to_string(),
63 loader: "neoforge@latest".to_string(),
64 mods: BTreeMap::new(),
65 }
66 }
67}