Skip to content

Commit

Permalink
fix(tari-bor): ciborium_io error for nostd after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Sep 15, 2023
1 parent 97115c4 commit e76680d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
12 changes: 3 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dan_layer/engine/tests/templates/buggy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"

[dependencies]
tari_template_abi = { path = "../../../../template_abi", default-features = false, features = ["alloc"] }
tari_bor = { path = "../../../../tari_bor", default-features = false }
tari_bor = { path = "../../../../tari_bor", default-features = false, features = ["alloc"] }
lol_alloc = "0.4.0"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/tari_bor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "BSD-3-Clause"
[dependencies]
# git rev: include fix from https://github.com/enarx/ciborium/pull/80
ciborium = { git = "https://github.com/enarx/ciborium.git", rev = "114614d2a61102eb2321c68e53799d1e6f087aef", default-features = false }
ciborium-io = { version = "0.2.1", default-features = false }
ciborium-io = { git = "https://github.com/enarx/ciborium.git", rev = "114614d2a61102eb2321c68e53799d1e6f087aef", default-features = false }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }

[dev-dependencies]
Expand Down
34 changes: 34 additions & 0 deletions dan_layer/tari_bor/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

#[cfg(not(feature = "std"))]
use alloc::{format, string::String};

#[derive(Debug, Clone)]
pub struct BorError(String);

impl BorError {
pub fn new(str: String) -> Self {
Self(str)
}
}

#[cfg(feature = "std")]
impl std::fmt::Display for BorError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(feature = "std")]
impl std::error::Error for BorError {
fn description(&self) -> &str {
&self.0
}
}

impl From<ciborium::value::Error> for BorError {
fn from(value: ciborium::value::Error) -> Self {
BorError(format!("{}", value))
}
}
58 changes: 13 additions & 45 deletions dan_layer/tari_bor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,23 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
use alloc::{format, string::String, string::ToString, vec::Vec};
use alloc::{format, string::ToString, vec::Vec};

#[cfg(not(feature = "std"))]
extern crate alloc;

mod tag;
pub use tag::*;

mod error;
mod walker;

pub use ciborium::value::Value;
use ciborium::{de::from_reader, ser::into_writer};
pub use error::BorError;
pub use serde;
use serde::{de::DeserializeOwned, Serialize};
pub use walker::*;

#[derive(Debug, Clone)]
pub struct BorError(String);

impl BorError {
pub fn new(str: String) -> Self {
Self(str)
}
}

#[cfg(feature = "std")]
impl std::fmt::Display for BorError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(feature = "std")]
impl std::error::Error for BorError {
fn description(&self) -> &str {
&self.0
}
}

impl From<ciborium::value::Error> for BorError {
fn from(value: ciborium::value::Error) -> Self {
BorError(format!("{}", value))
}
}

pub fn encode_with_len<T: Serialize>(val: &T) -> Vec<u8> {
let mut buf = Vec::with_capacity(512);
buf.extend([0u8; 4]);
Expand All @@ -62,8 +34,12 @@ pub fn encode_with_len<T: Serialize>(val: &T) -> Vec<u8> {
}

#[cfg(not(feature = "std"))]
pub fn encode_into<T: Serialize + ?Sized, W: ciborium_io::Write>(val: &T, writer: &mut W) -> Result<(), BorError>
where W::Error: core::fmt::Debug {
pub fn encode_into<T, W>(val: &T, writer: &mut W) -> Result<(), BorError>
where
W::Error: core::fmt::Debug,
T: Serialize + ?Sized,
W: ciborium_io::Write,
{
into_writer(&val, writer).map_err(to_bor_error)
}

Expand All @@ -72,17 +48,9 @@ pub fn encode_into<T: Serialize + ?Sized, W: std::io::Write>(val: &T, writer: &m
into_writer(&val, writer).map_err(to_bor_error)
}

#[cfg(not(feature = "std"))]
pub fn encode<T: Serialize + ?Sized>(val: &T) -> Result<Vec<u8>, BorError> {
let mut buf = Vec::with_capacity(512);
encode_into(val, &mut buf).map_err(|_| BorError(String::new()))?;
Ok(buf)
}

#[cfg(feature = "std")]
pub fn encode<T: Serialize + ?Sized>(val: &T) -> Result<Vec<u8>, BorError> {
let mut buf = Vec::with_capacity(512);
encode_into(val, &mut buf).map_err(|e| BorError(format!("{:?}", e)))?;
encode_into(val, &mut buf).map_err(|e| BorError::new(format!("{:?}", e)))?;
Ok(buf)
}

Expand All @@ -98,7 +66,7 @@ fn decode_inner<T: DeserializeOwned>(input: &mut &[u8]) -> Result<T, BorError> {
pub fn decode_exact<T: DeserializeOwned>(mut input: &[u8]) -> Result<T, BorError> {
let val = decode_inner(&mut input)?;
if !input.is_empty() {
return Err(BorError(format!(
return Err(BorError::new(format!(
"decode_exact: {} bytes remaining on input",
input.len()
)));
Expand All @@ -108,7 +76,7 @@ pub fn decode_exact<T: DeserializeOwned>(mut input: &[u8]) -> Result<T, BorError

pub fn decode_len(input: &[u8]) -> Result<usize, BorError> {
if input.len() < 4 {
return Err(BorError("Not enough bytes to decode length".to_string()));
return Err(BorError::new("Not enough bytes to decode length".to_string()));
}

let mut buf = [0u8; 4];
Expand All @@ -119,5 +87,5 @@ pub fn decode_len(input: &[u8]) -> Result<usize, BorError> {

fn to_bor_error<E>(e: E) -> BorError
where E: core::fmt::Display {
BorError(e.to_string())
BorError::new(e.to_string())
}

0 comments on commit e76680d

Please sign in to comment.