dia-semver 11.0.1

For handling Semantic Versions 2.0.0
Documentation
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Dia-Semver

Copyright (C) 2018-2022  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2018-2022".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # `FromStr`

use {
    core::{
        ops::Bound,
        str::FromStr,
    },
    crate::{Error, Result, Range, Semver},
};

/// # Mark size in bytes
const MARK_SIZE: usize = 1;

#[derive(Debug)]
enum OpenMark {
    Inclusive,
    Exclusive,
}

impl OpenMark {

    /// # Makes a Bound from self and a semver
    fn make_bound(self, semver: Option<Semver>) -> Result<Bound<Semver>> {
        match semver {
            None => match self {
                OpenMark::Inclusive => Ok(Bound::Unbounded),
                OpenMark::Exclusive => Err(err!("Start index of unbounded range must be inclusive")),
            },
            Some(semver) => match self {
                OpenMark::Inclusive => Ok(Bound::Included(semver)),
                OpenMark::Exclusive => Ok(Bound::Excluded(semver)),
            },
        }
    }

}

#[derive(Debug)]
enum CloseMark {
    Inclusive,
    Exclusive,
}

impl CloseMark {

    /// # Makes a Bound from self and a semver
    fn make_bound(self, semver: Option<Semver>) -> Result<Bound<Semver>> {
        match semver {
            None => match self {
                CloseMark::Inclusive => Ok(Bound::Unbounded),
                CloseMark::Exclusive => Err(err!("End index of unbounded range must be inclusive")),
            },
            Some(semver) => match self {
                CloseMark::Inclusive => Ok(Bound::Included(semver)),
                CloseMark::Exclusive => Ok(Bound::Excluded(semver)),
            },
        }
    }

}

#[test]
fn test_mark() {
    for c in &[
        crate::range::INCLUSIVE_OPEN,
        crate::range::INCLUSIVE_CLOSE,
        crate::range::EXCLUSIVE_OPEN,
        crate::range::EXCLUSIVE_CLOSE,
    ] {
        assert_eq!(c.len_utf8(), MARK_SIZE);
    }
}

impl FromStr for Range {

    type Err = Error;

    fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
        #[cfg(target_pointer_width = "8")]
        const MAX_LEN: usize = 255;

        #[cfg(not(target_pointer_width = "8"))]
        const MAX_LEN: usize = 4096;

        if s.len() > MAX_LEN {
            return Err(err!("String is too long, max length supported: {} bytes", MAX_LEN));
        }

        let err = || err!("Invalid range: {:?}", s);

        let open_mark = match s.chars().next() {
            Some(crate::range::INCLUSIVE_OPEN) => OpenMark::Inclusive,
            Some(crate::range::EXCLUSIVE_OPEN) => OpenMark::Exclusive,
            _ => return Err(err()),
        };
        let close_mark = if s.ends_with(crate::range::INCLUSIVE_CLOSE) {
            CloseMark::Inclusive
        } else if s.ends_with(crate::range::EXCLUSIVE_CLOSE) {
            CloseMark::Exclusive
        } else {
            return Err(err());
        };

        let mut parts = s[MARK_SIZE .. s.len() - MARK_SIZE].trim().split(',').map(|s| s.trim());
        let start = match parts.next() {
            None => return Err(err()),
            Some(start) => open_mark.make_bound(if start.is_empty() { None } else { Some(Semver::from_str(start).map_err(|_| err())?) })?,
        };
        let end = match parts.next() {
            None => return Err(err()),
            Some(end) => close_mark.make_bound(if end.is_empty() { None } else { Some(Semver::from_str(end).map_err(|_| err())?) })?,
        };
        if parts.next().is_none() {
            Ok(Self { start, end })
        } else {
            Err(err())
        }
    }

}