diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-12-10 16:58:09 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-12-10 16:58:09 +0200 |
commit | e5a830248d8a5d68a701377e0935fc9a8e3785a1 (patch) | |
tree | 0b7a29cea7881b1e98c7e100e9106df6bd6dfb88 | |
parent | 96f6cee09873d7f4edf59dddd57c8a27df9e6b91 (diff) | |
download | obnam-benchmark-e5a830248d8a5d68a701377e0935fc9a8e3785a1.tar.gz |
refactor to pre-create all needed pathbufs
Sponsored-by: author
-rw-r--r-- | src/obnam.rs | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/src/obnam.rs b/src/obnam.rs index e4ed09e..87bafcb 100644 --- a/src/obnam.rs +++ b/src/obnam.rs @@ -18,7 +18,10 @@ lazy_static! { /// /// Manage an Obnam server and run the Obnam client. pub struct Obnam { - configs: TempDir, + client_config: PathBuf, + server_config: PathBuf, + tls_key: PathBuf, + tls_cert: PathBuf, root: TempDir, chunks: TempDir, } @@ -33,9 +36,15 @@ pub enum ObnamError { impl Obnam { pub fn new() -> Result<Self, ObnamError> { + let configs = tempdir()?; + let path = configs.path(); + let root = tempdir()?; let o = Self { - configs: tempdir()?, - root: tempdir()?, + client_config: path.join("client.yaml"), + server_config: path.join("server.yaml"), + tls_key: path.join("tls.key"), + tls_cert: path.join("tls.pem"), + root, chunks: tempdir()?, }; o.configure()?; @@ -50,33 +59,28 @@ impl Obnam { self.chunks.path() } - fn configs(&self) -> &Path { - self.configs.path() - } - - fn server_config(&self) -> PathBuf { - self.configs().join("server.yaml") + fn server_config(&self) -> &Path { + &self.server_config } - fn tls_key(&self) -> PathBuf { - self.configs().join("tls.key") + fn tls_key(&self) -> &Path { + &self.tls_key } - fn tls_cert(&self) -> PathBuf { - self.configs().join("tls.pem") + fn tls_cert(&self) -> &Path { + &self.tls_cert } - fn client_config(&self) -> PathBuf { - self.configs().join("client.yaml") + fn client_config(&self) -> &Path { + &self.client_config } fn configure(&self) -> Result<(), ObnamError> { - let key = self.tls_key(); - let cert = self.tls_cert(); - std::fs::write(&key, TLS_KEY.to_vec())?; - std::fs::write(&cert, TLS_KEY.to_vec())?; - ServerConfig::new(SERVER_PORT, self.chunks(), &key, &cert).write(&self.server_config())?; - ClientConfig::new(SERVER_PORT, self.root()).write(&self.client_config())?; + std::fs::write(self.tls_key(), TLS_KEY.to_vec())?; + std::fs::write(self.tls_cert(), TLS_KEY.to_vec())?; + ServerConfig::new(SERVER_PORT, self.chunks(), self.tls_key(), self.tls_cert()) + .write(self.server_config())?; + ClientConfig::new(SERVER_PORT, self.root()).write(self.client_config())?; Ok(()) } |