consistent_hasher/
transaction.rs

1use std::fmt;
2use serde::{Serialize,Deserialize};
3
4#[derive(Debug,Serialize,Deserialize)]
5pub struct Transaction<T: fmt::Display + PartialEq>
6{
7    pub source:T,
8    pub destination:T,
9    pub min:T,
10    pub max:T,
11    exception: bool
12}
13
14impl<T:PartialOrd +  fmt::Display > Transaction<T>
15{
16    pub fn new(source: T , destination: T, min: T , max: T)-> Self
17    {
18        Self {exception:(min > max ), source, destination,min,max }
19    }
20    pub fn in_range(&self,num: T) -> bool
21    {
22        if !self.exception
23        {
24            return num>self.min && num<=self.max;
25        }
26        return num<= self.max || num > self.min;
27    }
28    
29}
30impl<T:PartialOrd +  fmt::Display > fmt::Display for Transaction<T>
31{
32    fn fmt(&self,f: &mut fmt::Formatter)->fmt::Result
33    {
34        let stringy = match self.exception
35        {
36            true =>
37            {
38                format!("[0,{}] u ]{},..[",self.max,self.min)
39            }
40            ,
41            _=>
42            {
43                format!("]{},{}]",self.min,self.max)
44            }
45
46        };
47        write!(f,"source: {}  -  destination: {}  -  range: {}",self.source,self.destination,stringy)
48
49    }
50}
51impl<T: fmt::Display + PartialEq> PartialEq for Transaction<T> {
52    fn eq(&self, other: &Self) -> bool {
53        self.source == other.source
54            && self.destination == other.destination
55            && self.exception == other.exception
56    }
57}