validation: move validation crate to crates folder

This commit is contained in:
Simon Broeng Jensen
2025-02-03 23:10:16 +01:00
committed by nitnelave
parent b5e87c7226
commit 37a683dcb2
6 changed files with 3 additions and 2 deletions
+9
View File
@@ -0,0 +1,9 @@
[package]
authors = ["Simon Broeng Jensen <sbj@cwconsult.dk>"]
description = "Validation logic for LLDAP"
edition = "2021"
homepage = "https://github.com/lldap/lldap"
license = "GPL-3.0-only"
name = "lldap_validation"
repository = "https://github.com/lldap/lldap"
version = "0.6.0"
+45
View File
@@ -0,0 +1,45 @@
// Description of allowed characters. Intended for error messages.
pub const ALLOWED_CHARACTERS_DESCRIPTION: &str = "a-z, A-Z, 0-9, and dash (-)";
pub fn validate_attribute_name(attribute_name: &str) -> Result<(), Vec<char>> {
let invalid_chars: Vec<char> = attribute_name
.chars()
.filter(|c| !(c.is_alphanumeric() || *c == '-'))
.collect();
if invalid_chars.is_empty() {
Ok(())
} else {
Err(invalid_chars)
}
}
mod tests {
#[test]
fn test_valid_attribute_name() {
let valid1: String = "AttrName-01".to_string();
let result = super::validate_attribute_name(&valid1);
assert!(result == Ok(()));
}
#[test]
fn test_invalid_attribute_name_chars() {
fn test_invalid_char(c: char) {
let prefix: String = "AttrName".to_string();
let suffix: String = "01".to_string();
let name: String = format!("{prefix}{c}{suffix}");
let result = super::validate_attribute_name(&name);
match result {
Ok(()) => {
panic!()
}
Err(invalid) => {
assert!(invalid == vec![c.to_owned()]);
}
}
}
test_invalid_char(' ');
test_invalid_char('_');
test_invalid_char('#');
}
}
+3
View File
@@ -0,0 +1,3 @@
#![forbid(non_ascii_idents)]
pub mod attributes;