dmsc/java/classes/
validation.rs

1//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
2//!
3//! This file is part of DMSC.
4//! The DMSC project belongs to the Dunimd Team.
5//!
6//! Licensed under the Apache License, Version 2.0 (the "License");
7//! You may not use this file except in compliance with the License.
8//! You may obtain a copy of the License at
9//!
10//!     http://www.apache.org/licenses/LICENSE-2.0
11//!
12//! Unless required by applicable law or agreed to in writing, software
13//! distributed under the License is distributed on an "AS IS" BASIS,
14//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15//! See the License for the specific language governing permissions and
16//! limitations under the License.
17
18//! # Validation Module JNI Bindings
19//!
20//! JNI bindings for DMSC validation classes.
21
22use jni::JNIEnv;
23use jni::objects::JClass;
24use jni::sys::{jlong, jboolean};
25use crate::validation::{DMSCValidatorBuilder, DMSCValidationResult};
26use crate::java::exception::check_not_null;
27
28#[no_mangle]
29pub extern "system" fn Java_com_dunimd_dmsc_validation_DMSCValidationModule_nativeValidateEmail(
30    mut env: JNIEnv,
31    _class: JClass,
32    value: jni::objects::JString,
33) -> jlong {
34    let value_str: String = env.get_string(&value)
35        .expect("Failed to get email value")
36        .into();
37    
38    let result = DMSCValidatorBuilder::new("email")
39        .is_email()
40        .max_length(255)
41        .build()
42        .validate_value(Some(&value_str));
43    Box::into_raw(Box::new(result)) as jlong
44}
45
46#[no_mangle]
47pub extern "system" fn Java_com_dunimd_dmsc_validation_DMSCValidationModule_nativeValidateUsername(
48    mut env: JNIEnv,
49    _class: JClass,
50    value: jni::objects::JString,
51) -> jlong {
52    let value_str: String = env.get_string(&value)
53        .expect("Failed to get username value")
54        .into();
55    
56    let result = DMSCValidatorBuilder::new("username")
57        .not_empty()
58        .min_length(3)
59        .max_length(32)
60        .alphanumeric()
61        .build()
62        .validate_value(Some(&value_str));
63    Box::into_raw(Box::new(result)) as jlong
64}
65
66#[no_mangle]
67pub extern "system" fn Java_com_dunimd_dmsc_validation_DMSCValidationResult_isValid(
68    mut env: JNIEnv,
69    _class: JClass,
70    ptr: jlong,
71) -> jboolean {
72    if !check_not_null(&mut env, ptr, "DMSCValidationResult") {
73        return 0;
74    }
75    
76    let result = unsafe { &*(ptr as *const DMSCValidationResult) };
77    result.is_valid as jboolean
78}
79
80#[no_mangle]
81pub extern "system" fn Java_com_dunimd_dmsc_validation_DMSCValidationResult_free0(
82    _env: JNIEnv,
83    _class: JClass,
84    ptr: jlong,
85) {
86    if ptr != 0 {
87        unsafe {
88            let _ = Box::from_raw(ptr as *mut DMSCValidationResult);
89        }
90    }
91}
92
93#[no_mangle]
94pub extern "system" fn Java_com_dunimd_dmsc_validation_DMSCValidatorBuilder_new0(
95    mut env: JNIEnv,
96    _class: JClass,
97    field_name: jni::objects::JString,
98) -> jlong {
99    let name: String = env.get_string(&field_name)
100        .expect("Failed to get field name")
101        .into();
102    
103    let builder = DMSCValidatorBuilder::new(&name);
104    Box::into_raw(Box::new(builder)) as jlong
105}
106
107#[no_mangle]
108pub extern "system" fn Java_com_dunimd_dmsc_validation_DMSCValidatorBuilder_build(
109    mut env: JNIEnv,
110    _class: JClass,
111    ptr: jlong,
112) -> jlong {
113    if !check_not_null(&mut env, ptr, "DMSCValidatorBuilder") {
114        return 0;
115    }
116    
117    let builder = unsafe { Box::from_raw(ptr as *mut DMSCValidatorBuilder) };
118    let runner = builder.build();
119    Box::into_raw(Box::new(runner)) as jlong
120}
121
122#[no_mangle]
123pub extern "system" fn Java_com_dunimd_dmsc_validation_DMSCValidatorBuilder_free0(
124    _env: JNIEnv,
125    _class: JClass,
126    ptr: jlong,
127) {
128    if ptr != 0 {
129        unsafe {
130            let _ = Box::from_raw(ptr as *mut DMSCValidatorBuilder);
131        }
132    }
133}