Skip to content
Snippets Groups Projects
Commit a6929ea4 authored by Éloïs's avatar Éloïs
Browse files

[ref] kv_typed: remove StringErr

parent 3c48177e
No related branches found
No related tags found
No related merge requests found
Showing
with 137 additions and 128 deletions
...@@ -6,3 +6,7 @@ dex = "run --release --package duniter-dbex --" ...@@ -6,3 +6,7 @@ dex = "run --release --package duniter-dbex --"
ta = "test --all" ta = "test --all"
rr = "run --release --" rr = "run --release --"
xtask = "run --package xtask --" xtask = "run --package xtask --"
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld"]
...@@ -1097,6 +1097,7 @@ dependencies = [ ...@@ -1097,6 +1097,7 @@ dependencies = [
name = "duniter-dbs" name = "duniter-dbs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"arrayvec", "arrayvec",
"bincode", "bincode",
"byteorder", "byteorder",
......
...@@ -66,8 +66,9 @@ pub(crate) fn export_bc<B: Backend>( ...@@ -66,8 +66,9 @@ pub(crate) fn export_bc<B: Backend>(
r.iter() r.iter()
.map(|block_res| { .map(|block_res| {
let json_block_res = match block_res { let json_block_res = match block_res {
Ok(block) => serde_json::to_value(&block) Ok(block) => {
.map_err(|e| KvError::DeserError(e.to_string())), serde_json::to_value(&block).map_err(|e| KvError::DeserError(e.into()))
}
Err(e) => Err(e), Err(e) => Err(e),
}; };
s2.send(json_block_res).map_err(|_| anyhow!("fail to send")) s2.send(json_block_res).map_err(|_| anyhow!("fail to send"))
......
...@@ -30,6 +30,7 @@ uninit = "0.4.0" ...@@ -30,6 +30,7 @@ uninit = "0.4.0"
zerocopy = "0.3.0" zerocopy = "0.3.0"
[dev-dependencies] [dev-dependencies]
anyhow = "1.0.34"
tempdir = "0.3.7" tempdir = "0.3.7"
unwrap = "1.2.1" unwrap = "1.2.1"
......
...@@ -25,13 +25,13 @@ impl KeyAsBytes for AllKeyV1 { ...@@ -25,13 +25,13 @@ impl KeyAsBytes for AllKeyV1 {
} }
impl kv_typed::prelude::FromBytes for AllKeyV1 { impl kv_typed::prelude::FromBytes for AllKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
if bytes == b"ALL" { if bytes == b"ALL" {
Ok(Self) Ok(Self)
} else { } else {
Err(StringErr(format!( Err(CorruptedBytes(format!(
"Invalid key: expected '{:?}', found '{:?}'", "Invalid key: expected '{:?}', found '{:?}'",
b"ALL", bytes b"ALL", bytes
))) )))
...@@ -47,8 +47,9 @@ impl ToDumpString for AllKeyV1 { ...@@ -47,8 +47,9 @@ impl ToDumpString for AllKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for AllKeyV1 { impl ExplorableKey for AllKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
<Self as kv_typed::prelude::FromBytes>::from_bytes(source.as_bytes()) <Self as kv_typed::prelude::FromBytes>::from_bytes(source.as_bytes())
.map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned())) self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned()))
......
...@@ -29,18 +29,16 @@ impl KeyAsBytes for BlockNumberKeyV1 { ...@@ -29,18 +29,16 @@ impl KeyAsBytes for BlockNumberKeyV1 {
} }
impl FromBytes for BlockNumberKeyV1 { impl FromBytes for BlockNumberKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let key_str = std::str::from_utf8(bytes).map_err(|e| StringErr(format!("{}", e)))?; let key_str = std::str::from_utf8(bytes).map_err(|e| CorruptedBytes(e.to_string()))?;
if key_str == "0000000NaN" { if key_str == "0000000NaN" {
Ok(BlockNumberKeyV1(BlockNumber(u32::MAX))) Ok(BlockNumberKeyV1(BlockNumber(u32::MAX)))
} else { } else {
Ok(BlockNumberKeyV1(BlockNumber( Ok(BlockNumberKeyV1(BlockNumber(key_str.parse().map_err(
key_str |e| CorruptedBytes(format!("{}: {}", e, key_str)),
.parse() )?)))
.map_err(|e| StringErr(format!("{}: {}", e, key_str)))?,
)))
} }
} }
} }
...@@ -53,8 +51,8 @@ impl ToDumpString for BlockNumberKeyV1 { ...@@ -53,8 +51,8 @@ impl ToDumpString for BlockNumberKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for BlockNumberKeyV1 { impl ExplorableKey for BlockNumberKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
Ok(format!("{}", (self.0).0)) Ok(format!("{}", (self.0).0))
......
...@@ -25,18 +25,18 @@ impl KeyAsBytes for BlockstampKeyV1 { ...@@ -25,18 +25,18 @@ impl KeyAsBytes for BlockstampKeyV1 {
} }
impl kv_typed::prelude::FromBytes for BlockstampKeyV1 { impl kv_typed::prelude::FromBytes for BlockstampKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let blockstamp_strs: ArrayVec<[&str; 2]> = std::str::from_utf8(bytes) let blockstamp_strs: ArrayVec<[&str; 2]> = std::str::from_utf8(bytes)
.map_err(|e| StringErr(format!("{}", e)))? .map_err(|e| CorruptedBytes(e.to_string()))?
.split('-') .split('-')
.collect(); .collect();
let block_number = blockstamp_strs[0] let block_number = blockstamp_strs[0]
.parse() .parse()
.map_err(|e| StringErr(format!("{}", e)))?; .map_err(|e: ParseIntError| CorruptedBytes(e.to_string()))?;
let block_hash = let block_hash =
Hash::from_hex(blockstamp_strs[1]).map_err(|e| StringErr(format!("{}", e)))?; Hash::from_hex(blockstamp_strs[1]).map_err(|e| CorruptedBytes(e.to_string()))?;
Ok(BlockstampKeyV1(Blockstamp { Ok(BlockstampKeyV1(Blockstamp {
number: BlockNumber(block_number), number: BlockNumber(block_number),
hash: BlockHash(block_hash), hash: BlockHash(block_hash),
...@@ -52,8 +52,8 @@ impl ToDumpString for BlockstampKeyV1 { ...@@ -52,8 +52,8 @@ impl ToDumpString for BlockstampKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for BlockstampKeyV1 { impl ExplorableKey for BlockstampKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
Ok(format!("{}", self.0)) Ok(format!("{}", self.0))
...@@ -71,12 +71,12 @@ impl KeyAsBytes for BlockstampKeyV2 { ...@@ -71,12 +71,12 @@ impl KeyAsBytes for BlockstampKeyV2 {
} }
impl kv_typed::prelude::FromBytes for BlockstampKeyV2 { impl kv_typed::prelude::FromBytes for BlockstampKeyV2 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
use dubp::common::bytes_traits::FromBytes as _; use dubp::common::bytes_traits::FromBytes as _;
Ok(Self( Ok(Self(
Blockstamp::from_bytes(bytes).map_err(|e| StringErr(e.to_string()))?, Blockstamp::from_bytes(bytes).map_err(|e| CorruptedBytes(e.to_string()))?,
)) ))
} }
} }
...@@ -89,9 +89,9 @@ impl ToDumpString for BlockstampKeyV2 { ...@@ -89,9 +89,9 @@ impl ToDumpString for BlockstampKeyV2 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for BlockstampKeyV2 { impl ExplorableKey for BlockstampKeyV2 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Ok(Self( Ok(Self(
Blockstamp::from_str(source).map_err(|e| StringErr(e.to_string()))?, Blockstamp::from_str(source).map_err(|e| FromExplorerKeyErr(e.into()))?,
)) ))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
......
...@@ -69,11 +69,11 @@ impl KeyAsBytes for DunpNodeIdV1Db { ...@@ -69,11 +69,11 @@ impl KeyAsBytes for DunpNodeIdV1Db {
} }
impl kv_typed::prelude::FromBytes for DunpNodeIdV1Db { impl kv_typed::prelude::FromBytes for DunpNodeIdV1Db {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let layout = zerocopy::LayoutVerified::<_, DunpNodeIdV1Db>::new(bytes) let layout = zerocopy::LayoutVerified::<_, DunpNodeIdV1Db>::new(bytes)
.ok_or_else(|| StringErr("corrupted db".to_owned()))?; .ok_or_else(|| CorruptedBytes("corrupted db".to_owned()))?;
Ok(*layout) Ok(*layout)
} }
} }
...@@ -86,7 +86,7 @@ impl ToDumpString for DunpNodeIdV1Db { ...@@ -86,7 +86,7 @@ impl ToDumpString for DunpNodeIdV1Db {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for DunpNodeIdV1Db { impl ExplorableKey for DunpNodeIdV1Db {
fn from_explorer_str(_: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(_: &str) -> std::result::Result<Self, FromExplorerKeyErr> {
unimplemented!() unimplemented!()
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
......
...@@ -25,12 +25,12 @@ impl KeyAsBytes for HashKeyV1 { ...@@ -25,12 +25,12 @@ impl KeyAsBytes for HashKeyV1 {
} }
impl kv_typed::prelude::FromBytes for HashKeyV1 { impl kv_typed::prelude::FromBytes for HashKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let hash_str = std::str::from_utf8(bytes).map_err(|e| StringErr(format!("{}", e)))?; let hash_str = std::str::from_utf8(bytes).map_err(|e| CorruptedBytes(e.to_string()))?;
Ok(HashKeyV1( Ok(HashKeyV1(
Hash::from_hex(&hash_str).map_err(|e| StringErr(format!("{}", e)))?, Hash::from_hex(&hash_str).map_err(|e| CorruptedBytes(e.to_string()))?,
)) ))
} }
} }
...@@ -43,8 +43,8 @@ impl ToDumpString for HashKeyV1 { ...@@ -43,8 +43,8 @@ impl ToDumpString for HashKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for HashKeyV1 { impl ExplorableKey for HashKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned())) self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned()))
...@@ -71,11 +71,11 @@ impl KeyAsBytes for HashKeyV2 { ...@@ -71,11 +71,11 @@ impl KeyAsBytes for HashKeyV2 {
} }
impl kv_typed::prelude::FromBytes for HashKeyV2 { impl kv_typed::prelude::FromBytes for HashKeyV2 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
if bytes.len() != 32 { if bytes.len() != 32 {
Err(StringErr(format!( Err(CorruptedBytes(format!(
"Invalid length: expected 32 found {}", "Invalid length: expected 32 found {}",
bytes.len() bytes.len()
))) )))
...@@ -95,9 +95,9 @@ impl ToDumpString for HashKeyV2 { ...@@ -95,9 +95,9 @@ impl ToDumpString for HashKeyV2 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for HashKeyV2 { impl ExplorableKey for HashKeyV2 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Ok(Self( Ok(Self(
Hash::from_hex(source).map_err(|e| StringErr(format!("{}", e)))?, Hash::from_hex(source).map_err(|e| FromExplorerKeyErr(e.into()))?,
)) ))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
......
...@@ -39,12 +39,12 @@ impl KeyAsBytes for PubKeyKeyV1 { ...@@ -39,12 +39,12 @@ impl KeyAsBytes for PubKeyKeyV1 {
} }
impl kv_typed::prelude::FromBytes for PubKeyKeyV1 { impl kv_typed::prelude::FromBytes for PubKeyKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let pubkey_str = std::str::from_utf8(bytes).map_err(|e| StringErr(format!("{}", e)))?; let pubkey_str = std::str::from_utf8(bytes).map_err(|e| CorruptedBytes(e.to_string()))?;
Ok(PubKeyKeyV1(PublicKey::from_base58(&pubkey_str).map_err( Ok(PubKeyKeyV1(PublicKey::from_base58(&pubkey_str).map_err(
|e| StringErr(format!("{}: {}", e, pubkey_str)), |e| CorruptedBytes(format!("{}: {}", e, pubkey_str)),
)?)) )?))
} }
} }
...@@ -65,12 +65,12 @@ impl KeyAsBytes for PubKeyKeyV2 { ...@@ -65,12 +65,12 @@ impl KeyAsBytes for PubKeyKeyV2 {
} }
impl kv_typed::prelude::FromBytes for PubKeyKeyV2 { impl kv_typed::prelude::FromBytes for PubKeyKeyV2 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
Ok(PubKeyKeyV2( Ok(PubKeyKeyV2(PublicKey::try_from(bytes).map_err(|e| {
PublicKey::try_from(bytes).map_err(|e| StringErr(format!("{}: {:?}", e, bytes)))?, CorruptedBytes(format!("{}: {:?}", e, bytes))
)) })?))
} }
} }
...@@ -82,8 +82,8 @@ impl ToDumpString for PubKeyKeyV2 { ...@@ -82,8 +82,8 @@ impl ToDumpString for PubKeyKeyV2 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for PubKeyKeyV1 { impl ExplorableKey for PubKeyKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned())) self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned()))
...@@ -92,9 +92,9 @@ impl ExplorableKey for PubKeyKeyV1 { ...@@ -92,9 +92,9 @@ impl ExplorableKey for PubKeyKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for PubKeyKeyV2 { impl ExplorableKey for PubKeyKeyV2 {
fn from_explorer_str(pubkey_str: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(pubkey_str: &str) -> std::result::Result<Self, FromExplorerKeyErr> {
Ok(PubKeyKeyV2(PublicKey::from_base58(&pubkey_str).map_err( Ok(PubKeyKeyV2(PublicKey::from_base58(&pubkey_str).map_err(
|e| StringErr(format!("{}: {}", e, pubkey_str)), |e| FromExplorerKeyErr(format!("{}: {}", e, pubkey_str).into()),
)?)) )?))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
......
...@@ -35,18 +35,18 @@ impl KeyAsBytes for PubKeyAndSigV1 { ...@@ -35,18 +35,18 @@ impl KeyAsBytes for PubKeyAndSigV1 {
} }
impl kv_typed::prelude::FromBytes for PubKeyAndSigV1 { impl kv_typed::prelude::FromBytes for PubKeyAndSigV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let raw_str = std::str::from_utf8(bytes).map_err(|e| StringErr(format!("{}", e)))?; let raw_str = std::str::from_utf8(bytes).map_err(|e| CorruptedBytes(e.to_string()))?;
if raw_str == "ALL" { if raw_str == "ALL" {
Ok(PubKeyAndSigV1::all()) Ok(PubKeyAndSigV1::all())
} else { } else {
let array_str: ArrayVec<[&str; 2]> = raw_str.split(':').collect(); let array_str: ArrayVec<[&str; 2]> = raw_str.split(':').collect();
let pubkey = let pubkey =
PublicKey::from_base58(array_str[0]).map_err(|e| StringErr(format!("{}", e)))?; PublicKey::from_base58(array_str[0]).map_err(|e| CorruptedBytes(e.to_string()))?;
let sig = let sig =
Signature::from_base64(array_str[1]).map_err(|e| StringErr(format!("{}", e)))?; Signature::from_base64(array_str[1]).map_err(|e| CorruptedBytes(e.to_string()))?;
Ok(PubKeyAndSigV1(pubkey, sig)) Ok(PubKeyAndSigV1(pubkey, sig))
} }
} }
...@@ -60,8 +60,8 @@ impl ToDumpString for PubKeyAndSigV1 { ...@@ -60,8 +60,8 @@ impl ToDumpString for PubKeyAndSigV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for PubKeyAndSigV1 { impl ExplorableKey for PubKeyAndSigV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned())) self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned()))
......
...@@ -44,15 +44,17 @@ impl KeyAsBytes for SourceKeyV1 { ...@@ -44,15 +44,17 @@ impl KeyAsBytes for SourceKeyV1 {
} }
impl kv_typed::prelude::FromBytes for SourceKeyV1 { impl kv_typed::prelude::FromBytes for SourceKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let strs: ArrayVec<[&str; 3]> = std::str::from_utf8(bytes) let strs: ArrayVec<[&str; 3]> = std::str::from_utf8(bytes)
.map_err(|e| StringErr(format!("{}", e)))? .map_err(|e| CorruptedBytes(e.to_string()))?
.split('-') .split('-')
.collect(); .collect();
let tx_hash = Hash::from_hex(strs[0]).map_err(|e| StringErr(format!("{}", e)))?; let tx_hash = Hash::from_hex(strs[0]).map_err(|e| CorruptedBytes(e.to_string()))?;
let pos = strs[1].parse().map_err(|e| StringErr(format!("{}", e)))?; let pos = strs[1]
.parse()
.map_err(|e: ParseIntError| CorruptedBytes(e.to_string()))?;
let consumed = if strs.len() <= 2 { let consumed = if strs.len() <= 2 {
None None
} else { } else {
...@@ -60,7 +62,7 @@ impl kv_typed::prelude::FromBytes for SourceKeyV1 { ...@@ -60,7 +62,7 @@ impl kv_typed::prelude::FromBytes for SourceKeyV1 {
"1" => Some(true), "1" => Some(true),
"0" => Some(false), "0" => Some(false),
_ => { _ => {
return Err(StringErr( return Err(CorruptedBytes(
"invalid format: field consumed must be encoded with '0' or '1'".to_owned(), "invalid format: field consumed must be encoded with '0' or '1'".to_owned(),
)) ))
} }
...@@ -82,8 +84,8 @@ impl ToDumpString for SourceKeyV1 { ...@@ -82,8 +84,8 @@ impl ToDumpString for SourceKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for SourceKeyV1 { impl ExplorableKey for SourceKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned())) self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned()))
......
...@@ -25,15 +25,13 @@ impl KeyAsBytes for TimestampKeyV1 { ...@@ -25,15 +25,13 @@ impl KeyAsBytes for TimestampKeyV1 {
} }
impl kv_typed::prelude::FromBytes for TimestampKeyV1 { impl kv_typed::prelude::FromBytes for TimestampKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let key_str = std::str::from_utf8(bytes).map_err(|e| StringErr(format!("{}", e)))?; let key_str = std::str::from_utf8(bytes).map_err(|e| CorruptedBytes(e.to_string()))?;
Ok(TimestampKeyV1( Ok(TimestampKeyV1(key_str.parse().map_err(|e| {
key_str CorruptedBytes(format!("{}: {}", e, key_str))
.parse() })?))
.map_err(|e| StringErr(format!("{}: {}", e, key_str)))?,
))
} }
} }
...@@ -45,10 +43,10 @@ impl ToDumpString for TimestampKeyV1 { ...@@ -45,10 +43,10 @@ impl ToDumpString for TimestampKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for TimestampKeyV1 { impl ExplorableKey for TimestampKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
NaiveDateTime::parse_from_str(source, "%Y-%m-%d %H:%M:%S") NaiveDateTime::parse_from_str(source, "%Y-%m-%d %H:%M:%S")
.map(|dt| TimestampKeyV1(dt.timestamp() as u64)) .map(|dt| TimestampKeyV1(dt.timestamp() as u64))
.map_err(|e| StringErr(format!("{}: {}", e, source))) .map_err(|e| FromExplorerKeyErr(format!("{}: {}", e, source).into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
Ok(NaiveDateTime::from_timestamp(self.0 as i64, 0) Ok(NaiveDateTime::from_timestamp(self.0 as i64, 0)
......
...@@ -47,15 +47,15 @@ impl KeyAsBytes for UdIdV2 { ...@@ -47,15 +47,15 @@ impl KeyAsBytes for UdIdV2 {
} }
impl FromBytes for UdIdV2 { impl FromBytes for UdIdV2 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let pubkey = PublicKey::try_from(&bytes[..33]) let pubkey = PublicKey::try_from(&bytes[..33])
.map_err(|e| StringErr(format!("{}: {:?}", e, bytes)))?; .map_err(|e| CorruptedBytes(format!("{}: {:?}", e, bytes)))?;
let block_number = BlockNumber( let block_number = BlockNumber(
zerocopy::LayoutVerified::<_, zerocopy::U32<byteorder::BigEndian>>::new(&bytes[33..]) zerocopy::LayoutVerified::<_, zerocopy::U32<byteorder::BigEndian>>::new(&bytes[33..])
.ok_or_else(|| { .ok_or_else(|| {
StringErr( CorruptedBytes(
"Corrupted DB: BlockNumber bytes are invalid length or unaligned" "Corrupted DB: BlockNumber bytes are invalid length or unaligned"
.to_owned(), .to_owned(),
) )
...@@ -74,22 +74,22 @@ impl ToDumpString for UdIdV2 { ...@@ -74,22 +74,22 @@ impl ToDumpString for UdIdV2 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for UdIdV2 { impl ExplorableKey for UdIdV2 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
let mut source = source.split(':'); let mut source = source.split(':');
if let Some(pubkey_str) = source.next() { if let Some(pubkey_str) = source.next() {
let pubkey = PublicKey::from_base58(&pubkey_str) let pubkey = PublicKey::from_base58(&pubkey_str)
.map_err(|e| StringErr(format!("{}: {}", e, pubkey_str)))?; .map_err(|e| FromExplorerKeyErr(format!("{}: {}", e, pubkey_str).into()))?;
if let Some(block_number_str) = source.next() { if let Some(block_number_str) = source.next() {
Ok(UdIdV2( Ok(UdIdV2(
pubkey, pubkey,
BlockNumber::from_str(block_number_str) BlockNumber::from_str(block_number_str)
.map_err(|e| StringErr(format!("{}", e)))?, .map_err(|e| FromExplorerKeyErr(e.into()))?,
)) ))
} else { } else {
Err(StringErr("UdIdV2: Invalid format".to_owned())) Err(FromExplorerKeyErr("UdIdV2: Invalid format".into()))
} }
} else { } else {
Err(StringErr("UdIdV2: Invalid format".to_owned())) Err(FromExplorerKeyErr("UdIdV2: Invalid format".into()))
} }
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
...@@ -102,7 +102,7 @@ mod tests { ...@@ -102,7 +102,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn ud_id_v2_as_bytes() -> std::result::Result<(), StringErr> { fn ud_id_v2_as_bytes() -> anyhow::Result<()> {
let ud_id = UdIdV2(PublicKey::default(), BlockNumber(3)); let ud_id = UdIdV2(PublicKey::default(), BlockNumber(3));
let ud_id_2_res = ud_id.as_bytes(|bytes| { let ud_id_2_res = ud_id.as_bytes(|bytes| {
......
...@@ -27,13 +27,13 @@ impl KeyAsBytes for UidKeyV1 { ...@@ -27,13 +27,13 @@ impl KeyAsBytes for UidKeyV1 {
} }
impl kv_typed::prelude::FromBytes for UidKeyV1 { impl kv_typed::prelude::FromBytes for UidKeyV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let uid_str = std::str::from_utf8(bytes).map_err(|e| StringErr(format!("{}", e)))?; let uid_str = std::str::from_utf8(bytes).map_err(|e| CorruptedBytes(e.to_string()))?;
Ok(Self( Ok(Self(
ArrayString::<[u8; USERNAME_MAX_LEN]>::from_str(uid_str) ArrayString::<[u8; USERNAME_MAX_LEN]>::from_str(uid_str)
.map_err(|e| StringErr(format!("{}", e)))?, .map_err(|e| CorruptedBytes(e.to_string()))?,
)) ))
} }
} }
...@@ -56,8 +56,8 @@ impl ToDumpString for UidKeyV1 { ...@@ -56,8 +56,8 @@ impl ToDumpString for UidKeyV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for UidKeyV1 { impl ExplorableKey for UidKeyV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned())) self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned()))
......
...@@ -49,16 +49,16 @@ impl KeyAsBytes for UtxoIdDbV2 { ...@@ -49,16 +49,16 @@ impl KeyAsBytes for UtxoIdDbV2 {
} }
impl FromBytes for UtxoIdDbV2 { impl FromBytes for UtxoIdDbV2 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let hash = zerocopy::LayoutVerified::<_, Hash>::new(&bytes[..32]).ok_or_else(|| { let hash = zerocopy::LayoutVerified::<_, Hash>::new(&bytes[..32]).ok_or_else(|| {
StringErr("Corrupted DB: Hash bytes are invalid length or unaligned".to_owned()) CorruptedBytes("Corrupted DB: Hash bytes are invalid length or unaligned".to_owned())
})?; })?;
let output_index = let output_index =
zerocopy::LayoutVerified::<_, zerocopy::U32<byteorder::BigEndian>>::new(&bytes[32..]) zerocopy::LayoutVerified::<_, zerocopy::U32<byteorder::BigEndian>>::new(&bytes[32..])
.ok_or_else(|| { .ok_or_else(|| {
StringErr( CorruptedBytes(
"Corrupted DB: OutputIndex bytes are invalid length or unaligned" "Corrupted DB: OutputIndex bytes are invalid length or unaligned"
.to_owned(), .to_owned(),
) )
...@@ -76,21 +76,21 @@ impl ToDumpString for UtxoIdDbV2 { ...@@ -76,21 +76,21 @@ impl ToDumpString for UtxoIdDbV2 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for UtxoIdDbV2 { impl ExplorableKey for UtxoIdDbV2 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
let mut source = source.split(':'); let mut source = source.split(':');
if let Some(hash_str) = source.next() { if let Some(hash_str) = source.next() {
let hash = let hash = Hash::from_hex(&hash_str)
Hash::from_hex(&hash_str).map_err(|e| StringErr(format!("{}: {}", e, hash_str)))?; .map_err(|e| FromExplorerKeyErr(format!("{}: {}", e, hash_str).into()))?;
if let Some(output_index_str) = source.next() { if let Some(output_index_str) = source.next() {
Ok(UtxoIdDbV2( Ok(UtxoIdDbV2(
hash, hash,
u32::from_str(output_index_str).map_err(|e| StringErr(format!("{}", e)))?, u32::from_str(output_index_str).map_err(|e| FromExplorerKeyErr(e.into()))?,
)) ))
} else { } else {
Err(StringErr("UtxoIdDbV2: Invalid format".to_owned())) Err(FromExplorerKeyErr("UtxoIdDbV2: Invalid format".into()))
} }
} else { } else {
Err(StringErr("UtxoIdDbV2: Invalid format".to_owned())) Err(FromExplorerKeyErr("UtxoIdDbV2: Invalid format".into()))
} }
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
...@@ -202,7 +202,7 @@ impl KeyAsBytes for GvaUtxoIdDbV1 { ...@@ -202,7 +202,7 @@ impl KeyAsBytes for GvaUtxoIdDbV1 {
} }
impl FromBytes for GvaUtxoIdDbV1 { impl FromBytes for GvaUtxoIdDbV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
if bytes.len() == 69 { if bytes.len() == 69 {
...@@ -214,14 +214,14 @@ impl FromBytes for GvaUtxoIdDbV1 { ...@@ -214,14 +214,14 @@ impl FromBytes for GvaUtxoIdDbV1 {
buffer.copy_from_slice(bytes); buffer.copy_from_slice(bytes);
Ok(Self(buffer)) Ok(Self(buffer))
} else { } else {
Err(StringErr("db corrupted".to_owned())) Err(CorruptedBytes("db corrupted".to_owned()))
} }
} }
} }
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for GvaUtxoIdDbV1 { impl ExplorableKey for GvaUtxoIdDbV1 {
fn from_explorer_str(_: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(_: &str) -> std::result::Result<Self, FromExplorerKeyErr> {
unimplemented!() unimplemented!()
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
...@@ -234,7 +234,7 @@ mod tests { ...@@ -234,7 +234,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn utxo_id_v2_as_bytes() -> std::result::Result<(), StringErr> { fn utxo_id_v2_as_bytes() -> anyhow::Result<()> {
let utxo_id = UtxoIdDbV2(Hash::default(), 3); let utxo_id = UtxoIdDbV2(Hash::default(), 3);
let utxo_id_2_res = utxo_id.as_bytes(|bytes| { let utxo_id_2_res = utxo_id.as_bytes(|bytes| {
......
...@@ -27,13 +27,13 @@ impl KeyAsBytes for WalletConditionsV1 { ...@@ -27,13 +27,13 @@ impl KeyAsBytes for WalletConditionsV1 {
} }
impl kv_typed::prelude::FromBytes for WalletConditionsV1 { impl kv_typed::prelude::FromBytes for WalletConditionsV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let uid_str = std::str::from_utf8(bytes).map_err(|e| StringErr(format!("{}", e)))?; let uid_str = std::str::from_utf8(bytes).map_err(|e| CorruptedBytes(e.to_string()))?;
Ok(Self( Ok(Self(
ArrayString::<[u8; CONDITIONS_MAX_LEN]>::from_str(uid_str) ArrayString::<[u8; CONDITIONS_MAX_LEN]>::from_str(uid_str)
.map_err(|e| StringErr(format!("{}", e)))?, .map_err(|e| CorruptedBytes(e.to_string()))?,
)) ))
} }
} }
...@@ -56,8 +56,8 @@ impl ToDumpString for WalletConditionsV1 { ...@@ -56,8 +56,8 @@ impl ToDumpString for WalletConditionsV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for WalletConditionsV1 { impl ExplorableKey for WalletConditionsV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerKeyErr(e.0.into()))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned())) self.as_bytes(|bytes| Ok(unsafe { std::str::from_utf8_unchecked(bytes) }.to_owned()))
...@@ -85,12 +85,10 @@ impl KeyAsBytes for WalletConditionsV2 { ...@@ -85,12 +85,10 @@ impl KeyAsBytes for WalletConditionsV2 {
} }
impl kv_typed::prelude::FromBytes for WalletConditionsV2 { impl kv_typed::prelude::FromBytes for WalletConditionsV2 {
type Err = StringErr; type Err = bincode::Error;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
Ok(Self( Ok(Self(bincode::deserialize(bytes)?))
bincode::deserialize(bytes).map_err(|e| StringErr(format!("{}", e)))?,
))
} }
} }
...@@ -102,10 +100,10 @@ impl ToDumpString for WalletConditionsV2 { ...@@ -102,10 +100,10 @@ impl ToDumpString for WalletConditionsV2 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for WalletConditionsV2 { impl ExplorableKey for WalletConditionsV2 {
fn from_explorer_str(s: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(s: &str) -> std::result::Result<Self, FromExplorerKeyErr> {
Ok(Self( Ok(Self(
dubp::documents_parser::wallet_script_from_str(s) dubp::documents_parser::wallet_script_from_str(s)
.map_err(|e| StringErr(format!("{}", e)))?, .map_err(|e| FromExplorerKeyErr(e.into()))?,
)) ))
} }
fn to_explorer_string(&self) -> KvResult<String> { fn to_explorer_string(&self) -> KvResult<String> {
......
...@@ -70,11 +70,11 @@ impl KeyAsBytes for WalletHashWithBnV1Db { ...@@ -70,11 +70,11 @@ impl KeyAsBytes for WalletHashWithBnV1Db {
} }
impl kv_typed::prelude::FromBytes for WalletHashWithBnV1Db { impl kv_typed::prelude::FromBytes for WalletHashWithBnV1Db {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let layout = zerocopy::LayoutVerified::<_, WalletHashWithBnV1Db>::new(bytes) let layout = zerocopy::LayoutVerified::<_, WalletHashWithBnV1Db>::new(bytes)
.ok_or_else(|| StringErr("corrupted db".to_owned()))?; .ok_or_else(|| CorruptedBytes("corrupted db".to_owned()))?;
Ok(*layout) Ok(*layout)
} }
} }
...@@ -91,19 +91,19 @@ impl ToDumpString for WalletHashWithBnV1Db { ...@@ -91,19 +91,19 @@ impl ToDumpString for WalletHashWithBnV1Db {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableKey for WalletHashWithBnV1Db { impl ExplorableKey for WalletHashWithBnV1Db {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerKeyErr> {
let mut source = source.split(':'); let mut source = source.split(':');
let hash_str = source let hash_str = source
.next() .next()
.ok_or_else(|| StringErr("missing hash".to_owned()))?; .ok_or_else(|| FromExplorerKeyErr("missing hash".into()))?;
let bn_str = source let bn_str = source
.next() .next()
.ok_or_else(|| StringErr("missing block number".to_owned()))?; .ok_or_else(|| FromExplorerKeyErr("missing block number".into()))?;
let hash = Hash::from_hex(hash_str).map_err(|e| StringErr(e.to_string()))?; let hash = Hash::from_hex(hash_str).map_err(|e| FromExplorerKeyErr(e.into()))?;
let block_number = bn_str let block_number = bn_str
.parse() .parse()
.map_err(|e: std::num::ParseIntError| StringErr(e.to_string()))?; .map_err(|e: std::num::ParseIntError| FromExplorerKeyErr(e.into()))?;
Ok(WalletHashWithBnV1Db::new(hash, block_number)) Ok(WalletHashWithBnV1Db::new(hash, block_number))
} }
......
...@@ -106,9 +106,14 @@ pub(crate) use kv_typed::prelude::*; ...@@ -106,9 +106,14 @@ pub(crate) use kv_typed::prelude::*;
pub(crate) use serde::{Deserialize, Serialize}; pub(crate) use serde::{Deserialize, Serialize};
pub(crate) use smallvec::SmallVec; pub(crate) use smallvec::SmallVec;
pub(crate) use std::{ pub(crate) use std::{
collections::BTreeSet, convert::TryFrom, fmt::Debug, iter::Iterator, path::Path, str::FromStr, collections::BTreeSet, convert::TryFrom, fmt::Debug, iter::Iterator, num::ParseIntError,
path::Path, str::FromStr,
}; };
#[derive(Debug, Error)]
#[error("{0}")]
pub struct CorruptedBytes(pub String);
pub trait ToDumpString { pub trait ToDumpString {
fn to_dump_string(&self) -> String; fn to_dump_string(&self) -> String;
} }
......
...@@ -66,19 +66,18 @@ pub struct BlockDbV1 { ...@@ -66,19 +66,18 @@ pub struct BlockDbV1 {
impl ValueAsBytes for BlockDbV1 { impl ValueAsBytes for BlockDbV1 {
fn as_bytes<T, F: FnMut(&[u8]) -> KvResult<T>>(&self, mut f: F) -> KvResult<T> { fn as_bytes<T, F: FnMut(&[u8]) -> KvResult<T>>(&self, mut f: F) -> KvResult<T> {
let json = let json = serde_json::to_string(self).map_err(|e| KvError::DeserError(e.into()))?;
serde_json::to_string(self).map_err(|e| KvError::DeserError(format!("{}", e)))?;
f(json.as_bytes()) f(json.as_bytes())
} }
} }
impl kv_typed::prelude::FromBytes for BlockDbV1 { impl kv_typed::prelude::FromBytes for BlockDbV1 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
let json_str = std::str::from_utf8(bytes).expect("corrupted db : invalid utf8 bytes"); let json_str = std::str::from_utf8(bytes).expect("corrupted db : invalid utf8 bytes");
Ok(serde_json::from_str(&json_str) Ok(serde_json::from_str(&json_str)
.map_err(|e| StringErr(format!("{}: '{}'", e, json_str)))?) .map_err(|e| CorruptedBytes(format!("{}: '{}'", e, json_str)))?)
} }
} }
...@@ -90,11 +89,11 @@ impl ToDumpString for BlockDbV1 { ...@@ -90,11 +89,11 @@ impl ToDumpString for BlockDbV1 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableValue for BlockDbV1 { impl ExplorableValue for BlockDbV1 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerValueErr> {
Self::from_bytes(source.as_bytes()) Self::from_bytes(source.as_bytes()).map_err(|e| FromExplorerValueErr(e.0.into()))
} }
fn to_explorer_json(&self) -> KvResult<serde_json::Value> { fn to_explorer_json(&self) -> KvResult<serde_json::Value> {
serde_json::to_value(self).map_err(|e| KvError::DeserError(format!("{}", e))) serde_json::to_value(self).map_err(|e| KvError::DeserError(e.into()))
} }
} }
...@@ -123,16 +122,17 @@ pub struct BlockDbV2(pub dubp::block::DubpBlockV10); ...@@ -123,16 +122,17 @@ pub struct BlockDbV2(pub dubp::block::DubpBlockV10);
impl ValueAsBytes for BlockDbV2 { impl ValueAsBytes for BlockDbV2 {
fn as_bytes<T, F: FnMut(&[u8]) -> KvResult<T>>(&self, mut f: F) -> KvResult<T> { fn as_bytes<T, F: FnMut(&[u8]) -> KvResult<T>>(&self, mut f: F) -> KvResult<T> {
let bytes = bincode::serialize(self).map_err(|e| KvError::DeserError(format!("{}", e)))?; let bytes = bincode::serialize(self).map_err(|e| KvError::DeserError(e.into()))?;
f(bytes.as_ref()) f(bytes.as_ref())
} }
} }
impl kv_typed::prelude::FromBytes for BlockDbV2 { impl kv_typed::prelude::FromBytes for BlockDbV2 {
type Err = StringErr; type Err = CorruptedBytes;
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> { fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, Self::Err> {
Ok(bincode::deserialize(&bytes).map_err(|e| StringErr(format!("{}: '{:?}'", e, bytes)))?) Ok(bincode::deserialize(&bytes)
.map_err(|e| CorruptedBytes(format!("{}: '{:?}'", e, bytes)))?)
} }
} }
...@@ -144,10 +144,10 @@ impl ToDumpString for BlockDbV2 { ...@@ -144,10 +144,10 @@ impl ToDumpString for BlockDbV2 {
#[cfg(feature = "explorer")] #[cfg(feature = "explorer")]
impl ExplorableValue for BlockDbV2 { impl ExplorableValue for BlockDbV2 {
fn from_explorer_str(source: &str) -> std::result::Result<Self, StringErr> { fn from_explorer_str(source: &str) -> Result<Self, FromExplorerValueErr> {
Ok(serde_json::from_str(source).map_err(|e| StringErr(e.to_string()))?) Ok(serde_json::from_str(source).map_err(|e| FromExplorerValueErr(e.into()))?)
} }
fn to_explorer_json(&self) -> KvResult<serde_json::Value> { fn to_explorer_json(&self) -> KvResult<serde_json::Value> {
serde_json::to_value(self).map_err(|e| KvError::DeserError(format!("{}", e))) serde_json::to_value(self).map_err(|e| KvError::DeserError(e.into()))
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment