pixelsrc 0.2.0

Pixelsrc - GenAI-native pixel art format and compiler
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Configuration schema types for `pxl.toml`
//!
//! Defines the structure and validation rules for pixelsrc project configuration.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Validation severity level for config issues
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum ValidationLevel {
    /// Treat as error, fail build
    Error,
    /// Emit warning, continue build
    #[default]
    Warn,
    /// Silently ignore
    Ignore,
}

/// Sprite sheet layout direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum SheetLayout {
    /// Frames arranged left to right
    #[default]
    Horizontal,
    /// Frames arranged top to bottom
    Vertical,
    /// Frames arranged in a grid
    Grid,
}

/// Texture filter mode for exports
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum FilterMode {
    /// Nearest-neighbor (pixel-perfect)
    #[default]
    Point,
    /// Bilinear interpolation
    Bilinear,
}

/// Project metadata section
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectConfig {
    /// Project name (required)
    pub name: String,
    /// Project version
    #[serde(default = "default_version")]
    pub version: String,
    /// Source directory for .pxl files
    #[serde(default = "default_src")]
    pub src: PathBuf,
    /// Build output directory
    #[serde(default = "default_out")]
    pub out: PathBuf,
}

fn default_version() -> String {
    "0.1.0".to_string()
}

fn default_src() -> PathBuf {
    PathBuf::from("src/pxl")
}

fn default_out() -> PathBuf {
    PathBuf::from("build")
}

/// Default settings applied to all outputs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DefaultsConfig {
    /// Default scale factor
    #[serde(default = "default_scale")]
    pub scale: u32,
    /// Default padding between sprites
    #[serde(default = "default_padding")]
    pub padding: u32,
}

impl Default for DefaultsConfig {
    fn default() -> Self {
        Self { scale: default_scale(), padding: default_padding() }
    }
}

fn default_scale() -> u32 {
    1
}

fn default_padding() -> u32 {
    1
}

/// Atlas configuration for sprite packing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AtlasConfig {
    /// Glob patterns for sprite sources
    pub sources: Vec<String>,
    /// Maximum atlas dimensions [width, height]
    #[serde(default = "default_max_size")]
    pub max_size: [u32; 2],
    /// Padding between sprites (overrides defaults)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub padding: Option<u32>,
    /// Constrain to power-of-two dimensions
    #[serde(default)]
    pub power_of_two: bool,
    /// Preserve nine-slice metadata
    #[serde(default)]
    pub nine_slice: bool,
}

fn default_max_size() -> [u32; 2] {
    [1024, 1024]
}

/// Animation output configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnimationsConfig {
    /// Glob patterns for animation files
    #[serde(default = "default_animation_sources")]
    pub sources: Vec<String>,
    /// Generate preview GIFs
    #[serde(default)]
    pub preview: bool,
    /// Scale factor for previews
    #[serde(default = "default_scale")]
    pub preview_scale: u32,
    /// Layout direction for sprite sheets
    #[serde(default)]
    pub sheet_layout: SheetLayout,
}

impl Default for AnimationsConfig {
    fn default() -> Self {
        Self {
            sources: default_animation_sources(),
            preview: false,
            preview_scale: default_scale(),
            sheet_layout: SheetLayout::default(),
        }
    }
}

fn default_animation_sources() -> Vec<String> {
    vec!["animations/**".to_string()]
}

/// Generic JSON export configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericExportConfig {
    /// Enable this export
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Output format identifier
    #[serde(default = "default_json_format")]
    pub atlas_format: String,
}

fn default_true() -> bool {
    true
}

fn default_json_format() -> String {
    "json".to_string()
}

impl Default for GenericExportConfig {
    fn default() -> Self {
        Self { enabled: true, atlas_format: "json".to_string() }
    }
}

/// Godot export configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GodotExportConfig {
    /// Enable Godot export
    #[serde(default)]
    pub enabled: bool,
    /// Output format identifier
    #[serde(default = "default_godot_format")]
    pub atlas_format: String,
    /// Godot resource path prefix
    #[serde(default = "default_godot_resource_path")]
    pub resource_path: String,
    /// Generate AnimationPlayer resources
    #[serde(default = "default_true")]
    pub animation_player: bool,
    /// Generate SpriteFrames resources
    #[serde(default = "default_true")]
    pub sprite_frames: bool,
}

fn default_godot_format() -> String {
    "godot".to_string()
}

fn default_godot_resource_path() -> String {
    "res://assets/sprites".to_string()
}

/// Unity export configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UnityExportConfig {
    /// Enable Unity export
    #[serde(default)]
    pub enabled: bool,
    /// Output format identifier
    #[serde(default = "default_unity_format")]
    pub atlas_format: String,
    /// Pixels per unit setting
    #[serde(default = "default_pixels_per_unit")]
    pub pixels_per_unit: u32,
    /// Texture filter mode
    #[serde(default)]
    pub filter_mode: FilterMode,
    /// Generate texture .meta file with sprite slice data
    #[serde(default = "default_true")]
    pub generate_meta: bool,
    /// Generate .anim files for animations
    #[serde(default = "default_true")]
    pub generate_anim: bool,
    /// Generate JSON metadata (for custom import scripts)
    #[serde(default = "default_true")]
    pub generate_json: bool,
}

fn default_unity_format() -> String {
    "unity".to_string()
}

fn default_pixels_per_unit() -> u32 {
    16
}

/// libGDX export configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct LibGdxExportConfig {
    /// Enable libGDX export
    #[serde(default)]
    pub enabled: bool,
    /// Output format identifier
    #[serde(default = "default_libgdx_format")]
    pub atlas_format: String,
}

fn default_libgdx_format() -> String {
    "libgdx".to_string()
}

/// Export configurations container
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ExportsConfig {
    /// Generic JSON export
    #[serde(default)]
    pub generic: GenericExportConfig,
    /// Godot export
    #[serde(default)]
    pub godot: GodotExportConfig,
    /// Unity export
    #[serde(default)]
    pub unity: UnityExportConfig,
    /// libGDX export
    #[serde(default)]
    pub libgdx: LibGdxExportConfig,
}

/// Validation settings for the build process
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ValidateConfig {
    /// Treat warnings as errors
    #[serde(default)]
    pub strict: bool,
    /// How to handle unused palettes
    #[serde(default)]
    pub unused_palettes: ValidationLevel,
    /// How to handle missing references
    #[serde(default = "default_missing_refs")]
    pub missing_refs: ValidationLevel,
}

fn default_missing_refs() -> ValidationLevel {
    ValidationLevel::Error
}

/// Watch mode configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatchConfig {
    /// Debounce delay in milliseconds
    #[serde(default = "default_debounce_ms")]
    pub debounce_ms: u32,
    /// Clear terminal between rebuilds
    #[serde(default = "default_true")]
    pub clear_screen: bool,
}

fn default_debounce_ms() -> u32 {
    100
}

impl Default for WatchConfig {
    fn default() -> Self {
        Self { debounce_ms: 100, clear_screen: true }
    }
}

/// Complete pxl.toml configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PxlConfig {
    /// Project metadata (required)
    pub project: ProjectConfig,
    /// Default settings
    #[serde(default)]
    pub defaults: DefaultsConfig,
    /// Atlas definitions
    #[serde(default)]
    pub atlases: HashMap<String, AtlasConfig>,
    /// Animation settings
    #[serde(default)]
    pub animations: AnimationsConfig,
    /// Export configurations
    #[serde(default, rename = "export")]
    pub exports: ExportsConfig,
    /// Validation settings
    #[serde(default)]
    pub validate: ValidateConfig,
    /// Watch mode settings
    #[serde(default)]
    pub watch: WatchConfig,
}

/// Configuration validation error
#[derive(Debug, Clone)]
pub struct ConfigValidationError {
    /// Path to the invalid field (e.g., "atlases.characters.max_size")
    pub field: String,
    /// Error message
    pub message: String,
}

impl std::fmt::Display for ConfigValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "pxl.toml: '{}' {}", self.field, self.message)
    }
}

impl PxlConfig {
    /// Validate the configuration and return any errors
    pub fn validate(&self) -> Vec<ConfigValidationError> {
        let mut errors = Vec::new();

        // Validate project.name is non-empty
        if self.project.name.is_empty() {
            errors.push(ConfigValidationError {
                field: "project.name".to_string(),
                message: "must be a non-empty string".to_string(),
            });
        }

        // Validate defaults
        if self.defaults.scale == 0 {
            errors.push(ConfigValidationError {
                field: "defaults.scale".to_string(),
                message: "must be a positive integer".to_string(),
            });
        }

        // Validate atlases
        for (name, atlas) in &self.atlases {
            if atlas.sources.is_empty() {
                errors.push(ConfigValidationError {
                    field: format!("atlases.{}.sources", name),
                    message: "must contain at least one glob pattern".to_string(),
                });
            }

            if atlas.max_size[0] == 0 || atlas.max_size[1] == 0 {
                errors.push(ConfigValidationError {
                    field: format!("atlases.{}.max_size", name),
                    message: "dimensions must be positive".to_string(),
                });
            }
        }

        // Validate animations
        if self.animations.preview_scale == 0 {
            errors.push(ConfigValidationError {
                field: "animations.preview_scale".to_string(),
                message: "must be a positive integer".to_string(),
            });
        }

        // Validate unity export
        if self.exports.unity.enabled && self.exports.unity.pixels_per_unit == 0 {
            errors.push(ConfigValidationError {
                field: "export.unity.pixels_per_unit".to_string(),
                message: "must be a positive integer".to_string(),
            });
        }

        errors
    }

    /// Check if validation passed
    pub fn is_valid(&self) -> bool {
        self.validate().is_empty()
    }

    /// Get effective padding for an atlas (atlas-specific or default)
    pub fn effective_padding(&self, atlas: &AtlasConfig) -> u32 {
        atlas.padding.unwrap_or(self.defaults.padding)
    }
}

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

    #[test]
    fn test_minimal_config_parse() {
        let toml = r#"
[project]
name = "test-project"
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        assert_eq!(config.project.name, "test-project");
        assert_eq!(config.project.version, "0.1.0");
        assert_eq!(config.project.src, PathBuf::from("src/pxl"));
        assert_eq!(config.project.out, PathBuf::from("build"));
    }

    #[test]
    fn test_full_config_parse() {
        let toml = r#"
[project]
name = "full-project"
version = "1.0.0"
src = "assets/pxl"
out = "dist"

[defaults]
scale = 2
padding = 4

[atlases.characters]
sources = ["sprites/player/**", "sprites/enemies/**"]
max_size = [2048, 2048]
padding = 2
power_of_two = true

[animations]
sources = ["anims/**"]
preview = true
preview_scale = 4
sheet_layout = "vertical"

[export.godot]
enabled = true
resource_path = "res://sprites"

[export.unity]
enabled = true
pixels_per_unit = 32
filter_mode = "bilinear"

[validate]
strict = true
unused_palettes = "error"
missing_refs = "warn"

[watch]
debounce_ms = 200
clear_screen = false
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();

        assert_eq!(config.project.name, "full-project");
        assert_eq!(config.project.version, "1.0.0");
        assert_eq!(config.defaults.scale, 2);
        assert_eq!(config.defaults.padding, 4);

        let chars_atlas = config.atlases.get("characters").unwrap();
        assert_eq!(chars_atlas.sources.len(), 2);
        assert_eq!(chars_atlas.max_size, [2048, 2048]);
        assert_eq!(chars_atlas.padding, Some(2));
        assert!(chars_atlas.power_of_two);

        assert!(config.animations.preview);
        assert_eq!(config.animations.preview_scale, 4);
        assert_eq!(config.animations.sheet_layout, SheetLayout::Vertical);

        assert!(config.exports.godot.enabled);
        assert_eq!(config.exports.godot.resource_path, "res://sprites");

        assert!(config.exports.unity.enabled);
        assert_eq!(config.exports.unity.pixels_per_unit, 32);
        assert_eq!(config.exports.unity.filter_mode, FilterMode::Bilinear);

        assert!(config.validate.strict);
        assert_eq!(config.validate.unused_palettes, ValidationLevel::Error);
        assert_eq!(config.validate.missing_refs, ValidationLevel::Warn);

        assert_eq!(config.watch.debounce_ms, 200);
        assert!(!config.watch.clear_screen);
    }

    #[test]
    fn test_validation_empty_name() {
        let toml = r#"
[project]
name = ""
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        let errors = config.validate();
        assert!(!errors.is_empty());
        assert!(errors.iter().any(|e| e.field == "project.name"));
    }

    #[test]
    fn test_validation_zero_scale() {
        let toml = r#"
[project]
name = "test"

[defaults]
scale = 0
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        let errors = config.validate();
        assert!(errors.iter().any(|e| e.field == "defaults.scale"));
    }

    #[test]
    fn test_validation_empty_atlas_sources() {
        let toml = r#"
[project]
name = "test"

[atlases.empty]
sources = []
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        let errors = config.validate();
        assert!(errors.iter().any(|e| e.field == "atlases.empty.sources"));
    }

    #[test]
    fn test_validation_zero_max_size() {
        let toml = r#"
[project]
name = "test"

[atlases.bad]
sources = ["sprites/**"]
max_size = [0, 1024]
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        let errors = config.validate();
        assert!(errors.iter().any(|e| e.field == "atlases.bad.max_size"));
    }

    #[test]
    fn test_effective_padding() {
        let toml = r#"
[project]
name = "test"

[defaults]
padding = 4

[atlases.with_padding]
sources = ["a/**"]
padding = 2

[atlases.without_padding]
sources = ["b/**"]
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();

        let with = config.atlases.get("with_padding").unwrap();
        let without = config.atlases.get("without_padding").unwrap();

        assert_eq!(config.effective_padding(with), 2);
        assert_eq!(config.effective_padding(without), 4);
    }

    #[test]
    fn test_validation_level_serde() {
        let toml = r#"
[project]
name = "test"

[validate]
unused_palettes = "error"
missing_refs = "ignore"
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        assert_eq!(config.validate.unused_palettes, ValidationLevel::Error);
        assert_eq!(config.validate.missing_refs, ValidationLevel::Ignore);
    }

    #[test]
    fn test_sheet_layout_serde() {
        let toml = r#"
[project]
name = "test"

[animations]
sheet_layout = "grid"
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        assert_eq!(config.animations.sheet_layout, SheetLayout::Grid);
    }

    #[test]
    fn test_filter_mode_serde() {
        let toml = r#"
[project]
name = "test"

[export.unity]
enabled = true
filter_mode = "bilinear"
"#;
        let config: PxlConfig = toml::from_str(toml).unwrap();
        assert_eq!(config.exports.unity.filter_mode, FilterMode::Bilinear);
    }
}