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
149
150
151
152
153
154
use super::Term;
use super::parsing_error::ParsingError;
use super::free_group_term::FreeGroupTerm;
use std::ops::Mul;

/// The smallest term (apart from the identity).
/// 
/// # Examples
/// Literals can be parsed from a string.
/// Basic usage:
/// Symbols with ids are okay.
/// ```
/// use l_group_formulas::literal::Literal;
/// let literal = Literal::new('x', 31, true);
/// assert_eq!(literal, Literal::from("X31"));
/// ```
/// So are symbols without.
/// ```
/// # use l_group_formulas::literal::Literal;
/// let literal = Literal::new('y', 0, false);
/// assert_eq!(literal, Literal::from("y"));
/// ```
/// 
/// Alternatively, literals can be constructed from characters.
/// This sets `id` to zero and `is_inverted` to false.
/// ```
/// use l_group_formulas::literal::*;
/// let literal1 = Literal::from('x');
/// let literal2 = Literal::new('x', 0, false);
/// assert_eq!(literal1, literal2);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Literal {
    pub character: char,
    pub id: usize,
    pub is_inverted: bool
}

impl Literal {
    pub fn new(character: char, id: usize, is_inverted: bool) -> Literal {
        Literal {
            character: character,
            id: id,
            is_inverted: is_inverted
        }
    }
}

impl From<char> for Literal {
    fn from(c: char) -> Literal {
        Literal {
            character: c,
            id: 0,
            is_inverted: false
        }
    }
}

impl Term for Literal {
    fn inverse(&self) -> Literal {
        Literal {
            character: self.character,
            id: self.id,
            is_inverted: !self.is_inverted
        }
    }
}

impl ToString for Literal {
    fn to_string(&self) -> String {
        let mut result = String::new();
        match self.is_inverted {
            false => result.push(self.character),
            true => {
                let upper = self.character.to_uppercase();
                for c in upper { result.push(c); }
            }
        }
        if self.id != 0 {
            result.push_str(&self.id.to_string());
        }
        return result;
    }
}

impl Mul for Literal {
    type Output = FreeGroupTerm;
    fn mul(self, other: Literal) -> FreeGroupTerm {
        FreeGroupTerm::new(vec![self, other])
    }
}

impl From<&str> for Literal {
    fn from(s: &str) -> Literal {
        let result = parse(s);
        match result {
            Ok(literal) => literal,
            Err(e) => panic!(e)
        }
    }
}


fn parse(s: &str) -> Result<Literal, ParsingError> {
    let l = s.len();
    if l == 0 {
        return Err(ParsingError::EmptyLiteralError);
    } else {
        let is_inverted: bool;
        let mut lower_case: std::char::ToLowercase;
        match s.chars().next() {
            None => return Err(ParsingError::EmptyLiteralError),
            Some(c) => { 
                is_inverted = c.is_uppercase(); 
                lower_case = c.to_lowercase(); 
            }
        };
        let character: char;
        match lower_case.next() {
            None => return Err(ParsingError::EmptyLiteralError),
            Some(c) => character = c
        };
        if l == 1 { return Ok(Literal::new(character, 0, is_inverted)); }

        let result = without_first(s).parse::<usize>();
        match result {
            Ok(id) => Ok(Literal::new(character, id, is_inverted)),
            Err(e) => Err(ParsingError::InvalidLiteralError(e.to_string()))
        }
    }
}

fn without_first(string: &str) -> String {
    let mut result = String::new();
    let mut iterator = string.chars();
    iterator.next();
    for c in iterator { result.push(c); }

    return result;
}


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

    #[test]
    fn test_literal_to_string() {
        assert_eq!("x", Literal::from('x').to_string());
        assert_eq!("X", Literal::from('x').inverse().to_string());
        let l = Literal::new('x', 31, true);
        assert_eq!("X31", l.to_string());
    }
}