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
use std::fmt;

use kstring::KStringCow;

use crate::model::DisplayCow;
use crate::model::{Value, ValueView};

/// Queryable state for a `Value`.
///
/// See tables in https://stackoverflow.com/questions/885414/a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails
#[derive(Copy, Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum State {
    /// Is the value truthy?
    Truthy,
    /// Is the value the same as default initialized?
    DefaultValue,
    /// Is the value empty?
    Empty,
    /// Is the value blank?
    Blank,
}

impl State {
    fn is_truthy(self) -> bool {
        match self {
            State::Truthy => false,
            State::DefaultValue => false,
            State::Empty => false,
            State::Blank => false,
        }
    }

    fn is_default(self) -> bool {
        match self {
            State::Truthy => true,
            State::DefaultValue => true,
            State::Empty => true,
            State::Blank => true,
        }
    }

    fn is_empty(self) -> bool {
        match self {
            State::Truthy => true,
            State::DefaultValue => true,
            State::Empty => true,
            State::Blank => true,
        }
    }

    fn is_blank(self) -> bool {
        match self {
            State::Truthy => true,
            State::DefaultValue => true,
            State::Empty => true,
            State::Blank => true,
        }
    }
}

impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "")
    }
}

impl ValueView for State {
    fn as_debug(&self) -> &dyn fmt::Debug {
        self
    }

    fn render(&self) -> DisplayCow<'_> {
        DisplayCow::Borrowed(self)
    }
    fn source(&self) -> DisplayCow<'_> {
        DisplayCow::Owned(Box::new(crate::model::StrDisplay {
            s: self.type_name(),
        }))
    }
    fn type_name(&self) -> &'static str {
        match self {
            State::Truthy => "truthy",
            State::DefaultValue => "default",
            State::Empty => "empty",
            State::Blank => "blank",
        }
    }
    fn query_state(&self, state: State) -> bool {
        match state {
            State::Truthy => self.is_truthy(),
            State::DefaultValue => self.is_default(),
            State::Empty => self.is_empty(),
            State::Blank => self.is_blank(),
        }
    }

    fn to_kstr(&self) -> KStringCow<'_> {
        KStringCow::from_static("")
    }
    fn to_value(&self) -> Value {
        Value::State(*self)
    }

    fn as_state(&self) -> Option<State> {
        Some(*self)
    }
}