oxihuman-export 0.1.2

Export pipeline for OxiHuman — glTF, COLLADA, STL, and streaming formats
Documentation
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! JSON Schema generation for parameter/config export validation.
//!
//! Provides a lightweight, dependency-free JSON Schema builder that can
//! serialise schema definitions to JSON strings for downstream validation.

// ── Types ─────────────────────────────────────────────────────────────────────

/// JSON Schema primitive and composite types.
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SchemaType {
    /// An object with named properties.
    Object,
    /// An ordered list of values.
    Array,
    /// A UTF-8 string value.
    String,
    /// A numeric value (integer or float).
    Number,
    /// A boolean true/false value.
    Boolean,
    /// The JSON null value.
    Null,
}

/// A single property definition within a JSON Schema object.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct SchemaProperty {
    /// Property key name.
    pub name: String,
    /// The JSON type of this property.
    pub schema_type: SchemaType,
    /// Optional human-readable description.
    pub description: Option<String>,
    /// Optional default value as a JSON-encoded string.
    pub default_value: Option<String>,
    /// Whether this property is required.
    pub required: bool,
}

/// A complete JSON Schema definition.
#[allow(dead_code)]
#[derive(Debug, Clone, Default)]
pub struct JsonSchema {
    /// Schema title (maps to `"title"` key).
    pub title: String,
    /// Schema description.
    pub description: String,
    /// Root type of the schema.
    pub root_type: Option<SchemaType>,
    /// Properties (valid when root_type is Object).
    pub properties: Vec<SchemaProperty>,
    /// Names of required properties.
    pub required_fields: Vec<String>,
    /// Optional enum values as JSON-encoded strings.
    pub enum_values: Vec<String>,
}

/// Configuration for schema export behaviour.
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct SchemaExportConfig {
    /// Include `$schema` declaration in output.
    pub include_schema_declaration: bool,
    /// Indent level for pretty-printing (0 = compact).
    pub indent: usize,
    /// Schema draft version string.
    pub draft_version: String,
}

// ── Constructors ──────────────────────────────────────────────────────────────

/// Returns a default [`SchemaExportConfig`].
#[allow(dead_code)]
pub fn default_schema_config() -> SchemaExportConfig {
    SchemaExportConfig {
        include_schema_declaration: true,
        indent: 2,
        draft_version: "https://json-schema.org/draft/2020-12/schema".to_string(),
    }
}

/// Creates a new empty [`JsonSchema`] with the given title.
#[allow(dead_code)]
pub fn new_json_schema(title: &str) -> JsonSchema {
    JsonSchema {
        title: title.to_string(),
        root_type: Some(SchemaType::Object),
        ..Default::default()
    }
}

// ── Mutation helpers ───────────────────────────────────────────────────────────

/// Adds a property to the schema.
#[allow(dead_code)]
pub fn add_property(schema: &mut JsonSchema, prop: SchemaProperty) {
    if prop.required && !schema.required_fields.contains(&prop.name) {
        schema.required_fields.push(prop.name.clone());
    }
    // Replace if name already exists
    if let Some(pos) = schema.properties.iter().position(|p| p.name == prop.name) {
        schema.properties[pos] = prop;
    } else {
        schema.properties.push(prop);
    }
}

/// Marks a field name as required (adds to required list if not already present).
#[allow(dead_code)]
pub fn add_required_field(schema: &mut JsonSchema, field_name: &str) {
    if !schema.required_fields.contains(&field_name.to_string()) {
        schema.required_fields.push(field_name.to_string());
    }
}

/// Sets the schema title.
#[allow(dead_code)]
pub fn set_schema_title(schema: &mut JsonSchema, title: &str) {
    schema.title = title.to_string();
}

/// Returns the schema title.
#[allow(dead_code)]
pub fn schema_title(schema: &JsonSchema) -> &str {
    &schema.title
}

// ── Counting helpers ───────────────────────────────────────────────────────────

/// Returns the number of properties defined on the schema.
#[allow(dead_code)]
pub fn schema_property_count(schema: &JsonSchema) -> usize {
    schema.properties.len()
}

/// Returns the number of required fields.
#[allow(dead_code)]
pub fn required_field_count(schema: &JsonSchema) -> usize {
    schema.required_fields.len()
}

// ── Serialisation ─────────────────────────────────────────────────────────────

fn type_name(t: &SchemaType) -> &'static str {
    match t {
        SchemaType::Object => "object",
        SchemaType::Array => "array",
        SchemaType::String => "string",
        SchemaType::Number => "number",
        SchemaType::Boolean => "boolean",
        SchemaType::Null => "null",
    }
}

/// Serialises the schema to a JSON string.
///
/// Uses a simple hand-rolled formatter to avoid external dependencies.
#[allow(dead_code)]
pub fn schema_to_json(schema: &JsonSchema, config: &SchemaExportConfig) -> String {
    let indent = config.indent;
    let i1 = " ".repeat(indent);
    let i2 = " ".repeat(indent * 2);
    let i3 = " ".repeat(indent * 3);

    let mut parts: Vec<String> = Vec::new();

    if config.include_schema_declaration {
        parts.push(format!("{}\"$schema\": \"{}\"", i1, config.draft_version));
    }

    if !schema.title.is_empty() {
        parts.push(format!("{}\"title\": \"{}\"", i1, schema.title));
    }
    if !schema.description.is_empty() {
        parts.push(format!("{}\"description\": \"{}\"", i1, schema.description));
    }

    if let Some(rt) = &schema.root_type {
        parts.push(format!("{}\"type\": \"{}\"", i1, type_name(rt)));
    }

    if !schema.enum_values.is_empty() {
        let vals = schema.enum_values.join(", ");
        parts.push(format!("{}\"enum\": [{}]", i1, vals));
    }

    if !schema.properties.is_empty() {
        let mut prop_parts: Vec<String> = Vec::new();
        for prop in &schema.properties {
            let mut lines: Vec<String> = Vec::new();
            lines.push(format!(
                "{}\"type\": \"{}\"",
                i3,
                type_name(&prop.schema_type)
            ));
            if let Some(desc) = &prop.description {
                lines.push(format!("{}\"description\": \"{}\"", i3, desc));
            }
            if let Some(dv) = &prop.default_value {
                lines.push(format!("{}\"default\": {}", i3, dv));
            }
            prop_parts.push(format!(
                "{}\"{}\": {{\n{}\n{}}}",
                i2,
                prop.name,
                lines.join(",\n"),
                i2
            ));
        }
        parts.push(format!(
            "{}\"properties\": {{\n{}\n{}}}",
            i1,
            prop_parts.join(",\n"),
            i1
        ));
    }

    if !schema.required_fields.is_empty() {
        let req: Vec<String> = schema
            .required_fields
            .iter()
            .map(|r| format!("\"{}\"", r))
            .collect();
        parts.push(format!("{}\"required\": [{}]", i1, req.join(", ")));
    }

    format!("{{\n{}\n}}", parts.join(",\n"))
}

// ── Validation ─────────────────────────────────────────────────────────────────

/// Performs basic type-checking of a value string against a [`SchemaType`].
///
/// Accepts JSON-encoded value strings (e.g. `"42"`, `"true"`, `"\"hello\""`)
/// and returns `true` when the value matches the expected type.
#[allow(dead_code)]
pub fn validate_against_schema(schema_type: &SchemaType, value: &str) -> bool {
    let v = value.trim();
    match schema_type {
        SchemaType::Object => v.starts_with('{') && v.ends_with('}'),
        SchemaType::Array => v.starts_with('[') && v.ends_with(']'),
        SchemaType::String => v.starts_with('"') && v.ends_with('"'),
        SchemaType::Number => v.parse::<f64>().is_ok(),
        SchemaType::Boolean => v == "true" || v == "false",
        SchemaType::Null => v == "null",
    }
}

// ── Schema merging ─────────────────────────────────────────────────────────────

/// Merges `other` into `base`, adding properties and required fields.
///
/// Properties with duplicate names from `other` overwrite those in `base`.
#[allow(dead_code)]
pub fn merge_schemas(base: &mut JsonSchema, other: &JsonSchema) {
    for prop in &other.properties {
        add_property(base, prop.clone());
    }
    for req in &other.required_fields {
        add_required_field(base, req);
    }
}

// ── Convenience builders ───────────────────────────────────────────────────────

/// Builds a [`JsonSchema`] from an iterator of `(name, SchemaType)` pairs.
#[allow(dead_code)]
pub fn schema_from_pairs(title: &str, pairs: &[(&str, SchemaType)]) -> JsonSchema {
    let mut schema = new_json_schema(title);
    for (name, t) in pairs {
        add_property(
            &mut schema,
            SchemaProperty {
                name: name.to_string(),
                schema_type: t.clone(),
                description: None,
                default_value: None,
                required: false,
            },
        );
    }
    schema
}

/// Creates a schema that represents a closed enum of string values.
#[allow(dead_code)]
pub fn enum_schema(title: &str, values: &[&str]) -> JsonSchema {
    let mut schema = JsonSchema {
        title: title.to_string(),
        root_type: Some(SchemaType::String),
        enum_values: values.iter().map(|v| format!("\"{}\"", v)).collect(),
        ..Default::default()
    };
    schema.description = format!("Enum: {}", values.join(", "));
    schema
}

// ── Tests ──────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn make_prop(name: &str, t: SchemaType) -> SchemaProperty {
        SchemaProperty {
            name: name.to_string(),
            schema_type: t,
            description: None,
            default_value: None,
            required: false,
        }
    }

    #[test]
    fn test_default_schema_config_has_declaration() {
        let cfg = default_schema_config();
        assert!(cfg.include_schema_declaration);
    }

    #[test]
    fn test_default_schema_config_indent() {
        let cfg = default_schema_config();
        assert_eq!(cfg.indent, 2);
    }

    #[test]
    fn test_new_json_schema_title() {
        let s = new_json_schema("MySchema");
        assert_eq!(schema_title(&s), "MySchema");
    }

    #[test]
    fn test_new_json_schema_empty_properties() {
        let s = new_json_schema("Empty");
        assert_eq!(schema_property_count(&s), 0);
    }

    #[test]
    fn test_add_property_increments_count() {
        let mut s = new_json_schema("Test");
        add_property(&mut s, make_prop("x", SchemaType::Number));
        assert_eq!(schema_property_count(&s), 1);
    }

    #[test]
    fn test_add_property_required_auto_registers() {
        let mut s = new_json_schema("Test");
        add_property(
            &mut s,
            SchemaProperty {
                required: true,
                ..make_prop("y", SchemaType::String)
            },
        );
        assert_eq!(required_field_count(&s), 1);
        assert!(s.required_fields.contains(&"y".to_string()));
    }

    #[test]
    fn test_add_property_replaces_duplicate() {
        let mut s = new_json_schema("Test");
        add_property(&mut s, make_prop("z", SchemaType::Boolean));
        add_property(&mut s, make_prop("z", SchemaType::Number));
        assert_eq!(schema_property_count(&s), 1);
        assert_eq!(s.properties[0].schema_type, SchemaType::Number);
    }

    #[test]
    fn test_add_required_field() {
        let mut s = new_json_schema("Test");
        add_required_field(&mut s, "name");
        assert_eq!(required_field_count(&s), 1);
    }

    #[test]
    fn test_add_required_field_no_duplicates() {
        let mut s = new_json_schema("Test");
        add_required_field(&mut s, "id");
        add_required_field(&mut s, "id");
        assert_eq!(required_field_count(&s), 1);
    }

    #[test]
    fn test_set_and_get_schema_title() {
        let mut s = new_json_schema("Old");
        set_schema_title(&mut s, "New");
        assert_eq!(schema_title(&s), "New");
    }

    #[test]
    fn test_schema_to_json_contains_title() {
        let s = new_json_schema("Config");
        let cfg = default_schema_config();
        let json = schema_to_json(&s, &cfg);
        assert!(json.contains("\"title\": \"Config\""));
    }

    #[test]
    fn test_schema_to_json_contains_type() {
        let s = new_json_schema("Config");
        let cfg = default_schema_config();
        let json = schema_to_json(&s, &cfg);
        assert!(json.contains("\"type\": \"object\""));
    }

    #[test]
    fn test_validate_against_schema_number_ok() {
        assert!(validate_against_schema(&SchemaType::Number, "3.14"));
    }

    #[test]
    fn test_validate_against_schema_number_fail() {
        assert!(!validate_against_schema(&SchemaType::Number, "\"hello\""));
    }

    #[test]
    fn test_validate_against_schema_boolean() {
        assert!(validate_against_schema(&SchemaType::Boolean, "true"));
        assert!(validate_against_schema(&SchemaType::Boolean, "false"));
        assert!(!validate_against_schema(&SchemaType::Boolean, "1"));
    }

    #[test]
    fn test_validate_against_schema_null() {
        assert!(validate_against_schema(&SchemaType::Null, "null"));
        assert!(!validate_against_schema(&SchemaType::Null, "false"));
    }

    #[test]
    fn test_merge_schemas() {
        let mut base = new_json_schema("Base");
        add_property(&mut base, make_prop("a", SchemaType::String));

        let mut other = new_json_schema("Other");
        add_property(&mut other, make_prop("b", SchemaType::Number));
        add_required_field(&mut other, "b");

        merge_schemas(&mut base, &other);
        assert_eq!(schema_property_count(&base), 2);
        assert!(base.required_fields.contains(&"b".to_string()));
    }

    #[test]
    fn test_schema_from_pairs() {
        let pairs = [("width", SchemaType::Number), ("label", SchemaType::String)];
        let s = schema_from_pairs("Cfg", &pairs);
        assert_eq!(schema_property_count(&s), 2);
    }

    #[test]
    fn test_enum_schema_values() {
        let s = enum_schema("Mode", &["fast", "slow", "medium"]);
        assert_eq!(s.enum_values.len(), 3);
        assert_eq!(s.root_type, Some(SchemaType::String));
    }
}