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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Chemfiles, a modern library for chemistry file reading and writing
// Copyright (C) 2015-2018 Guillaume Fraux -- BSD licensed
use std::marker::PhantomData;

use chemfiles_sys::*;

use crate::errors::{check_not_null, check_success};
use crate::property::{PropertiesIter, Property, RawProperty};
use crate::strings;

/// An `Atom` is a particle in the current `Frame`. It stores the following
/// atomic properties:
///
/// - atom name;
/// - atom type;
/// - atom mass;
/// - atom charge.
///
/// The atom name is usually an unique identifier (`H1`, `C_a`) while the
/// atom type will be shared between all particles of the same type: `H`,
/// `Ow`, `CH3`.
#[derive(Debug)]
pub struct Atom {
    handle: *mut CHFL_ATOM,
}

/// An analog to a reference to an atom (`&Atom`)
#[derive(Debug)]
pub struct AtomRef<'a> {
    inner: Atom,
    marker: PhantomData<&'a Atom>,
}

impl<'a> std::ops::Deref for AtomRef<'a> {
    type Target = Atom;
    fn deref(&self) -> &Atom {
        &self.inner
    }
}

/// An analog to a mutable reference to an atom (`&mut Atom`)
#[derive(Debug)]
pub struct AtomMut<'a> {
    inner: Atom,
    marker: PhantomData<&'a mut Atom>,
}

impl<'a> std::ops::Deref for AtomMut<'a> {
    type Target = Atom;
    fn deref(&self) -> &Atom {
        &self.inner
    }
}

impl<'a> std::ops::DerefMut for AtomMut<'a> {
    fn deref_mut(&mut self) -> &mut Atom {
        &mut self.inner
    }
}

impl Clone for Atom {
    fn clone(&self) -> Atom {
        unsafe {
            let new_handle = chfl_atom_copy(self.as_ptr());
            Atom::from_ptr(new_handle)
        }
    }
}

impl Atom {
    /// Create an owned `Atom` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the
    /// pointer.
    #[inline]
    pub(crate) unsafe fn from_ptr(ptr: *mut CHFL_ATOM) -> Atom {
        check_not_null(ptr);
        Atom { handle: ptr }
    }

    /// Create a borrowed `Atom` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the
    /// pointer, and the caller is responsible for setting the right lifetime
    #[inline]
    pub(crate) unsafe fn ref_from_ptr<'a>(ptr: *const CHFL_ATOM) -> AtomRef<'a> {
        AtomRef {
            inner: Atom::from_ptr(ptr as *mut CHFL_ATOM),
            marker: PhantomData,
        }
    }

    /// Create a mutably borrowed `Atom` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the
    /// pointer, and the caller is responsible for setting the right lifetime
    #[inline]
    pub(crate) unsafe fn ref_mut_from_ptr<'a>(ptr: *mut CHFL_ATOM) -> AtomMut<'a> {
        AtomMut {
            inner: Atom::from_ptr(ptr),
            marker: PhantomData,
        }
    }

    /// Get the underlying C pointer as a const pointer.
    #[inline]
    pub(crate) fn as_ptr(&self) -> *const CHFL_ATOM {
        self.handle
    }

    /// Get the underlying C pointer as a mutable pointer.
    #[inline]
    pub(crate) fn as_mut_ptr(&mut self) -> *mut CHFL_ATOM {
        self.handle
    }

    /// Create an atom with the given `name`, and set the atom type to `name`.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let atom = Atom::new("He");
    /// assert_eq!(atom.name(), "He");
    /// ```
    pub fn new<'a>(name: impl Into<&'a str>) -> Atom {
        let buffer = strings::to_c(name.into());
        unsafe {
            let handle = chfl_atom(buffer.as_ptr());
            Atom::from_ptr(handle)
        }
    }

    /// Get the atom mass, in atomic mass units.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let atom = Atom::new("He");
    /// assert_eq!(atom.mass(), 4.002602);
    /// ```
    pub fn mass(&self) -> f64 {
        let mut mass = 0.0;
        unsafe {
            check_success(chfl_atom_mass(self.as_ptr(), &mut mass));
        }
        return mass;
    }

    /// Set the atom mass to `mass`, in atomic mass units.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let mut atom = Atom::new("He");
    ///
    /// atom.set_mass(34.9);
    /// assert_eq!(atom.mass(), 34.9);
    /// ```
    pub fn set_mass(&mut self, mass: f64) {
        unsafe {
            check_success(chfl_atom_set_mass(self.as_mut_ptr(), mass));
        }
    }

    /// Get the atom charge, in number of the electron charge *e*.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let atom = Atom::new("He");
    /// assert_eq!(atom.charge(), 0.0);
    /// ```
    pub fn charge(&self) -> f64 {
        let mut charge = 0.0;
        unsafe {
            check_success(chfl_atom_charge(self.as_ptr(), &mut charge));
        }
        return charge;
    }

    /// Set the atom charge to `charge`, in number of the electron charge *e*.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let mut atom = Atom::new("He");
    ///
    /// atom.set_charge(-2.0);
    /// assert_eq!(atom.charge(), -2.0);
    /// ```
    pub fn set_charge(&mut self, charge: f64) {
        unsafe {
            check_success(chfl_atom_set_charge(self.as_mut_ptr(), charge));
        }
    }

    /// Get the atom name.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let atom = Atom::new("He");
    /// assert_eq!(atom.name(), "He");
    /// ```
    pub fn name(&self) -> String {
        let get_name = |ptr, len| unsafe { chfl_atom_name(self.as_ptr(), ptr, len) };
        let name = strings::call_autogrow_buffer(10, get_name).expect("getting name failed");
        return strings::from_c(name.as_ptr());
    }

    /// Get the atom type.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let atom = Atom::new("He");
    /// assert_eq!(atom.atomic_type(), "He");
    /// ```
    pub fn atomic_type(&self) -> String {
        let get_type = |ptr, len| unsafe { chfl_atom_type(self.as_ptr(), ptr, len) };
        let buffer = strings::call_autogrow_buffer(10, get_type).expect("getting type failed");
        return strings::from_c(buffer.as_ptr());
    }

    /// Set the atom name to `name`.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let mut atom = Atom::new("He");
    ///
    /// atom.set_name("Zn3");
    /// assert_eq!(atom.name(), "Zn3");
    /// ```
    pub fn set_name<'a>(&mut self, name: impl Into<&'a str>) {
        let buffer = strings::to_c(name.into());
        unsafe {
            check_success(chfl_atom_set_name(self.as_mut_ptr(), buffer.as_ptr()));
        }
    }

    /// Set the atom type to `atomic_type`.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let mut atom = Atom::new("He");
    ///
    /// atom.set_atomic_type("F");
    /// assert_eq!(atom.atomic_type(), "F");
    /// ```
    pub fn set_atomic_type<'a>(&mut self, atomic_type: impl Into<&'a str>) {
        let buffer = strings::to_c(atomic_type.into());
        unsafe {
            check_success(chfl_atom_set_type(self.as_mut_ptr(), buffer.as_ptr()));
        }
    }

    /// Try to get the full name of the atom from the atomic type. For example,
    /// the full name of "He" is "Helium", and so on. If the name can not be
    /// found, this function returns the empty string.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// let atom = Atom::new("Zn");
    /// assert_eq!(atom.full_name(), "Zinc");
    /// ```
    pub fn full_name(&self) -> String {
        let get_full_name = |ptr, len| unsafe { chfl_atom_full_name(self.as_ptr(), ptr, len) };
        let name = strings::call_autogrow_buffer(10, get_full_name).expect("getting full name failed");
        return strings::from_c(name.as_ptr());
    }

    /// Try to get the Van der Waals radius of the atom from the atomic type.
    /// If the radius can not be found, returns 0.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// assert_eq!(Atom::new("He").vdw_radius(), 1.4);
    /// assert_eq!(Atom::new("Xxx").vdw_radius(), 0.0);
    /// ```
    pub fn vdw_radius(&self) -> f64 {
        let mut radius: f64 = 0.0;
        unsafe {
            check_success(chfl_atom_vdw_radius(self.as_ptr(), &mut radius));
        }
        return radius;
    }

    /// Try to get the covalent radius of the atom from the atomic type. If the
    /// radius can not be found, returns 0.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// assert_eq!(Atom::new("He").covalent_radius(), 0.32);
    /// assert_eq!(Atom::new("Xxx").covalent_radius(), 0.0);
    /// ```
    pub fn covalent_radius(&self) -> f64 {
        let mut radius: f64 = 0.0;
        unsafe {
            check_success(chfl_atom_covalent_radius(self.as_ptr(), &mut radius));
        }
        return radius;
    }

    /// Try to get the atomic number of the atom from the atomic type. If the
    /// number can not be found, returns 0.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::Atom;
    /// assert_eq!(Atom::new("He").atomic_number(), 2);
    /// assert_eq!(Atom::new("Xxx").atomic_number(), 0);
    /// ```
    pub fn atomic_number(&self) -> u64 {
        let mut number = 0;
        unsafe {
            check_success(chfl_atom_atomic_number(self.as_ptr(), &mut number));
        }
        return number;
    }

    /// Add a new `property` with the given `name` to this atom.
    ///
    /// If a property with the same name already exists, this function override
    /// the existing property with the new one.
    ///
    /// # Examples
    /// ```
    /// # use chemfiles::{Atom, Property};
    /// let mut atom = Atom::new("He");
    /// atom.set("a bool", true);
    /// atom.set("a string", "test");
    ///
    /// assert_eq!(atom.get("a bool"), Some(Property::Bool(true)));
    /// assert_eq!(atom.get("a string"), Some(Property::String("test".into())));
    /// ```
    pub fn set(&mut self, name: &str, property: impl Into<Property>) {
        let buffer = strings::to_c(name);
        let property = property.into().as_raw();
        unsafe {
            check_success(chfl_atom_set_property(
                self.as_mut_ptr(),
                buffer.as_ptr(),
                property.as_ptr(),
            ));
        }
    }

    /// Get a property with the given `name` in this atom, if it exist.
    ///
    /// # Examples
    /// ```
    /// # use chemfiles::{Atom, Property};
    /// let mut atom = Atom::new("He");
    /// atom.set("foo", Property::Double(22.2));
    ///
    /// assert_eq!(atom.get("foo"), Some(Property::Double(22.2)));
    /// assert_eq!(atom.get("Bar"), None);
    /// ```
    pub fn get(&self, name: &str) -> Option<Property> {
        let buffer = strings::to_c(name);
        unsafe {
            let handle = chfl_atom_get_property(self.as_ptr(), buffer.as_ptr());
            if handle.is_null() {
                None
            } else {
                let raw = RawProperty::from_ptr(handle);
                let property = Property::from_raw(raw);
                Some(property)
            }
        }
    }

    /// Get an iterator over all (name, property) pairs for this atom
    ///
    /// # Examples
    /// ```
    /// # use chemfiles::{Atom, Property};
    /// let mut atom = Atom::new("He");
    /// atom.set("foo", Property::Double(22.2));
    /// atom.set("bar", Property::Bool(false));
    ///
    /// for (name, property) in atom.properties() {
    ///     if name == "foo" {
    ///         assert_eq!(property, Property::Double(22.2));
    ///     } else if name == "bar" {
    ///         assert_eq!(property, Property::Bool(false));
    ///     }
    /// }
    /// ```
    pub fn properties(&self) -> PropertiesIter {
        let mut count = 0;
        unsafe {
            check_success(chfl_atom_properties_count(self.as_ptr(), &mut count));
        }

        #[allow(clippy::cast_possible_truncation)]
        let size = count as usize;
        let mut c_names = vec![std::ptr::null_mut(); size];
        unsafe {
            check_success(chfl_atom_list_properties(self.as_ptr(), c_names.as_mut_ptr(), count));
        }

        let mut names = Vec::new();
        for ptr in c_names {
            names.push(strings::from_c(ptr));
        }

        PropertiesIter {
            names: names.into_iter(),
            getter: Box::new(move |name| self.get(name).expect("failed to get property")),
        }
    }
}

impl Drop for Atom {
    fn drop(&mut self) {
        unsafe {
            let _ = chfl_free(self.as_ptr().cast());
        }
    }
}

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

    #[test]
    fn clone() {
        let mut atom = Atom::new("He");
        assert_eq!(atom.name(), "He");

        let copy = atom.clone();
        assert_eq!(copy.name(), "He");

        atom.set_name("Na");
        assert_eq!(atom.name(), "Na");
        assert_eq!(copy.name(), "He");
    }

    #[test]
    fn mass() {
        let mut atom = Atom::new("He");
        approx::assert_ulps_eq!(atom.mass(), 4.002602);
        atom.set_mass(15.0);
        assert_eq!(atom.mass(), 15.0);
    }

    #[test]
    fn charge() {
        let mut atom = Atom::new("He");
        assert_eq!(atom.charge(), 0.0);
        atom.set_charge(-1.5);
        assert_eq!(atom.charge(), -1.5);
    }

    #[test]
    fn name() {
        let mut atom = Atom::new("He");
        assert_eq!(atom.name(), "He");
        atom.set_name("Zn-12");
        assert_eq!(atom.name(), "Zn-12");
    }

    #[test]
    fn atomic_type() {
        let mut atom = Atom::new("He");
        assert_eq!(atom.atomic_type(), "He");
        atom.set_atomic_type("Zn");
        assert_eq!(atom.atomic_type(), "Zn");
    }

    #[test]
    fn full_name() {
        let mut atom = Atom::new("He");
        assert_eq!(atom.full_name(), "Helium");

        atom.set_atomic_type("Zn");
        assert_eq!(atom.full_name(), "Zinc");

        let atom = Atom::new("Unknown");
        assert_eq!(atom.full_name(), "");
    }

    #[test]
    fn radii() {
        let atom = Atom::new("He");
        approx::assert_ulps_eq!(atom.vdw_radius(), 1.4);
        approx::assert_ulps_eq!(atom.covalent_radius(), 0.32);

        let atom = Atom::new("Unknown");
        assert_eq!(atom.vdw_radius(), 0.0);
        assert_eq!(atom.covalent_radius(), 0.0);
    }

    #[test]
    fn atomic_number() {
        let atom = Atom::new("He");
        assert_eq!(atom.atomic_number(), 2);

        let atom = Atom::new("Unknown");
        assert_eq!(atom.atomic_number(), 0);
    }

    #[test]
    fn property() {
        let mut atom = Atom::new("F");

        atom.set("foo", -22.0);
        assert_eq!(atom.get("foo"), Some(Property::Double(-22.0)));
        assert_eq!(atom.get("bar"), None);

        atom.set("bar", Property::String("here".into()));
        for (name, property) in atom.properties() {
            if name == "foo" {
                assert_eq!(property, Property::Double(-22.0));
            } else if name == "bar" {
                assert_eq!(property, Property::String("here".into()));
            }
        }
    }
}