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
// Copyright 2018 Michael Lamparski
// Part of the vasp-poscar crate.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;
use crate::{Poscar, RawPoscar, ScaleLine, Coords};

impl fmt::Display for Poscar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
    { crate::write::display(f, self) }
}

fn display(w: &mut fmt::Formatter<'_>, poscar: &Poscar) -> fmt::Result
{
    let &Poscar(RawPoscar {
        scale, ref lattice_vectors, ref velocities, ref dynamics,
        ref comment, ref positions, ref group_counts, ref group_symbols,
        _cant_touch_this: (),
    }) = poscar;

    assert!(!comment.contains("\n"), "BUG");
    assert!(!comment.contains("\r"), "BUG");

    let style = FloatStyle::new(w);

    writeln!(w, "{}", comment)?;
    write!(w, "  ")?;
    match scale {
        ScaleLine::Factor(x) => {
            style.write_f64(w, x)?;
        },
        ScaleLine::Volume(x) => {
            write!(w, "-")?;
            style.write_f64(w, x)?;
        },
    }
    writeln!(w)?;

    for row in lattice_vectors {
        write!(w, "    ")?;
        style.write_v3(w, *row)?;
        writeln!(w)?;
    }

    if let Some(group_symbols) = group_symbols.as_ref() {
        write!(w, "  ")?;
        write_sep(&mut *w, " ", group_symbols.iter().map(|s| format!("{:>2}", s)))?;
        writeln!(w)?;
    }

    assert!(!group_counts.is_empty(), "BUG");
    write!(w, "  ")?;
    write_sep(&mut *w, " ", group_counts.iter().map(|&c| format!("{:>2}", c)))?;
    writeln!(w)?;

    if let &Some(_) = dynamics {
        writeln!(w, "Selective Dynamics")?;
    }

    match positions {
        &Coords::Cart(_) => writeln!(w, "Cartesian")?,
        &Coords::Frac(_) => writeln!(w, "Direct")?,
    }

    let positions = positions.as_ref().raw();
    for (i, pos) in positions.iter().enumerate() {
        write!(w, "  ")?;
        style.write_v3(w, *pos)?;
        if let &Some(ref dynamics) = dynamics {
            let fmt = |b| match b { true => 'T', false => 'F' };
            write!(w, " {}", By3(dynamics[i], fmt))?;
        }
        writeln!(w)?;
    }

    if let &Some(ref velocities) = velocities {
        match velocities {
            &Coords::Cart(_) => writeln!(w, "Cartesian")?,
            // (NOTE: typical appearance in CONTCAR; pymatgen expects this form)
            &Coords::Frac(_) => writeln!(w, "")?,
        }

        let velocities = velocities.as_ref().raw();
        for v in velocities {
            write!(w, "  ")?;
            style.write_v3(w, *v)?;
            writeln!(w)?;
        }
    }

    Ok(())
}

fn write_sep<W, Xs>(mut w: W, sep: &str, xs: Xs) -> fmt::Result
where
    W: fmt::Write,
    Xs: IntoIterator,
    Xs::Item: fmt::Display,
{
    let mut xs = xs.into_iter();
    if let Some(x) = xs.next() {
        write!(&mut w, "{}", x)?;
    }
    for x in xs {
        write!(&mut w, "{}{}", sep, x)?;
    }
    Ok(())
}

#[derive(Copy, Clone)]
enum FloatStyle { Dtoa, ForwardDisplay }
impl FloatStyle {
    fn new(f: &fmt::Formatter) -> Self {
        // Use Dtoa only for {}.
        if f.width().is_some() || f.precision().is_some() || f.sign_plus() || f.alternate() {
            FloatStyle::ForwardDisplay
        } else {
            FloatStyle::Dtoa
        }
    }

    // (directly implemented as a fn() -> fmt::Result instead of a Display type
    //  to ensure that we never forget to forward the flags)
    fn write_f64(self, f: &mut fmt::Formatter<'_>, value: f64) -> fmt::Result {
        match self {
            FloatStyle::ForwardDisplay => {
                fmt::Display::fmt(&value, f)
            },
            FloatStyle::Dtoa => {
                // not the most efficient thing in the world...
                let mut bytes = vec![];
                dtoa::write(&mut bytes, value).map_err(|_| fmt::Error)?;
                f.write_str(&String::from_utf8(bytes).unwrap())
            },
        }
    }

    fn write_v3(self, f: &mut fmt::Formatter<'_>, value: [f64; 3]) -> fmt::Result {
        self.write_f64(f, value[0])?;
        write!(f, " ")?;
        self.write_f64(f, value[1])?;
        write!(f, " ")?;
        self.write_f64(f, value[2])?;
        Ok(())
    }
}

// Formats three space-separated tokens after applying a conversion function to each.
struct By3<A, F>([A; 3], F);
impl<A, B, F> fmt::Display for By3<A, F>
where A: Clone,
      F: Fn(A) -> B,
      B: fmt::Display,
{
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        let By3(ref arr, ref f) = *self;
        write!(fmt, "{} {} {}", f(arr[0].clone()), f(arr[1].clone()), f(arr[2].clone()))
    }
}