Skip to content

Commit 3dc1ad2

Browse files
committed
f DRY up persist
1 parent cf0991f commit 3dc1ad2

File tree

3 files changed

+29
-39
lines changed

3 files changed

+29
-39
lines changed

src/io/fs_store.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#[cfg(target_os = "windows")]
22
extern crate winapi;
33

4-
use super::KVStore;
4+
use super::{get_namespace_and_key_from_prefixed, KVStore};
55

66
use std::collections::HashMap;
77
use std::fs;
88
use std::io::{BufReader, Read, Write};
99
use std::path::{Path, PathBuf};
10-
use std::str::FromStr;
1110
use std::sync::{Arc, Mutex, RwLock};
1211

1312
#[cfg(not(target_os = "windows"))]
@@ -249,25 +248,8 @@ impl Read for FilesystemReader {
249248

250249
impl KVStorePersister for FilesystemStore {
251250
fn persist<W: Writeable>(&self, prefixed_key: &str, object: &W) -> lightning::io::Result<()> {
252-
let dest_file_path = PathBuf::from_str(prefixed_key).map_err(|_| {
253-
let msg = format!("Could not persist file for key {}.", prefixed_key);
254-
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
255-
})?;
256-
257-
let parent_directory = dest_file_path.parent().ok_or_else(|| {
258-
let msg = format!("Could not persist file for key {}.", prefixed_key);
259-
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
260-
})?;
261-
let namespace = parent_directory.display().to_string();
262-
263-
let dest_without_namespace = dest_file_path.strip_prefix(&namespace).map_err(|_| {
264-
let msg = format!("Could not persist file for key {}.", prefixed_key);
265-
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
266-
})?;
267-
let key = dest_without_namespace.display().to_string();
268-
269-
self.write(&namespace, &key, &object.encode())?;
270-
Ok(())
251+
let (namespace, key) = get_namespace_and_key_from_prefixed(prefixed_key)?;
252+
self.write(&namespace, &key, &object.encode())
271253
}
272254
}
273255

src/io/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub use sqlite_store::SqliteStore;
1010
use lightning::util::persist::KVStorePersister;
1111

1212
use std::io::Read;
13+
use std::path::PathBuf;
14+
use std::str::FromStr;
1315

1416
// The namespacs and keys LDK uses for persisting
1517
pub(crate) const CHANNEL_MANAGER_PERSISTENCE_NAMESPACE: &str = "";
@@ -74,3 +76,26 @@ pub trait KVStore: KVStorePersister {
7476
/// Will return an empty list if the `namespace` is unknown.
7577
fn list(&self, namespace: &str) -> std::io::Result<Vec<String>>;
7678
}
79+
80+
fn get_namespace_and_key_from_prefixed(
81+
prefixed_key: &str,
82+
) -> lightning::io::Result<(String, String)> {
83+
let dest_file_path = PathBuf::from_str(prefixed_key).map_err(|_| {
84+
let msg = format!("Could not persist file for key {}.", prefixed_key);
85+
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
86+
})?;
87+
88+
let parent_directory = dest_file_path.parent().ok_or_else(|| {
89+
let msg = format!("Could not persist file for key {}.", prefixed_key);
90+
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
91+
})?;
92+
let namespace = parent_directory.display().to_string();
93+
94+
let dest_without_namespace = dest_file_path.strip_prefix(&namespace).map_err(|_| {
95+
let msg = format!("Could not persist file for key {}.", prefixed_key);
96+
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
97+
})?;
98+
let key = dest_without_namespace.display().to_string();
99+
100+
Ok((namespace, key))
101+
}

src/io/sqlite_store.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rusqlite::{named_params, Connection};
88
use std::fs;
99
use std::io::Cursor;
1010
use std::path::PathBuf;
11-
use std::str::FromStr;
1211
use std::sync::{Arc, Mutex};
1312

1413
// The database file name.
@@ -161,23 +160,7 @@ impl KVStore for SqliteStore {
161160

162161
impl KVStorePersister for SqliteStore {
163162
fn persist<W: Writeable>(&self, prefixed_key: &str, object: &W) -> lightning::io::Result<()> {
164-
let dest_file_path = PathBuf::from_str(prefixed_key).map_err(|_| {
165-
let msg = format!("Could not persist file for key {}.", prefixed_key);
166-
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
167-
})?;
168-
169-
let parent_directory = dest_file_path.parent().ok_or_else(|| {
170-
let msg = format!("Could not persist file for key {}.", prefixed_key);
171-
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
172-
})?;
173-
let namespace = parent_directory.display().to_string();
174-
175-
let dest_without_namespace = dest_file_path.strip_prefix(&namespace).map_err(|_| {
176-
let msg = format!("Could not persist file for key {}.", prefixed_key);
177-
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
178-
})?;
179-
let key = dest_without_namespace.display().to_string();
180-
163+
let (namespace, key) = get_namespace_and_key_from_prefixed(prefixed_key)?;
181164
self.write(&namespace, &key, &object.encode())
182165
}
183166
}

0 commit comments

Comments
 (0)