1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) DUSK NETWORK. All rights reserved.
// Licensed under the MPL 2.0 license. See LICENSE file in the project root for details.

use core::fmt::Debug;

use crate::store::{Sink, Source, Store};

/// The sole error that can be encountered by reading data
#[derive(Debug, Clone)]
pub struct InvalidEncoding;

impl<S: Store> Canon<S> for InvalidEncoding {
    fn write(&self, _sink: &mut impl Sink<S>) -> Result<(), S::Error> {
        Ok(())
    }

    fn read(_source: &mut impl Source<S>) -> Result<Self, S::Error> {
        Ok(InvalidEncoding)
    }

    fn encoded_len(&self) -> usize {
        0
    }
}

/// Trait to read/write values as bytes
pub trait Canon<S: Store>: Sized + Clone {
    /// Write the value as bytes to a `Sink`
    fn write(&self, sink: &mut impl Sink<S>) -> Result<(), S::Error>;
    /// Read the value from bytes in a `Source`
    fn read(source: &mut impl Source<S>) -> Result<Self, S::Error>;
    /// Returns the number of bytes needed to encode this value
    fn encoded_len(&self) -> usize;
}