rust_ether/
lib.rs

1//! # Ether - transfer data through means
2//! **Ether** provides traits that abstracts data transmission layer, allowing developers to build applications that can transfer datas online through various means without worrying apis from each mean.
3/// A stream transfer protocol.
4pub trait StreamTransfer {
5    /// Connect to stream. Most implments should do two things here:
6    ///
7    /// 1. `Connect` to the stream.
8    /// 2. Make sure the server / other peer is online and active.
9    fn connect(&mut self) -> Result<(), String>;
10    /// Try to receive a data packet and remove it from queue.
11    fn recv(&mut self) -> Result<Option<Vec<u8>>, String>;
12    /// Try to receive a data packet.
13    fn peek(&mut self) -> Result<Option<Vec<u8>>, String>;
14    /// Send a data packet to the server / other peer.
15    fn send(&mut self, data: Vec<u8>) -> Result<(), String>;
16}
17/// A packet transfer protocol. It is similear to `StreamTransfer` trait but provides a `tag` for transfer index.
18pub trait PacketTransfer {
19    /// Connect to host. Most implments should make sure the server / other peer is avaliable.
20    fn connect(&mut self) -> Result<(), String>;
21    /// Try to receive a data packet and remove it.
22    fn recv(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String> {
23        let ret = self.peek(tag)?;
24        self.remove(tag)?;
25        Ok(ret)
26    }
27    /// Try to receive a data packet.
28    fn peek(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String>;
29    /// Try to send a data packet to the server / other peer.
30    fn send(&mut self, tag: &str, data: Vec<u8>) -> Result<(), String>;
31    /// Remove the packet from the queue.
32    fn remove(&mut self, tag: &str) -> Result<(), String>;
33}
34/// A data storage protocol.
35///
36///It is very similear to `PacketTransfer` trait, but implements should guarantee the persistence of the data when implementing `DataStorage`.
37pub trait DataStorage {
38    /// Connect to host. Most implments should make sure the server / other peer is avaliable.
39    fn connect(&mut self) -> Result<(), String>;
40    /// Try to download a data packet. Similear to *ftp*'s `GET`.
41    fn peek(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String>;
42    /// Try to upload a data packet. Similear to *ftp*'s `PUT`.
43    fn send(&mut self, tag: &str, data: Vec<u8>) -> Result<(), String>;
44    /// Remove the data packet from server.
45    fn remove(&mut self, tag: &str) -> Result<(), String>;
46}
47/// `extends` provides:
48///
49/// 1. Implementation of `StreamTransfer` for `PacketTransfer` - `StreamTransferOverlay`
50/// 2. Implementation of `PacketTransfer` for `DataStorage`
51pub mod extends {
52    use super::*;
53    pub struct StreamTransferOverlay<T: PacketTransfer> {
54        packet_transfer: T,
55        recv: u128,
56        send: u128,
57    }
58    impl<T: PacketTransfer> StreamTransferOverlay<T> {
59        pub fn new(packet_transfer: T) -> Self {
60            Self {
61                packet_transfer,
62                recv: 0,
63                send: 0,
64            }
65        }
66    }
67    impl<T: PacketTransfer> StreamTransfer for StreamTransferOverlay<T> {
68        fn connect(&mut self) -> Result<(), String> {
69            self.packet_transfer.connect()
70        }
71        fn recv(&mut self) -> Result<Option<Vec<u8>>, String> {
72            self.recv += 1;
73            self.packet_transfer.recv(&(self.recv - 1).to_string())
74        }
75        fn peek(&mut self) -> Result<Option<Vec<u8>>, String> {
76            self.packet_transfer.peek(&self.recv.to_string())
77        }
78        fn send(&mut self, data: Vec<u8>) -> Result<(), String> {
79            self.send += 1;
80            self.packet_transfer
81                .send(&(self.send - 1).to_string(), data)
82        }
83    }
84    impl<T: DataStorage> PacketTransfer for T {
85        fn connect(&mut self) -> Result<(), String> {
86            self.connect()
87        }
88        fn peek(&mut self, tag: &str) -> Result<Option<Vec<u8>>, String> {
89            self.peek(tag)
90        }
91        fn send(&mut self, tag: &str, data: Vec<u8>) -> Result<(), String> {
92            self.send(tag, data)
93        }
94        fn remove(&mut self, tag: &str) -> Result<(), String> {
95            self.remove(tag)
96        }
97    }
98}