1use super::*;
2use ark_std::format;
3
4#[derive(Clone, Debug)]
6#[repr(transparent)]
7pub struct ArkScaleError(pub scale::Error);
8
9impl fmt::Display for ArkScaleError {
10 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
11 self.0.fmt(f)
13 }
14}
15
16impl ark_std::error::Error for ArkScaleError {} pub fn scale_error_to_ark_error(error: scale::Error) -> io::Error {
19 io::Error::new(io::ErrorKind::UnexpectedEof, ArkScaleError(error))
20}
21
22pub fn ark_error_to_scale_error(error: SerializationError) -> scale::Error {
23 use SerializationError::*;
24 match error {
26 NotEnoughSpace => "Arkworks deserialization failed: NotEnoughSpace".into(),
27 InvalidData => "Arkworks deserialization failed: InvalidData".into(),
28 UnexpectedFlags => "Arkworks deserialization failed: UnexpectedFlags".into(),
29 IoError(io_error) => {
30 let err_msg: scale::Error = "Arkworks deserialization io error".into();
31 let err_msg = err_msg.chain(format!("{}", &io_error));
32 #[cfg(feature = "std")]
34 if let Some(boxed_dyn_error) = io_error.into_inner() {
35 if let Ok(error) = boxed_dyn_error.downcast::<ArkScaleError>() {
36 return error.0;
37 }
38 }
39 err_msg
40 }
41 }
42}
43
44pub struct InputAsRead<'a, I: Input>(pub &'a mut I);
46
47impl<'a, I: Input> Read for InputAsRead<'a, I> {
48 fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
49 panic!("At present Scale uses only read_exact, but if this changes then we should handle lengths correctly.");
50 }
62
63 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
64 self.0.read(buf).map_err(scale_error_to_ark_error)?;
66 Ok(())
67 }
68}
69
70pub struct OutputAsWrite<'a, O: Output + ?Sized>(pub &'a mut O);
72
73impl<'a, I: Output + ?Sized> Write for OutputAsWrite<'a, I> {
74 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
75 self.0.write(buf);
77 Ok(buf.len())
79 }
80
81 fn flush(&mut self) -> ArkResult<()> {
82 Ok(())
84 }
85}