ron2 0.3.0

RON parser with full AST access, perfect round-trip fidelity, and formatting tools
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
//! Comprehensive tests for ron-schema storage module.
//!
//! Tests cover:
//! - write_schema and read_schema roundtrip
//! - find_schema with different path configurations
//! - type_path_to_file_path edge cases
//! - Environment variable override
//! - XDG fallback

#![cfg(feature = "derive")]

use std::{env, ffi::OsStr, fs, path::PathBuf};

use ron2::schema::{
    Field, SCHEMA_DIR_ENV, Schema, TypeKind, find_schema_in, read_schema,
    storage::resolve_schema_dir, write_schema,
};
use serial_test::serial;

/// Create a temporary directory for testing.
fn create_temp_dir() -> tempfile::TempDir {
    tempfile::tempdir().expect("Failed to create temp directory")
}

fn set_schema_dir(value: impl AsRef<OsStr>) {
    // Safe in tests that run serially and don't spawn threads touching env vars.
    unsafe {
        env::set_var(SCHEMA_DIR_ENV, value);
    }
}

fn clear_schema_dir() {
    // Safe in tests that run serially and don't spawn threads touching env vars.
    unsafe {
        env::remove_var(SCHEMA_DIR_ENV);
    }
}

// ============================================================================
// type_path_to_file_path tests
// ============================================================================

#[test]
fn test_type_path_to_file_path_simple_type() {
    use ron2::schema::storage::type_path_to_file_path;

    let path = type_path_to_file_path("MyType");
    assert_eq!(path, PathBuf::from("MyType.schema.ron"));
}

#[test]
fn test_type_path_to_file_path_single_module() {
    use ron2::schema::storage::type_path_to_file_path;

    let path = type_path_to_file_path("my_crate::MyType");
    assert_eq!(path, PathBuf::from("my_crate/MyType.schema.ron"));
}

#[test]
fn test_type_path_to_file_path_nested_modules() {
    use ron2::schema::storage::type_path_to_file_path;

    let path = type_path_to_file_path("my_crate::config::AppConfig");
    assert_eq!(path, PathBuf::from("my_crate/config/AppConfig.schema.ron"));
}

#[test]
fn test_type_path_to_file_path_deeply_nested() {
    use ron2::schema::storage::type_path_to_file_path;

    let path = type_path_to_file_path("a::b::c::d::e::Type");
    assert_eq!(path, PathBuf::from("a/b/c/d/e/Type.schema.ron"));
}

#[test]
fn test_type_path_to_file_path_with_underscores() {
    use ron2::schema::storage::type_path_to_file_path;

    let path = type_path_to_file_path("my_crate::my_module::MyType");
    assert_eq!(path, PathBuf::from("my_crate/my_module/MyType.schema.ron"));
}

#[test]
fn test_type_path_to_file_path_with_numbers() {
    use ron2::schema::storage::type_path_to_file_path;

    let path = type_path_to_file_path("v2::api::Response");
    assert_eq!(path, PathBuf::from("v2/api/Response.schema.ron"));
}

// ============================================================================
// write_schema and read_schema roundtrip tests
// ============================================================================

#[test]
fn test_write_and_read_schema_simple() {
    let temp_dir = create_temp_dir();
    let schema = Schema::new(TypeKind::Bool);

    let path = write_schema("test::Simple", &schema, Some(temp_dir.path())).unwrap();

    assert!(path.exists());
    assert!(path.ends_with("test/Simple.schema.ron"));

    let read_back = read_schema(&path).unwrap();
    assert_eq!(schema, read_back);
}

#[test]
fn test_write_and_read_schema_with_doc() {
    let temp_dir = create_temp_dir();
    let schema = Schema::with_doc("A documented type", TypeKind::String);

    let path = write_schema("documented::Type", &schema, Some(temp_dir.path())).unwrap();

    let read_back = read_schema(&path).unwrap();
    assert_eq!(schema.doc, read_back.doc);
    assert_eq!(schema.kind, read_back.kind);
}

#[test]
fn test_write_and_read_schema_complex() {
    let temp_dir = create_temp_dir();
    let schema = Schema::with_doc(
        "Application configuration",
        TypeKind::Struct {
            fields: vec![
                Field::new("port", TypeKind::U16).with_doc("Server port"),
                Field::optional("host", TypeKind::String).with_doc("Hostname"),
                Field::new("tags", TypeKind::List(Box::new(TypeKind::String))),
            ],
        },
    );

    let path = write_schema("my_app::config::Config", &schema, Some(temp_dir.path())).unwrap();

    assert!(path.exists());
    assert!(path.ends_with("my_app/config/Config.schema.ron"));

    let read_back = read_schema(&path).unwrap();
    assert_eq!(schema, read_back);
}

#[test]
fn test_write_creates_parent_directories() {
    let temp_dir = create_temp_dir();
    let schema = Schema::new(TypeKind::I32);

    // Write to deeply nested path
    let path = write_schema("a::b::c::d::e::DeepType", &schema, Some(temp_dir.path())).unwrap();

    assert!(path.exists());
    assert!(path.ends_with("a/b/c/d/e/DeepType.schema.ron"));
}

#[test]
fn test_write_overwrites_existing_file() {
    let temp_dir = create_temp_dir();

    let schema1 = Schema::new(TypeKind::I32);
    let schema2 = Schema::new(TypeKind::String);

    let path1 = write_schema("test::Overwrite", &schema1, Some(temp_dir.path())).unwrap();
    let path2 = write_schema("test::Overwrite", &schema2, Some(temp_dir.path())).unwrap();

    assert_eq!(path1, path2);

    let read_back = read_schema(&path1).unwrap();
    // Should have the second schema's type
    assert_eq!(read_back.kind, TypeKind::String);
}

#[test]
fn test_read_nonexistent_file_fails() {
    let result = read_schema(&PathBuf::from("/nonexistent/path/schema.ron"));
    assert!(result.is_err());
}

#[test]
fn test_read_invalid_ron_fails() {
    let temp_dir = create_temp_dir();
    let file_path = temp_dir.path().join("invalid.schema.ron");
    fs::write(&file_path, "this is not valid RON {{{").unwrap();

    let result = read_schema(&file_path);
    assert!(result.is_err());
}

// ============================================================================
// find_schema_in tests
// ============================================================================

#[test]
fn test_find_schema_in_directory() {
    let temp_dir = create_temp_dir();
    let schema = Schema::new(TypeKind::Bool);

    write_schema("findme::Type", &schema, Some(temp_dir.path())).unwrap();

    let found = find_schema_in("findme::Type", temp_dir.path()).unwrap();
    assert_eq!(schema, found);
}

#[test]
fn test_find_schema_not_found_in_directory() {
    let temp_dir = create_temp_dir();

    let result = find_schema_in("nonexistent::Type", temp_dir.path());
    // Should fail since it's not in the temp dir and not in the default location
    assert!(result.is_err());
}

// ============================================================================
// Environment variable tests
// ============================================================================

#[test]
#[serial]
fn test_resolve_schema_dir_with_env_var() {
    let temp_dir = create_temp_dir();
    let temp_path = temp_dir.path().to_string_lossy().to_string();

    // Store original value
    let original = env::var(SCHEMA_DIR_ENV).ok();

    // Set environment variable
    set_schema_dir(&temp_path);

    let resolved = resolve_schema_dir().unwrap();
    assert_eq!(resolved, PathBuf::from(&temp_path));

    // Restore original value
    match original {
        Some(val) => set_schema_dir(val),
        None => clear_schema_dir(),
    }
}

#[test]
#[serial]
fn test_resolve_schema_dir_without_env_var_uses_xdg() {
    // Store original value
    let original = env::var(SCHEMA_DIR_ENV).ok();

    // Remove environment variable
    clear_schema_dir();

    let resolved = resolve_schema_dir();

    // Should succeed with XDG fallback (on most systems)
    // The exact path depends on the system, but it should end with "ron-schemas"
    if let Ok(path) = resolved {
        let path_str = path.to_string_lossy();
        assert!(
            path_str.ends_with("ron-schemas"),
            "Expected path to end with 'ron-schemas', got: {}",
            path_str
        );
    }

    // Restore original value
    if let Some(val) = original {
        set_schema_dir(val);
    }
}

// ============================================================================
// Schema file format tests
// ============================================================================

#[test]
fn test_written_schema_is_human_readable() {
    let temp_dir = create_temp_dir();
    let schema = Schema::with_doc(
        "Test schema",
        TypeKind::Struct {
            fields: vec![
                Field::new("name", TypeKind::String),
                Field::new("value", TypeKind::I32),
            ],
        },
    );

    let path = write_schema("format::Test", &schema, Some(temp_dir.path())).unwrap();
    let contents = fs::read_to_string(&path).unwrap();

    // Pretty-printed RON should be multi-line and readable
    assert!(contents.contains('\n'));
    assert!(contents.contains("doc:"));
    assert!(contents.contains("Test schema"));
    assert!(contents.contains("Struct"));
    assert!(contents.contains("name"));
    assert!(contents.contains("value"));
}

#[test]
fn test_schema_file_can_be_manually_edited_and_read() {
    let temp_dir = create_temp_dir();
    let file_path = temp_dir.path().join("manual.schema.ron");

    // Write a manually crafted schema file
    // Note: RON requires Some(...) syntax for Option fields when present
    let manual_content = r#"(
    doc: Some("Manually written schema"),
    kind: Struct(
        fields: [
            (
                name: "id",
                ty: U64,
            ),
            (
                name: "data",
                ty: String,
                optional: true,
            ),
        ],
    ),
)"#;

    fs::write(&file_path, manual_content).unwrap();

    let schema = read_schema(&file_path).unwrap();

    assert_eq!(schema.doc, Some("Manually written schema".to_string()));

    match &schema.kind {
        TypeKind::Struct { fields } => {
            assert_eq!(fields.len(), 2);
            assert_eq!(fields[0].name, "id");
            assert_eq!(fields[0].ty, TypeKind::U64);
            assert!(!fields[0].optional);
            assert_eq!(fields[1].name, "data");
            assert_eq!(fields[1].ty, TypeKind::String);
            assert!(fields[1].optional);
        }
        _ => panic!("Expected Struct type kind"),
    }
}

// ============================================================================
// Edge case tests
// ============================================================================

#[test]
fn test_type_path_with_empty_string() {
    use ron2::schema::storage::type_path_to_file_path;

    // Edge case: empty string should produce just the extension
    let path = type_path_to_file_path("");
    assert_eq!(path, PathBuf::from(".schema.ron"));
}

#[test]
fn test_write_schema_with_special_characters_in_type_name() {
    // Note: Rust type names can't contain special characters,
    // but we test with underscores and numbers which are valid
    let temp_dir = create_temp_dir();
    let schema = Schema::new(TypeKind::Bool);

    let path = write_schema(
        "my_crate_2::v2_api::Type_V2",
        &schema,
        Some(temp_dir.path()),
    )
    .unwrap();

    assert!(path.exists());
    let read_back = read_schema(&path).unwrap();
    assert_eq!(schema, read_back);
}

#[test]
fn test_multiple_schemas_in_same_directory() {
    let temp_dir = create_temp_dir();

    let schema1 = Schema::new(TypeKind::I32);
    let schema2 = Schema::new(TypeKind::String);
    let schema3 = Schema::new(TypeKind::Bool);

    write_schema("multi::Type1", &schema1, Some(temp_dir.path())).unwrap();
    write_schema("multi::Type2", &schema2, Some(temp_dir.path())).unwrap();
    write_schema("multi::Type3", &schema3, Some(temp_dir.path())).unwrap();

    let found1 = find_schema_in("multi::Type1", temp_dir.path()).unwrap();
    let found2 = find_schema_in("multi::Type2", temp_dir.path()).unwrap();
    let found3 = find_schema_in("multi::Type3", temp_dir.path()).unwrap();

    assert_eq!(schema1, found1);
    assert_eq!(schema2, found2);
    assert_eq!(schema3, found3);
}

#[test]
fn test_write_enum_schema() {
    use ron2::schema::Variant;

    let temp_dir = create_temp_dir();
    let schema = Schema::with_doc(
        "Status enum",
        TypeKind::Enum {
            variants: vec![
                Variant::unit("Pending"),
                Variant::unit("Running"),
                Variant::struct_variant("Completed", vec![Field::new("result", TypeKind::String)]),
                Variant::struct_variant("Failed", vec![Field::new("error", TypeKind::String)]),
            ],
        },
    );

    let path = write_schema("status::Status", &schema, Some(temp_dir.path())).unwrap();

    let read_back = read_schema(&path).unwrap();
    assert_eq!(schema, read_back);
}