whydrogen 0.1.0

A slightly opinioated search query parser/lexer.
Documentation
// SPDX-FileContributor: Slatian
//
// SPDX-License-Identifier: LGPL-3.0-only

use unicode_properties::GeneralCategory;
use unicode_properties::GeneralCategoryGroup;
use unicode_properties::UnicodeGeneralCategory;

/// A token end is communicated either by a space character or by there not being any character at all. 
#[inline]
pub fn is_token_end(c: Option<char>) -> bool {
	match c {
		Some(c) => {
			return is_space_char(c);
		},
		None => {
			return true;
		}
	}
}

/// Keywords may start with any unicode letter or number character.
pub fn is_keyword_safe_first_char(c: char) -> bool {
	match c.general_category_group() {
		GeneralCategoryGroup::Letter | GeneralCategoryGroup::Number => {
			return true;
		},
		_ => {},
	}
	return false;
}

/// Inside keywords any unicode, letter or number is allowed. In addition to that, the following characters are allowed: `_`, `-`, `.`
///
/// Since this is pretty English-centric the list of additionally allowed characters will be expanded in the future. 
pub fn is_keyword_safe_char(c: char) -> bool {
	match c {
		'_'|'-'|'.' => {
			return true;
		},
		_ => {}
	}
	return is_keyword_safe_first_char(c);
}

/// Returns whether the given character `c` is a unicode space type character.
#[inline]
pub fn is_space_char(c: char) -> bool {
	match c.general_category() {
		GeneralCategory::SpaceSeparator => true,
		GeneralCategory::LineSeparator => true,
		GeneralCategory::ParagraphSeparator => true,
		GeneralCategory::Control => true,
		GeneralCategory::Format => true,
		_ => false
	}
}

/// Convenience wrapper: Inverted version of `is_space_char`
#[inline]
pub fn is_non_space_char(c: char) -> bool {
	return !is_space_char(c);
}