Skip to main content

conduit_cli/core/
io.rs

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