lgx_runtime/core/
validator.rs

1use crate::models::{ParsedCode, ValidationError, ValidationResult};
2use anyhow::Result;
3use tracing::debug;
4
5pub struct Validator {
6    // Validation rules and configuration
7}
8
9impl Validator {
10    pub fn new() -> Self {
11        Self {}
12    }
13
14    pub async fn validate(&self, parsed: &ParsedCode) -> Result<ValidationResult> {
15        debug!("Validating parsed code with {} nodes", parsed.nodes.len());
16        
17        let mut errors = Vec::new();
18        
19        for node in &parsed.nodes {
20            if let Some(error) = self.validate_node(node).await? {
21                errors.push(error);
22            }
23        }
24        
25        let is_valid = errors.is_empty();
26        
27        Ok(ValidationResult {
28            is_valid,
29            errors,
30            warnings: Vec::new(),
31        })
32    }
33    
34    async fn validate_node(&self, node: &crate::models::ASTNode) -> Result<Option<ValidationError>> {
35        match node {
36            crate::models::ASTNode::Create { entity, fields, line } => {
37                if entity.is_empty() {
38                    return Ok(Some(ValidationError {
39                        message: "CREATE statement must specify an entity".to_string(),
40                        line: *line,
41                        severity: "error".to_string(),
42                    }));
43                }
44                
45                if fields.is_empty() {
46                    return Ok(Some(ValidationError {
47                        message: "CREATE statement must specify fields".to_string(),
48                        line: *line,
49                        severity: "error".to_string(),
50                    }));
51                }
52            }
53            
54            crate::models::ASTNode::Read { entity, conditions: _, line } => {
55                if entity.is_empty() {
56                    return Ok(Some(ValidationError {
57                        message: "READ statement must specify an entity".to_string(),
58                        line: *line,
59                        severity: "error".to_string(),
60                    }));
61                }
62            }
63            
64            crate::models::ASTNode::Update { entity, fields, conditions: _, line } => {
65                if entity.is_empty() {
66                    return Ok(Some(ValidationError {
67                        message: "UPDATE statement must specify an entity".to_string(),
68                        line: *line,
69                        severity: "error".to_string(),
70                    }));
71                }
72                
73                if fields.is_empty() {
74                    return Ok(Some(ValidationError {
75                        message: "UPDATE statement must specify fields to update".to_string(),
76                        line: *line,
77                        severity: "error".to_string(),
78                    }));
79                }
80            }
81            
82            crate::models::ASTNode::Delete { entity, conditions: _, line } => {
83                if entity.is_empty() {
84                    return Ok(Some(ValidationError {
85                        message: "DELETE statement must specify an entity".to_string(),
86                        line: *line,
87                        severity: "error".to_string(),
88                    }));
89                }
90            }
91            
92            crate::models::ASTNode::Function { name, parameters: _, body, line } => {
93                if name.is_empty() {
94                    return Ok(Some(ValidationError {
95                        message: "FUNCTION statement must specify a name".to_string(),
96                        line: *line,
97                        severity: "error".to_string(),
98                    }));
99                }
100                
101                if body.is_empty() {
102                    return Ok(Some(ValidationError {
103                        message: "FUNCTION statement must specify a body".to_string(),
104                        line: *line,
105                        severity: "error".to_string(),
106                    }));
107                }
108            }
109        }
110        
111        Ok(None)
112    }
113}