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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use crate::graph::value_wrappers::{unwrap_value, StrongValue};
use crate::node_wrappers::{debug_wrapper, BaseNodeTrait, FinalNode};
use crate::tao::archetype::{Archetype, ArchetypeTrait};
use crate::tao::form::data::Data;
use crate::tao::form::FormTrait;
use crate::Wrapper;
use std::convert::TryFrom;
use std::fmt;
use std::fmt::{Debug, Formatter};
use std::rc::Rc;

/// The concept of numbers.
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Number {
    base: FinalNode,
}

impl Debug for Number {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        debug_wrapper("Number", self, f)
    }
}

impl From<usize> for Number {
    fn from(id: usize) -> Self {
        Self {
            base: FinalNode::from(id),
        }
    }
}

impl From<FinalNode> for Number {
    fn from(f: FinalNode) -> Self {
        Self { base: f }
    }
}

impl<'a> TryFrom<&'a str> for Number {
    type Error = String;

    fn try_from(name: &'a str) -> Result<Self, Self::Error> {
        FinalNode::try_from(name).map(|f| Self { base: f })
    }
}

impl Wrapper for Number {
    type BaseType = FinalNode;

    fn essence(&self) -> &FinalNode {
        &self.base
    }

    fn essence_mut(&mut self) -> &mut FinalNode {
        &mut self.base
    }
}

impl<'a> ArchetypeTrait<'a> for Number {
    type ArchetypeForm = Archetype;
    type Form = Number;

    const TYPE_ID: usize = 15;
    const TYPE_NAME: &'static str = "number";
    const PARENT_TYPE_ID: usize = Data::TYPE_ID;
}

impl FormTrait for Number {}

impl Number {
    /// Set usize value for this concept.
    pub fn set_value(&mut self, value: usize) {
        self.essence_mut()
            .set_value(Rc::new(StrongValue::new(value)));
    }

    /// Retrieve usize-valued StrongValue.
    #[allow(clippy::rc_buffer)]
    pub fn value(&self) -> Option<Rc<usize>> {
        unwrap_value::<usize>(self.essence().value())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::node_wrappers::CommonNodeTrait;
    use crate::tao::archetype::ArchetypeFormTrait;
    use crate::tao::form::FormTrait;
    use crate::tao::initialize_kb;
    use std::rc::Rc;

    #[test]
    fn check_type_created() {
        initialize_kb();
        assert_eq!(Number::archetype().id(), Number::TYPE_ID);
        assert_eq!(
            Number::archetype().internal_name_str(),
            Some(Rc::from(Number::TYPE_NAME))
        );
    }

    #[test]
    fn check_type_attributes() {
        initialize_kb();
        #[rustfmt::skip]
        assert_eq!(Number::archetype().introduced_attribute_archetypes(), vec![]);
        assert_eq!(Number::archetype().attribute_archetypes(), vec![]);
    }

    #[test]
    fn from_node_id() {
        initialize_kb();
        let concept = Number::new();
        let concept_copy = Number::from(concept.id());
        assert_eq!(concept.id(), concept_copy.id());
    }

    #[test]
    fn from_name() {
        initialize_kb();
        let mut concept = Number::new();
        concept.set_internal_name_str("A");
        assert_eq!(Number::try_from("A").map(|c| c.id()), Ok(concept.id()));
        assert!(Number::try_from("B").is_err());
    }

    #[test]
    fn test_wrapper_implemented() {
        initialize_kb();
        let concept = Number::new();
        assert_eq!(concept.essence(), &FinalNode::from(concept.id()));
    }

    #[test]
    fn get_value_none() {
        initialize_kb();
        let concept = Number::new();
        assert_eq!(concept.value(), None);
    }

    #[test]
    fn get_value_some() {
        initialize_kb();
        let mut concept = Number::new();
        concept.set_value(0);
        assert_eq!(concept.value(), Some(Rc::new(0)));
    }
}