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
155
156
157
158
159
160
//! Allows a `Ply` object to be checked for consistency.

use std::fmt::{ Display, Formatter };
use std::fmt;
use std::error;
use super::Ply;
use super::PropertyAccess;

/// Contains a description, why a given `Ply` object isn't consistent and could not be made consistent.
#[derive(Debug)]
pub struct ConsistencyError {
    /// Describes in natural language, why a consistency check failed.
    description: String,
}
impl ConsistencyError {
    /// Create a new error object with a given description of the problem.
    pub fn new(description: &str) -> Self {
        ConsistencyError {
            description: description.to_string(),
        }
    }
}

impl Display for ConsistencyError {
    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        f.write_str(&format!("ConsistencyError: {}", self.description))
    }
}

impl error::Error for ConsistencyError {
    fn description(&self) -> &str {
        &self.description
    }
    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

fn has_white_space(s: &str) -> bool {
    return s.contains(" ") || s.contains("\t");
}

fn has_line_break(s: &str) -> bool {
    return s.contains("\n") || s.contains("\r");
}

impl<E: PropertyAccess> Ply<E>{
    /// Takes a mutable `Ply` object, performs common operations to make it consistent,
    ///
    /// When written, a consistent `Ply` object generates a valid PLY file.
    /// This method also checks for invariants that can't be fixed automatically.
    /// If something can not be fixed automatically, it returns a `ConsistencyError` describing the problem.
    ///
    /// # Remarks
    ///
    /// This method should always be called before writing to a file with `Writer`.
    /// Only exception is `write_ply()`, which, for convenience, performs the check itself.
    /// See `write_ply_unchecked()` for a variant that expects the client to assure consistency.
    ///
    /// No checks on encoding are performed.
    /// For maximal compatability, only ascii characters should be used but this is not checked.
    /// Every relevant string is checked to not contain line breaks.
    /// Identifiers are also checked to not contain white spaces.
    pub fn make_consistent(&mut self) -> Result<(), ConsistencyError>{
        for (ek, _) in &self.header.elements {
            if !self.payload.contains_key(ek) {
                self.payload.insert(ek.clone(), Vec::new());
            }
        }
        for (pk, pe) in &self.payload {
            if pk.is_empty() {
                return Err(ConsistencyError::new("Element cannot have empty name."));
            }
            let ed = self.header.elements.get_mut(pk);
            if ed.is_none() {
                return Err(ConsistencyError::new(&format!("No decleration for element `{}` found.", pk)));
            }
            ed.unwrap().count = pe.len();
        }
        for ref oi in &self.header.obj_infos {
            if has_line_break(oi) {
                return Err(ConsistencyError::new(&format!("Objection information `{}` should not contain any line breaks.", oi)));
            }
        }
        for ref c in &self.header.comments {
            if has_line_break(&c) {
               return Err(ConsistencyError::new(&format!("Comment `{}` should not contain any line breaks.", c)));
            }
        }
        for (_, ref e) in &self.header.elements {
            if has_line_break(&e.name) {
                return Err(ConsistencyError::new(&format!("Name of element `{}` should not contain any line breaks.", e.name)));
            }
            if has_white_space(&e.name) {
                return Err(ConsistencyError::new(&format!("Name of element `{}` should not contain any white spaces.", e.name)));
            }
            for (_, ref p) in &e.properties {
                if has_line_break(&p.name) {
                    return Err(ConsistencyError::new(&format!("Name of property `{}` of element `{}` should not contain any line breaks.", p.name, e.name)));
                }
                if has_white_space(&p.name) {
                    return Err(ConsistencyError::new(&format!("Name of property `{}` of element `{}` should not contain any spaces.", p.name, e.name)));
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::super::*;
    type P = Ply<DefaultElement>;
    #[test]
    fn consistent_new_line_fail_comment() {
        let mut p = P::new();
        p.header.comments.push("a beautiful\r\nnew line!".to_string());
        let r = p.make_consistent();
        assert!(r.is_err());
    }
    #[test]
    fn consistent_new_line_fail_obj_infos() {
        let mut p = P::new();
        p.header.obj_infos.push("some\r\nnew line!".to_string());
        let r = p.make_consistent();
        assert!(r.is_err());
    }
    #[test]
    fn consistent_new_line_fail_element() {
        let mut p = P::new();
        p.header.elements.add(ElementDef::new("new\nline".to_string()));
        let r = p.make_consistent();
        assert!(r.is_err());
    }
    #[test]
    fn consistent_new_line_fail_property () {
        let mut p = P::new();
        let mut e = ElementDef::new("ok".to_string());
        e.properties.add(PropertyDef::new("prop\nwith new line".to_string(), PropertyType::Scalar(ScalarType::Char)));
        p.header.elements.add(e);
        let r = p.make_consistent();
        assert!(r.is_err());
    }
    #[test]
    fn consistent_white_space_fail_element() {
        let mut p = P::new();
        p.header.elements.add(ElementDef::new("white space".to_string()));
        let r = p.make_consistent();
        assert!(r.is_err());
    }
    #[test]
    fn consistent_white_space_fail_property(){
        let mut p = P::new();
        let mut e = ElementDef::new("ok".to_string());
        e.properties.add(PropertyDef::new("prop\twhite space".to_string(), PropertyType::Scalar(ScalarType::Char)));
        p.header.elements.add(e);
        let r = p.make_consistent();
        assert!(r.is_err());
    }
}