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
60
61
62
63
64
65
66
67
use std::future::Future;

pub mod constant;
pub mod frame;
pub mod identifier;
pub mod j1939;

pub trait Conversion
    where
        Self: Sized, {
    type Type;

    /// Convert an integer of type `Self::Type` into `Self`
    fn from_bits(bits: Self::Type) -> Self;

    /// Convert a hexadecimal string slice into `Self`
    fn from_hex(hex_str: &str) -> Self;

    /// Convert an integer of type `Self::Type` into `Self`
    /// # Errors
    /// - Implementation dependent
    fn try_from_bits(bits: Self::Type) -> Option<Self>;

    /// Convert a hexadecimal string slice into `Self`
    /// # Errors
    /// - Implementation dependent
    fn try_from_hex(hex_str: &str) -> Option<Self>;

    /// Convert `self` into an integer of type `Self::Type`
    fn into_bits(self) -> Self::Type;

    /// Convert `self` into a hexadecimal string
    fn into_hex(self) -> String;
}

#[repr(C)]
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub enum Direct {
    #[default]
    Transmit,
    Receive,
}

/// Transmit and Receive device trait.
pub trait CanDeviceSync {
    type Error;
    type Frame;
    type Channel;

    fn transmit_sync(&self, channel: Self::Channel, frames: Self::Frame, canfd: bool, _: Option<usize>)
        -> Result<usize, Self::Error>;

    fn receive_sync(&self, channel: Self::Channel, canfd: bool, timeout: Option<usize>)
        -> Result<Self::Frame, Self::Error>;
}

pub trait CanDeviceAsync {
    type Error;
    type Frame;
    type Channel;

    fn transmit_async(&self, channel: Self::Channel, frames: Self::Frame, canfd: bool, _: Option<usize>)
        -> impl Future<Output = Result<usize, Self::Error>>;

    fn receive_async(&self, channel: Self::Channel, canfd: bool, timeout: Option<usize>)
        -> impl Future<Output = Result<Self::Frame, Self::Error>>;
}