mod error;
mod fields_map;
mod schema;

pub use error::{Error, SResult};
pub use fields_map::FieldsMap;
pub use schema::Schema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct IndexedPos(pub u16);

impl IndexedPos {
    pub const fn new(value: u16) -> IndexedPos {
        IndexedPos(value)
    }

    pub const fn min() -> IndexedPos {
        IndexedPos(u16::min_value())
    }

    pub const fn max() -> IndexedPos {
        IndexedPos(u16::max_value())
    }
}

impl From<u16> for IndexedPos {
    fn from(value: u16) -> IndexedPos {
        IndexedPos(value)
    }
}

impl Into<u16> for IndexedPos {
    fn into(self) -> u16 {
        self.0
    }
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct FieldId(pub u16);

impl FieldId {
    pub const fn new(value: u16) -> FieldId {
        FieldId(value)
    }

    pub const fn min() -> FieldId {
        FieldId(u16::min_value())
    }

    pub const fn max() -> FieldId {
        FieldId(u16::max_value())
    }

    pub fn next(self) -> SResult<FieldId> {
        self.0.checked_add(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded)
    }
}

impl From<u16> for FieldId {
    fn from(value: u16) -> FieldId {
        FieldId(value)
    }
}

impl Into<u16> for FieldId {
    fn into(self) -> u16 {
        self.0
    }
}
