1use std::fs;
2use std::fs::OpenOptions;
3use std::io::Read;
4use std::io::Write;
5use std::path::Path;
6
7use serde::{Deserialize, Serialize};
8
9use smplx_regtest::RegtestConfig;
10
11use super::error::TestError;
12
13pub const TEST_ENV_NAME: &str = "SIMPLEX_TEST_ENV";
14pub const DEFAULT_TEST_MNEMONIC: &str = "exist carry drive collect lend cereal occur much tiger just involve mean";
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(default)]
18pub struct TestConfig {
19 pub mnemonic: String,
20 pub esplora: Option<EsploraConfig>,
21 pub rpc: Option<RpcConfig>,
22}
23
24#[derive(Debug, Default, Clone, Serialize, Deserialize)]
25pub struct EsploraConfig {
26 pub url: String,
27 pub network: String,
28}
29
30#[derive(Debug, Default, Clone, Serialize, Deserialize)]
31pub struct RpcConfig {
32 pub url: String,
33 pub username: String,
34 pub password: String,
35}
36
37impl TestConfig {
38 pub fn from_file(path: impl AsRef<Path>) -> Result<Self, TestError> {
39 let mut content = String::new();
40 let mut file = OpenOptions::new().read(true).open(path)?;
41
42 file.read_to_string(&mut content)?;
43
44 Ok(toml::from_str(&content)?)
46 }
47
48 pub fn to_regtest_config(&self) -> RegtestConfig {
49 RegtestConfig {
50 mnemonic: self.mnemonic.clone(),
51 }
52 }
53
54 pub fn to_file(&self, path: &impl AsRef<Path>) -> Result<(), TestError> {
55 if let Some(parent_dir) = path.as_ref().parent() {
56 fs::create_dir_all(parent_dir)?;
57 }
58
59 let mut file = OpenOptions::new().create(true).write(true).truncate(true).open(&path)?;
60
61 file.write(toml::to_string_pretty(&self).unwrap().as_bytes())?;
62 file.flush()?;
63
64 Ok(())
65 }
66}
67
68impl Default for TestConfig {
69 fn default() -> Self {
70 Self {
71 mnemonic: DEFAULT_TEST_MNEMONIC.to_string(),
72 esplora: None,
73 rpc: None,
74 }
75 }
76}