-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.rs
59 lines (54 loc) · 2.08 KB
/
encode.rs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Module providing utility traits for encoding CAN-bus data.
//!
//! The module provides two traits: [TryEncode], and [Encode]. [try_encode](TryEncode::try_encode)
//! models the possibility that the encoding fails whereas [encode](Encode::encode) models the not
//! failable encoding. If [encode](Encode::encode) fails internally, it panics.
//!
//! # Example
//! ```
//! use cantools::data::CANRead;
//! use cantools::signals::Bit;
//! use cantools::encode::{TryEncode, Encode};
//!
//! let bit = Bit::new(20);
//! let mut data = [0u8, 0u8, 0u8, 0u8];
//!
//! let result = bit.try_encode(&mut data, true);
//! bit.encode(&mut data, false);
//! ```
use crate::data::CANWrite;
/// Type representing possible encoding errors.
#[derive(Debug, PartialEq)]
pub enum EncodeError {
/// There is not enough byte data available to encode a value.
NotEnoughData,
/// The value to encode is smaller than the minimum value encodable.
MinError,
/// The value to encode is greater than the maximum value encodable.
MaxError,
}
/// A trait modeling the failable encoding of data.
pub trait TryEncode<T> {
/// A type modelling the different possible failures of the encoding.
type Error;
/// Tries to encode `value` into `data`.
fn try_encode<D: CANWrite>(&self, data: &mut D, value: T) -> Result<(), Self::Error>;
}
/// A trait modeling the not failable encoding of data.
///
/// [Encode] is sub-trait of [TryEncode]. [encode](Encode::encode) is implemented using
/// [try_encode](TryEncode::try_encode). If [try_encode](TryEncode::try_encode) succeeds,
/// [encode](Encode::encode) returns. Otherwise, [encode](Encode::encode) panics.
pub trait Encode<T>: TryEncode<T> {
/// Encodes `value` into the CAN-bus data `data`.
///
/// # Panics
/// [encode](Encode::encode) panics if the call to [try_encode](TryEncode::try_encode) in the
/// implementation returns the error variant.
fn encode<D: CANWrite>(&self, data: &mut D, value: T) {
match self.try_encode(data, value) {
Ok(_) => (),
Err(_) => panic!("cannot encode data"),
}
}
}