1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use dialoguer::Validator;
use lazy_static::lazy_static;
use regex::Regex;
use thiserror::Error;
lazy_static! {
static ref NAMESPACE_VALIDATOR: Regex = Regex::new(r#"^[a-z0-9_\.\-:]+$"#).unwrap();
static ref NAME_VALIDATOR: Regex = Regex::new(r#"^\w{3,16}$"#).unwrap();
static ref DATAPACK_NAME_VALIDATOR: Regex = Regex::new(r#"^[\w\d\s]+$"#).unwrap();
}
#[derive(Debug, Error)]
pub enum ValidatorError {
#[error("Namespace '{0}' must follow this regex: /{}/", NAMESPACE_VALIDATOR.as_str())]
InvalidNamespace(String),
#[error("Name '{0}' must be within 3-16 character and must all be english characters")]
InvalidName(String),
#[error("Datapack Name '{0}' contain non-english character or special symbol")]
InvalidDatapackName(String),
}
pub struct NamespaceValidator;
impl Validator for NamespaceValidator {
type Err = ValidatorError;
fn validate(&self, text: &str) -> Result<(), Self::Err> {
if NAMESPACE_VALIDATOR.is_match(&text) {
Ok(())
} else {
Err(ValidatorError::InvalidNamespace(text.to_owned()))
}
}
}
pub struct NameValidator;
impl Validator for NameValidator {
type Err = ValidatorError;
fn validate(&self, text: &str) -> Result<(), Self::Err> {
if NAME_VALIDATOR.is_match(&text) {
Ok(())
} else {
Err(ValidatorError::InvalidName(text.to_owned()))
}
}
}
pub struct DatapackNameValidator;
impl Validator for DatapackNameValidator {
type Err = ValidatorError;
fn validate(&self, text: &str) -> Result<(), Self::Err> {
if DATAPACK_NAME_VALIDATOR.is_match(&text) {
Ok(())
} else {
Err(ValidatorError::InvalidDatapackName(text.to_owned()))
}
}
}