ant_networking/config.rs
1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use ant_protocol::CLOSE_GROUP_SIZE;
10use core::fmt::Debug;
11use std::num::NonZeroUsize;
12
13use crate::close_group_majority;
14
15/// Specifies the minimum number of distinct nodes that must be successfully contacted in order for a query to succeed.
16#[derive(Debug, Copy, Clone, PartialEq, Eq)]
17pub enum ResponseQuorum {
18 One,
19 Majority,
20 All,
21 N(NonZeroUsize),
22}
23
24impl std::str::FromStr for ResponseQuorum {
25 type Err = String;
26
27 fn from_str(s: &str) -> Result<Self, Self::Err> {
28 match s {
29 "one" => Ok(ResponseQuorum::One),
30 "majority" => Ok(ResponseQuorum::Majority),
31 "all" => Ok(ResponseQuorum::All),
32 _ => {
33 if let Ok(n) = s.parse::<usize>() {
34 let n = NonZeroUsize::new(n);
35 match n {
36 Some(n) => Ok(ResponseQuorum::N(n)),
37 None => Err("Quorum value must be greater than 0".to_string()),
38 }
39 } else {
40 Err("Invalid quorum value".to_string())
41 }
42 }
43 }
44 }
45}
46
47impl ResponseQuorum {
48 /// Get the value of the provided Quorum
49 pub fn get_value(&self) -> usize {
50 match self {
51 ResponseQuorum::Majority => close_group_majority(),
52 ResponseQuorum::All => CLOSE_GROUP_SIZE,
53 ResponseQuorum::N(v) => v.get(),
54 ResponseQuorum::One => 1,
55 }
56 }
57}