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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! # Component Utilities
//!
//! This module provides utility functions for working with components and component
//! definitions, including validation, creation, and schema handling.
//!
//! ## Key Features
//!
//! - **Component Creation**: Safe creation of component types with validation
//! - **Schema Validation**: Comprehensive JSON schema validation utilities
//! - **Error Handling**: User-friendly error messages for common component operations
//! - **JSON Parsing**: Robust parsing of JSON schemas and component data
//!
//! ## Usage Examples
//!
//! ```rust
//! use stigmergy::component_utils;
//! use serde_json::json;
//!
//! // Create and validate a component definition
//! let schema = json!({"type": "object", "properties": {"x": {"type": "number"}}});
//! let definition = component_utils::create_and_validate_definition("Position", schema).unwrap();
//!
//! // Parse JSON schema from string
//! let schema_str = r#"{"type": "string"}"#;
//! let schema = component_utils::parse_schema(schema_str).unwrap();
//! ```
use Value;
use crate::;
/// Creates a Component from a name string with validation.
///
/// This function validates that the component name follows Rust identifier
/// conventions before creating the Component instance.
///
/// # Arguments
/// * `name` - The component name to validate and use
///
/// # Returns
/// * `Ok(Component)` - Successfully created component
/// * `Err(String)` - Validation error with descriptive message
///
/// # Examples
/// ```
/// use stigmergy::component_utils::create_component;
///
/// let component = create_component("Position").unwrap();
/// assert!(create_component("invalid-name").is_err());
/// ```
/// Creates and validates a ComponentDefinition with comprehensive schema checking.
///
/// This function combines component creation and schema validation into a single
/// operation, ensuring both the component name and schema are valid before
/// returning a ComponentDefinition.
///
/// # Arguments
/// * `name` - The component name to validate
/// * `schema` - The JSON schema to validate and use
///
/// # Returns
/// * `Ok(ComponentDefinition)` - Successfully created and validated definition
/// * `Err(String)` - Validation error describing the specific problem
///
/// # Examples
/// ```
/// use stigmergy::component_utils::create_and_validate_definition;
/// use serde_json::json;
///
/// let schema = json!({"type": "object", "required": ["x", "y"]});
/// let definition = create_and_validate_definition("Position", schema).unwrap();
/// ```
/// Validates a JSON schema by creating a temporary component definition.
///
/// This function performs schema validation without permanently creating a
/// component definition, useful for checking schema validity before storage.
///
/// # Arguments
/// * `component_id` - The component identifier to use for validation
/// * `schema` - The JSON schema to validate
///
/// # Returns
/// * `Ok(())` - Schema is valid
/// * `Err(String)` - Validation error with specific details
///
/// # Examples
/// ```
/// use stigmergy::component_utils::validate_schema_for_component;
/// use serde_json::json;
///
/// let schema = json!({"type": "number"});
/// assert!(validate_schema_for_component("Health", &schema).is_ok());
/// ```
/// Parses a JSON schema from a string with comprehensive error handling.
///
/// This function provides user-friendly error messages for JSON parsing failures,
/// making it easier to debug schema definition problems.
///
/// # Arguments
/// * `schema_str` - The JSON schema string to parse
///
/// # Returns
/// * `Ok(Value)` - Successfully parsed JSON schema
/// * `Err(String)` - Parse error with descriptive message
///
/// # Examples
/// ```
/// use stigmergy::component_utils::parse_schema;
///
/// let valid_schema = parse_schema(r#"{"type": "string"}"#).unwrap();
/// assert!(parse_schema("invalid json").is_err());
/// ```
/// Parses JSON data from a string with comprehensive error handling.
///
/// This function provides user-friendly error messages for JSON parsing failures,
/// specifically designed for component data validation.
///
/// # Arguments
/// * `data_str` - The JSON data string to parse
///
/// # Returns
/// * `Ok(Value)` - Successfully parsed JSON data
/// * `Err(String)` - Parse error with descriptive message
///
/// # Examples
/// ```
/// use stigmergy::component_utils::parse_json_data;
///
/// let valid_data = parse_json_data(r#"{"x": 10, "y": 20}"#).unwrap();
/// assert!(parse_json_data("invalid json").is_err());
/// ```