dusk_wallet/wallet/
file.rs1use std::fmt;
8use std::path::{Path, PathBuf};
9use std::str::FromStr;
10
11pub trait SecureWalletFile {
13 fn path(&self) -> &WalletPath;
15 fn pwd(&self) -> &[u8];
17}
18
19#[derive(PartialEq, Eq, Hash, Debug, Clone)]
21pub struct WalletPath {
22 pub wallet: PathBuf,
24 pub profile_dir: PathBuf,
26 pub network: Option<String>,
28}
29
30impl WalletPath {
31 pub fn new(wallet: &Path) -> Self {
35 let wallet = wallet.to_path_buf();
36 let mut profile_dir = wallet.clone();
38
39 profile_dir.pop();
40
41 Self {
42 wallet,
43 profile_dir,
44 network: None,
45 }
46 }
47
48 pub fn name(&self) -> Option<String> {
50 let name = self.wallet.file_stem()?.to_str()?;
52 Some(String::from(name))
53 }
54
55 pub fn dir(&self) -> Option<PathBuf> {
57 self.wallet.parent().map(PathBuf::from)
58 }
59
60 pub fn inner(&self) -> &PathBuf {
62 &self.wallet
63 }
64
65 pub fn set_network_name(&mut self, network: Option<String>) {
68 self.network = network;
69 }
70
71 pub fn cache_dir(&self) -> PathBuf {
73 let mut cache = self.profile_dir.clone();
74
75 if let Some(network) = &self.network {
76 cache.push(format!("cache_{network}"));
77 } else {
78 cache.push("cache");
79 }
80
81 cache
82 }
83}
84
85impl FromStr for WalletPath {
86 type Err = crate::Error;
87
88 fn from_str(s: &str) -> Result<Self, Self::Err> {
89 let p = Path::new(s);
90
91 Ok(Self::new(p))
92 }
93}
94
95impl From<PathBuf> for WalletPath {
96 fn from(p: PathBuf) -> Self {
97 Self::new(&p)
98 }
99}
100
101impl From<&Path> for WalletPath {
102 fn from(p: &Path) -> Self {
103 Self::new(p)
104 }
105}
106
107impl fmt::Display for WalletPath {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 write!(
110 f,
111 "wallet path: {}\n\rprofile dir: {}\n\rnetwork: {}",
112 self.wallet.display(),
113 self.profile_dir.display(),
114 self.network.as_ref().unwrap_or(&"default".to_string())
115 )
116 }
117}