ark_scale/
rw.rs

1use super::*;
2use ark_std::format;
3
4/// Scale `Input` error wrapped for passage through Arkworks' `CanonicalDeserialize`
5#[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        // use fmt::Display;
12        self.0.fmt(f)
13    }
14}
15
16impl ark_std::error::Error for ArkScaleError {} // No source to return
17
18pub 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    // println!("{:?}",&error);
25    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            // ark_std::Error lacks downcasting https://github.com/arkworks-rs/std/issues/44
33            #[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
44/// Scale `Input` wrapped as Arkworks' `Read`
45pub 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        // assert_eq!(self.0.remaining_len(), Ok(Some(buf.len())));
51        // println!("{:?}",self.0.remaining_len());
52        // Avoid reading too much if the limit exists?!?
53        /*
54        let l = self.0.remaining_len()
55        .map_err(scale_error_to_ark_error) ?
56        .unwrap_or(buf.len());
57        let l = core::cmp::min(l,buf.len());
58        self.0.read(&mut buf[0..l]).map_err(scale_error_to_ark_error) ?;
59        Ok(l)
60        */
61    }
62
63    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
64        // scale's Input::read acts like Read::read_exact
65        self.0.read(buf).map_err(scale_error_to_ark_error)?;
66        Ok(())
67    }
68}
69
70/// Scale `Output` wrapped as Arkworks' `Write`
71pub 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        // Scale `Output`s always succeed
76        self.0.write(buf);
77        // Scale `Output`s always succeed fully
78        Ok(buf.len())
79    }
80
81    fn flush(&mut self) -> ArkResult<()> {
82        // Scale `Output`s always succeed immediately
83        Ok(())
84    }
85}