castep-cell-io 0.2.12

A crate helping to parse, edit and save `castep` input file format `.cell`
Documentation
use std::fmt::Display;

use castep_periodic_table::{data::ELEMENT_TABLE, element::ElementSymbol, element::LookupElement};

use crate::formatting::BlockDisplay;

use super::SpeciesCharacter;

#[derive(Debug, Clone)]
pub struct SpeciesPot {
    element: ElementSymbol,
    pot_file: String,
}

#[derive(Debug, Clone)]
pub struct SpeciesPotBlock {
    items: Vec<SpeciesPot>,
}

impl SpeciesPotBlock {
    pub fn new(items: Vec<SpeciesPot>) -> Self {
        Self { items }
    }
    pub fn from_elements(elements: &[ElementSymbol]) -> Self {
        Self::new(
            elements
                .iter()
                .map(|&elm| SpeciesPot::from_element(elm))
                .collect(),
        )
    }
}

impl SpeciesPot {
    pub fn new(element: ElementSymbol, pot_file: String) -> Self {
        Self { element, pot_file }
    }
}

impl SpeciesCharacter for SpeciesPot {
    type Output = SpeciesPot;

    fn from_element(element: ElementSymbol) -> Self::Output {
        Self::new(
            element,
            ELEMENT_TABLE.get_by_symbol(element).potential().to_string(),
        )
    }
}

impl Display for SpeciesPot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:>8}  {}", format!("{}", self.element), self.pot_file)
    }
}

impl BlockDisplay for SpeciesPotBlock {
    fn block_tag(&self) -> String {
        "SPECIES_POT".to_string()
    }

    fn entries(&self) -> String {
        self.items
            .iter()
            .map(|p| format!("{p}"))
            .collect::<Vec<String>>()
            .join("\n")
    }
}

impl Display for SpeciesPotBlock {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.content())
    }
}