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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Chemfiles, a modern library for chemistry file reading and writing
// Copyright (C) 2015-2017 Guillaume Fraux
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
use std::ops::Drop;

use chemfiles_sys::*;
use errors::{check, Error};
use strings;
use Result;

/// A thin wrapper around `CHFL_PROPERTY`
pub(crate) struct RawProperty {
    handle: *const CHFL_PROPERTY,
}

impl RawProperty {
    /// Create a `RawProperty` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the pointer,
    /// except for it being non-null.
    pub unsafe fn from_ptr(ptr: *const CHFL_PROPERTY) -> Result<RawProperty> {
        if ptr.is_null() {
            Err(Error::null_ptr())
        } else {
            Ok(RawProperty { handle: ptr })
        }
    }

    /// Get the underlying C pointer as a const pointer.
    pub fn as_ptr(&self) -> *const CHFL_PROPERTY {
        self.handle
    }

    /// Get the underlying C pointer as a mutable pointer.
    pub fn as_mut_ptr(&mut self) -> *mut CHFL_PROPERTY {
        self.handle as *mut CHFL_PROPERTY
    }

    fn double(value: f64) -> Result<RawProperty> {
        unsafe {
            let handle = chfl_property_double(value);
            RawProperty::from_ptr(handle)
        }
    }

    fn bool(value: bool) -> Result<RawProperty> {
        let value = if value { 1 } else { 0 };
        unsafe {
            let handle = chfl_property_bool(value);
            RawProperty::from_ptr(handle)
        }
    }

    fn vector3d(value: [f64; 3]) -> Result<RawProperty> {
        unsafe {
            let handle = chfl_property_vector3d(value.as_ptr());
            RawProperty::from_ptr(handle)
        }
    }

    fn string(value: &str) -> Result<RawProperty> {
        let buffer = strings::to_c(value);
        unsafe {
            let handle = chfl_property_string(buffer.as_ptr());
            RawProperty::from_ptr(handle)
        }
    }

    fn get_kind(&self) -> Result<chfl_property_kind> {
        let mut kind = chfl_property_kind::CHFL_PROPERTY_BOOL;
        unsafe {
            try!(check(chfl_property_get_kind(self.as_ptr(), &mut kind)));
        }
        return Ok(kind);
    }

    fn get_bool(&self) -> Result<bool> {
        let mut value = 0;
        unsafe {
            try!(check(chfl_property_get_bool(self.as_ptr(), &mut value)));
        }
        return Ok(value != 0);
    }

    fn get_double(&self) -> Result<f64> {
        let mut value = 0.0;
        unsafe {
            try!(check(chfl_property_get_double(self.as_ptr(), &mut value)));
        }
        return Ok(value);
    }

    fn get_string(&self) -> Result<String> {
        let get_string = |ptr, len| unsafe { chfl_property_get_string(self.as_ptr(), ptr, len) };
        let value = try!(strings::call_autogrow_buffer(64, get_string));
        return Ok(strings::from_c(value.as_ptr()));
    }

    fn get_vector3d(&self) -> Result<[f64; 3]> {
        let mut value = [0.0; 3];
        unsafe {
            try!(check(chfl_property_get_vector3d(self.as_ptr(), value.as_mut_ptr())));
        }
        return Ok(value);
    }
}

impl Drop for RawProperty {
    fn drop(&mut self) {
        unsafe {
            let status = chfl_property_free(self.as_mut_ptr());
            debug_assert_eq!(status, chfl_status::CHFL_SUCCESS);
        }
    }
}

/// A `Property` is a piece of data that can be associated with an `Atom` or a
/// `Frame`.
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Property {
    /// Boolean property
    Bool(bool),
    /// Floating point property
    Double(f64),
    /// String property
    String(String),
    /// 3-dimensional vector property
    Vector3D([f64; 3]),
}

impl Property {
    pub(crate) fn as_raw(&self) -> Result<RawProperty> {
        match *self {
            Property::Bool(value) => RawProperty::bool(value),
            Property::Double(value) => RawProperty::double(value),
            Property::String(ref value) => RawProperty::string(value),
            Property::Vector3D(value) => RawProperty::vector3d(value),
        }
    }

    pub(crate) fn from_raw(raw: RawProperty) -> Result<Property> {
        match raw.get_kind()? {
            chfl_property_kind::CHFL_PROPERTY_BOOL => Ok(Property::Bool(raw.get_bool()?)),
            chfl_property_kind::CHFL_PROPERTY_DOUBLE => Ok(Property::Double(raw.get_double()?)),
            chfl_property_kind::CHFL_PROPERTY_STRING => Ok(Property::String(raw.get_string()?)),
            chfl_property_kind::CHFL_PROPERTY_VECTOR3D => {
                Ok(Property::Vector3D(raw.get_vector3d()?))
            }
        }
    }
}

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

        #[test]
        fn bool() {
            let property = RawProperty::bool(false).unwrap();
            assert_eq!(property.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_BOOL));
            assert_eq!(property.get_bool(), Ok(false));
        }

        #[test]
        fn double() {
            let property = RawProperty::double(45.0).unwrap();
            assert_eq!(property.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_DOUBLE));
            assert_eq!(property.get_double(), Ok(45.0));
        }

        #[test]
        fn string() {
            let property = RawProperty::string("test").unwrap();
            assert_eq!(property.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_STRING));
            assert_eq!(property.get_string(), Ok("test".into()));
        }

        #[test]
        fn vector3d() {
            let property = RawProperty::vector3d([1.2, 3.4, 5.6]).unwrap();
            assert_eq!(property.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_VECTOR3D));
            assert_eq!(property.get_vector3d(), Ok([1.2, 3.4, 5.6]));
        }
    }

    mod rust {
        use super::super::*;

        #[test]
        fn bool() {
            let property = Property::Bool(false);

            let raw = property.as_raw().unwrap();
            assert_eq!(raw.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_BOOL));
            assert_eq!(raw.get_bool(), Ok(false));

            assert_eq!(Property::from_raw(raw), Ok(property));
        }

        #[test]
        fn double() {
            let property = Property::Double(45.0);

            let raw = property.as_raw().unwrap();
            assert_eq!(raw.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_DOUBLE));
            assert_eq!(raw.get_double(), Ok(45.0));

            assert_eq!(Property::from_raw(raw), Ok(property));
        }

        #[test]
        fn string() {
            let property = Property::String("test".into());

            let raw = property.as_raw().unwrap();
            assert_eq!(raw.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_STRING));
            assert_eq!(raw.get_string(), Ok("test".into()));

            assert_eq!(Property::from_raw(raw), Ok(property));
        }

        #[test]
        fn vector3d() {
            let property = Property::Vector3D([1.2, 3.4, 5.6]);

            let raw = property.as_raw().unwrap();
            assert_eq!(raw.get_kind(), Ok(chfl_property_kind::CHFL_PROPERTY_VECTOR3D));
            assert_eq!(raw.get_vector3d(), Ok([1.2, 3.4, 5.6]));

            assert_eq!(Property::from_raw(raw), Ok(property));
        }
    }
}