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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Serializable, Deserializable)]
pub struct Services(u64);

impl From<Services> for u64 {
	fn from(s: Services) -> Self {
		s.0
	}
}

impl From<u64> for Services {
	fn from(v: u64) -> Self {
		Services(v)
	}
}

impl Services {
	pub fn network(&self) -> bool {
		self.bit_at(0)
	}

	pub fn with_network(mut self, v: bool) -> Self {
		self.set_bit(0, v);
		self
	}

	pub fn getutxo(&self) -> bool {
		self.bit_at(1)
	}

	pub fn with_getutxo(mut self, v: bool) -> Self {
		self.set_bit(1, v);
		self
	}

	pub fn bloom(&self) -> bool {
		self.bit_at(2)
	}

	pub fn with_bloom(mut self, v: bool) -> Self {
		self.set_bit(2, v);
		self
	}

	pub fn witness(&self) -> bool {
		self.bit_at(3)
	}

	pub fn with_witness(mut self, v: bool) -> Self {
		self.set_bit(3, v);
		self
	}

	pub fn xthin(&self) -> bool {
		self.bit_at(4)
	}

	pub fn with_xthin(mut self, v: bool) -> Self {
		self.set_bit(4, v);
		self
	}

	pub fn bitcoin_cash(&self) -> bool {
		self.bit_at(5)
	}

	pub fn with_bitcoin_cash(mut self, v: bool) -> Self {
		self.set_bit(5, v);
		self
	}

	pub fn includes(&self, other: &Self) -> bool {
		self.0 & other.0 == other.0
	}

	fn set_bit(&mut self, bit: usize, bit_value: bool) {
		if bit_value {
			self.0 |= 1 << bit
		} else {
			self.0 &= !(1 << bit)
		}
	}

	fn bit_at(&self, bit: usize) -> bool {
		self.0 & (1 << bit) != 0
	}
}

#[cfg(test)]
mod test {
	use super::Services;

	#[test]
	fn test_serivces_includes() {
		let s1 = Services::default()
			.with_witness(true)
			.with_xthin(true);
		let s2 = Services::default()
			.with_witness(true);

		assert!(s1.witness());
		assert!(s1.xthin());
		assert!(s2.witness());
		assert!(!s2.xthin());
		assert!(s1.includes(&s2));
		assert!(!s2.includes(&s1));
	}
}