Skip to main content

neo_devpack_solidity/
validation.rs

1//! Input Validation Module
2//!
3//! Validates compiler inputs and configurations.
4
5use crate::error::CompilerError;
6
7/// Validation result
8pub type ValidationResult<T> = Result<T, Vec<CompilerError>>;
9
10/// Input validator
11#[derive(Default)]
12pub struct InputValidator {
13    errors: Vec<CompilerError>,
14}
15
16impl InputValidator {
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    pub fn validate_source(&mut self, source: &str) -> bool {
22        if source.is_empty() {
23            self.errors
24                .push(CompilerError::ParseError("Empty source file".to_string()));
25            return false;
26        }
27        if source.len() > 10_000_000 {
28            self.errors.push(CompilerError::ParseError(
29                "Source file too large (>10MB)".to_string(),
30            ));
31            return false;
32        }
33        true
34    }
35
36    /// Validate that raw bytes are valid UTF-8 before use as source.
37    pub fn validate_utf8<'a>(&mut self, raw: &'a [u8]) -> Option<&'a str> {
38        match std::str::from_utf8(raw) {
39            Ok(s) => Some(s),
40            Err(e) => {
41                self.errors.push(CompilerError::ParseError(format!(
42                    "Source is not valid UTF-8: {e}"
43                )));
44                None
45            }
46        }
47    }
48
49    /// Validate an import path, rejecting path traversal attempts.
50    pub fn validate_import_path(&mut self, path: &str) -> bool {
51        if path.contains("..") {
52            self.errors.push(CompilerError::ParseError(format!(
53                "Import path contains disallowed '..': '{path}'"
54            )));
55            return false;
56        }
57        true
58    }
59
60    pub fn errors(&self) -> &[CompilerError] {
61        &self.errors
62    }
63
64    pub fn has_errors(&self) -> bool {
65        !self.errors.is_empty()
66    }
67}