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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::{EndlessSelf, OrderBy, SortMethod, SortOrder};

use clashctl_core::model::Rule;

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    strum::EnumString,
    strum::Display,
    strum::EnumVariantNames,
)]
#[strum(ascii_case_insensitive)]
pub enum RuleSortBy {
    Payload,
    Proxy,
    Type,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct RuleSort {
    by: RuleSortBy,
    order: SortOrder,
}

impl RuleSort {
    #[inline]
    pub fn new(by: RuleSortBy, order: SortOrder) -> Self {
        Self { by, order }
    }

    #[inline]
    pub fn by(&self) -> RuleSortBy {
        self.by
    }

    #[inline]
    pub fn order(&self) -> SortOrder {
        self.order
    }

    #[inline]
    pub fn by_type_asc() -> Self {
        Self::new(RuleSortBy::Type, SortOrder::Ascendant)
    }

    #[inline]
    pub fn by_type_dsc() -> Self {
        Self::new(RuleSortBy::Type, SortOrder::Descendant)
    }

    #[inline]
    pub fn by_payload_asc() -> Self {
        Self::new(RuleSortBy::Payload, SortOrder::Ascendant)
    }

    #[inline]
    pub fn by_payload_dsc() -> Self {
        Self::new(RuleSortBy::Payload, SortOrder::Descendant)
    }

    #[inline]
    pub fn by_proxy_name_asc() -> Self {
        Self::new(RuleSortBy::Proxy, SortOrder::Ascendant)
    }

    #[inline]
    pub fn by_proxy_name_dsc() -> Self {
        Self::new(RuleSortBy::Proxy, SortOrder::Descendant)
    }
}

impl EndlessSelf for RuleSort {
    fn next_self(&mut self) {
        use RuleSortBy::*;
        use SortOrder::*;

        *self = match (self.by, self.order) {
            (Payload, Ascendant) => Self::by_payload_dsc(),
            (Payload, Descendant) => Self::by_type_asc(),
            (Type, Ascendant) => Self::by_type_dsc(),
            (Type, Descendant) => Self::by_proxy_name_asc(),
            (Proxy, Ascendant) => Self::by_proxy_name_dsc(),
            (Proxy, Descendant) => Self::by_payload_asc(),
        }
    }
    fn prev_self(&mut self) {
        use RuleSortBy::*;
        use SortOrder::*;

        *self = match (self.by, self.order) {
            (Payload, Ascendant) => Self::by_proxy_name_dsc(),
            (Payload, Descendant) => Self::by_payload_asc(),
            (Type, Ascendant) => Self::by_payload_dsc(),
            (Type, Descendant) => Self::by_type_asc(),
            (Proxy, Ascendant) => Self::by_type_dsc(),
            (Proxy, Descendant) => Self::by_proxy_name_asc(),
        }
    }
}

impl ToString for RuleSort {
    fn to_string(&self) -> String {
        format!(
            "{} {}",
            self.by,
            match self.order {
                SortOrder::Ascendant => "▲",
                SortOrder::Descendant => "▼",
            }
        )
    }
}

impl SortMethod<Rule> for RuleSort {
    fn sort_fn(&self, a: &Rule, b: &Rule) -> std::cmp::Ordering {
        match self.by {
            RuleSortBy::Payload => a.payload.cmp(&b.payload),
            RuleSortBy::Proxy => a.proxy.cmp(&b.proxy),
            RuleSortBy::Type => a.rule_type.cmp(&b.rule_type),
        }
        .order_by(self.order)
    }
}

impl Default for RuleSort {
    fn default() -> Self {
        Self::by_payload_dsc()
    }
}