superbook-pdf 0.1.0

High-quality PDF converter for scanned books with AI enhancement, deskew correction, and Japanese OCR
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Configuration file support for superbook-pdf
//!
//! Supports TOML configuration files with the following search order:
//! 1. `--config <path>` - explicitly specified path
//! 2. `./superbook.toml` - current directory
//! 3. `~/.config/superbook-pdf/config.toml` - user config
//! 4. Default values
//!
//! # Example Configuration
//!
//! ```toml
//! [general]
//! dpi = 300
//! threads = 4
//!
//! [processing]
//! deskew = true
//! margin_trim = 0.5
//!
//! [advanced]
//! internal_resolution = true
//! color_correction = true
//! ```

use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use thiserror::Error;

use crate::PipelineConfig;

/// Configuration file errors
#[derive(Debug, Error)]
pub enum ConfigError {
    /// IO error reading config file
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// TOML parse error
    #[error("TOML parse error: {0}")]
    TomlParse(#[from] toml::de::Error),

    /// File not found
    #[error("Config file not found: {0}")]
    NotFound(PathBuf),
}

/// General configuration options
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct GeneralConfig {
    /// Output DPI
    #[serde(default)]
    pub dpi: Option<u32>,

    /// Number of threads for parallel processing
    #[serde(default)]
    pub threads: Option<usize>,

    /// Verbosity level (0-2)
    #[serde(default)]
    pub verbose: Option<u8>,
}

/// Processing configuration options
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct ProcessingConfig {
    /// Enable deskew correction
    #[serde(default)]
    pub deskew: Option<bool>,

    /// Margin trim percentage
    #[serde(default)]
    pub margin_trim: Option<f64>,

    /// Enable AI upscaling
    #[serde(default)]
    pub upscale: Option<bool>,

    /// Enable GPU processing
    #[serde(default)]
    pub gpu: Option<bool>,
}

/// Advanced processing configuration
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct AdvancedConfig {
    /// Enable internal resolution normalization (4960x7016)
    #[serde(default)]
    pub internal_resolution: Option<bool>,

    /// Enable global color correction
    #[serde(default)]
    pub color_correction: Option<bool>,

    /// Enable page number offset alignment
    #[serde(default)]
    pub offset_alignment: Option<bool>,

    /// Output height in pixels
    #[serde(default)]
    pub output_height: Option<u32>,
}

/// OCR configuration
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct OcrConfig {
    /// Enable OCR
    #[serde(default)]
    pub enabled: Option<bool>,

    /// OCR language
    #[serde(default)]
    pub language: Option<String>,
}

/// Output configuration
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct OutputConfig {
    /// JPEG quality (1-100)
    #[serde(default)]
    pub jpeg_quality: Option<u8>,

    /// Skip existing files
    #[serde(default)]
    pub skip_existing: Option<bool>,
}

/// Main configuration structure
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct Config {
    /// General settings
    #[serde(default)]
    pub general: GeneralConfig,

    /// Processing settings
    #[serde(default)]
    pub processing: ProcessingConfig,

    /// Advanced settings
    #[serde(default)]
    pub advanced: AdvancedConfig,

    /// OCR settings
    #[serde(default)]
    pub ocr: OcrConfig,

    /// Output settings
    #[serde(default)]
    pub output: OutputConfig,
}

impl Config {
    /// Create a new default configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Load configuration from the default search path
    ///
    /// Search order:
    /// 1. `./superbook.toml`
    /// 2. `~/.config/superbook-pdf/config.toml`
    /// 3. Default values (if no file found)
    pub fn load() -> Result<Self, ConfigError> {
        // Try current directory first
        let current_dir_config = PathBuf::from("superbook.toml");
        if current_dir_config.exists() {
            return Self::load_from_path(&current_dir_config);
        }

        // Try user config directory
        if let Some(config_dir) = dirs::config_dir() {
            let user_config = config_dir.join("superbook-pdf").join("config.toml");
            if user_config.exists() {
                return Self::load_from_path(&user_config);
            }
        }

        // Return default config if no file found
        Ok(Self::default())
    }

    /// Load configuration from a specific file path
    pub fn load_from_path(path: &Path) -> Result<Self, ConfigError> {
        if !path.exists() {
            return Err(ConfigError::NotFound(path.to_path_buf()));
        }

        let content = std::fs::read_to_string(path)?;
        let config: Config = toml::from_str(&content)?;
        Ok(config)
    }

    /// Parse configuration from a TOML string
    pub fn from_toml(content: &str) -> Result<Self, ConfigError> {
        let config: Config = toml::from_str(content)?;
        Ok(config)
    }

    /// Serialize configuration to TOML string
    pub fn to_toml(&self) -> Result<String, toml::ser::Error> {
        toml::to_string_pretty(self)
    }

    /// Convert to PipelineConfig
    pub fn to_pipeline_config(&self) -> PipelineConfig {
        let mut config = PipelineConfig::default();

        // Apply general settings
        if let Some(dpi) = self.general.dpi {
            config = config.with_dpi(dpi);
        }
        if let Some(threads) = self.general.threads {
            config.threads = Some(threads);
        }

        // Apply processing settings
        if let Some(deskew) = self.processing.deskew {
            config = config.with_deskew(deskew);
        }
        if let Some(margin_trim) = self.processing.margin_trim {
            config = config.with_margin_trim(margin_trim);
        }
        if let Some(upscale) = self.processing.upscale {
            config = config.with_upscale(upscale);
        }
        if let Some(gpu) = self.processing.gpu {
            config = config.with_gpu(gpu);
        }

        // Apply advanced settings
        if let Some(internal) = self.advanced.internal_resolution {
            config.internal_resolution = internal;
        }
        if let Some(color) = self.advanced.color_correction {
            config.color_correction = color;
        }
        if let Some(offset) = self.advanced.offset_alignment {
            config.offset_alignment = offset;
        }
        if let Some(height) = self.advanced.output_height {
            config.output_height = height;
        }

        // Apply OCR settings
        if let Some(ocr) = self.ocr.enabled {
            config = config.with_ocr(ocr);
        }

        // Apply output settings
        if let Some(quality) = self.output.jpeg_quality {
            config.jpeg_quality = quality;
        }

        config
    }

    /// Merge with CLI arguments (CLI takes precedence)
    pub fn merge_with_cli(&self, cli: &CliOverrides) -> PipelineConfig {
        let mut config = self.to_pipeline_config();

        // CLI overrides take precedence
        if let Some(dpi) = cli.dpi {
            config = config.with_dpi(dpi);
        }
        if let Some(deskew) = cli.deskew {
            config = config.with_deskew(deskew);
        }
        if let Some(margin_trim) = cli.margin_trim {
            config = config.with_margin_trim(margin_trim);
        }
        if let Some(upscale) = cli.upscale {
            config = config.with_upscale(upscale);
        }
        if let Some(gpu) = cli.gpu {
            config = config.with_gpu(gpu);
        }
        if let Some(ocr) = cli.ocr {
            config = config.with_ocr(ocr);
        }
        if let Some(threads) = cli.threads {
            config.threads = Some(threads);
        }
        if let Some(internal) = cli.internal_resolution {
            config.internal_resolution = internal;
        }
        if let Some(color) = cli.color_correction {
            config.color_correction = color;
        }
        if let Some(offset) = cli.offset_alignment {
            config.offset_alignment = offset;
        }
        if let Some(height) = cli.output_height {
            config.output_height = height;
        }
        if let Some(quality) = cli.jpeg_quality {
            config.jpeg_quality = quality;
        }
        if let Some(max_pages) = cli.max_pages {
            config = config.with_max_pages(Some(max_pages));
        }
        if let Some(save_debug) = cli.save_debug {
            config.save_debug = save_debug;
        }

        config
    }

    /// Get config file search paths
    pub fn search_paths() -> Vec<PathBuf> {
        let mut paths = vec![PathBuf::from("superbook.toml")];

        if let Some(config_dir) = dirs::config_dir() {
            paths.push(config_dir.join("superbook-pdf").join("config.toml"));
        }

        paths
    }
}

/// CLI override values for merging with config file
#[derive(Debug, Clone, Default)]
pub struct CliOverrides {
    pub dpi: Option<u32>,
    pub deskew: Option<bool>,
    pub margin_trim: Option<f64>,
    pub upscale: Option<bool>,
    pub gpu: Option<bool>,
    pub ocr: Option<bool>,
    pub threads: Option<usize>,
    pub internal_resolution: Option<bool>,
    pub color_correction: Option<bool>,
    pub offset_alignment: Option<bool>,
    pub output_height: Option<u32>,
    pub jpeg_quality: Option<u8>,
    pub max_pages: Option<usize>,
    pub save_debug: Option<bool>,
}

impl CliOverrides {
    /// Create new empty overrides
    pub fn new() -> Self {
        Self::default()
    }

    /// Set DPI override
    pub fn with_dpi(mut self, dpi: u32) -> Self {
        self.dpi = Some(dpi);
        self
    }

    /// Set deskew override
    pub fn with_deskew(mut self, deskew: bool) -> Self {
        self.deskew = Some(deskew);
        self
    }

    /// Set margin trim override
    pub fn with_margin_trim(mut self, margin_trim: f64) -> Self {
        self.margin_trim = Some(margin_trim);
        self
    }

    /// Set upscale override
    pub fn with_upscale(mut self, upscale: bool) -> Self {
        self.upscale = Some(upscale);
        self
    }

    /// Set GPU override
    pub fn with_gpu(mut self, gpu: bool) -> Self {
        self.gpu = Some(gpu);
        self
    }

    /// Set OCR override
    pub fn with_ocr(mut self, ocr: bool) -> Self {
        self.ocr = Some(ocr);
        self
    }
}

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

    // CFG-001: Config::default
    #[test]
    fn test_config_default() {
        let config = Config::default();
        assert_eq!(config.general.dpi, None);
        assert_eq!(config.processing.deskew, None);
        assert_eq!(config.advanced.internal_resolution, None);
        assert_eq!(config.ocr.enabled, None);
        assert_eq!(config.output.jpeg_quality, None);
    }

    // CFG-002: Config::load_from_path (existing file)
    #[test]
    fn test_config_load_from_path_existing() {
        let dir = tempfile::tempdir().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[general]
dpi = 600

[processing]
deskew = true
"#,
        )
        .unwrap();

        let config = Config::load_from_path(&config_path).unwrap();
        assert_eq!(config.general.dpi, Some(600));
        assert_eq!(config.processing.deskew, Some(true));
    }

    // CFG-003: Config::load_from_path (non-existent file)
    #[test]
    fn test_config_load_from_path_not_found() {
        let result = Config::load_from_path(Path::new("/nonexistent/config.toml"));
        assert!(matches!(result, Err(ConfigError::NotFound(_))));
    }

    // CFG-004: Config::load (search order)
    #[test]
    fn test_config_search_paths() {
        let paths = Config::search_paths();
        assert!(!paths.is_empty());
        assert_eq!(paths[0], PathBuf::from("superbook.toml"));
    }

    // CFG-005: Config::merge (CLI priority)
    #[test]
    fn test_config_merge_cli_priority() {
        let config = Config {
            general: GeneralConfig {
                dpi: Some(300),
                ..Default::default()
            },
            processing: ProcessingConfig {
                deskew: Some(true),
                ..Default::default()
            },
            ..Default::default()
        };

        let cli = CliOverrides::new().with_dpi(600).with_deskew(false);

        let pipeline = config.merge_with_cli(&cli);
        assert_eq!(pipeline.dpi, 600); // CLI wins
        assert!(!pipeline.deskew); // CLI wins
    }

    // CFG-006: Config::to_pipeline_config
    #[test]
    fn test_config_to_pipeline_config() {
        let config = Config {
            general: GeneralConfig {
                dpi: Some(450),
                threads: Some(8),
                ..Default::default()
            },
            processing: ProcessingConfig {
                deskew: Some(false),
                margin_trim: Some(1.0),
                upscale: Some(true),
                gpu: Some(true),
            },
            advanced: AdvancedConfig {
                internal_resolution: Some(true),
                color_correction: Some(true),
                offset_alignment: Some(true),
                output_height: Some(4000),
            },
            ocr: OcrConfig {
                enabled: Some(true),
                ..Default::default()
            },
            output: OutputConfig {
                jpeg_quality: Some(95),
                ..Default::default()
            },
        };

        let pipeline = config.to_pipeline_config();
        assert_eq!(pipeline.dpi, 450);
        assert_eq!(pipeline.threads, Some(8));
        assert!(!pipeline.deskew);
        assert!((pipeline.margin_trim - 1.0).abs() < f64::EPSILON);
        assert!(pipeline.upscale);
        assert!(pipeline.gpu);
        assert!(pipeline.internal_resolution);
        assert!(pipeline.color_correction);
        assert!(pipeline.offset_alignment);
        assert_eq!(pipeline.output_height, 4000);
        assert!(pipeline.ocr);
        assert_eq!(pipeline.jpeg_quality, 95);
    }

    // CFG-007: TOML parse (complete config)
    #[test]
    fn test_config_toml_parse_complete() {
        let toml = r#"
[general]
dpi = 300
threads = 4
verbose = 2

[processing]
deskew = true
margin_trim = 0.5
upscale = true
gpu = true

[advanced]
internal_resolution = true
color_correction = true
offset_alignment = true
output_height = 3508

[ocr]
enabled = true
language = "ja"

[output]
jpeg_quality = 90
skip_existing = true
"#;

        let config = Config::from_toml(toml).unwrap();
        assert_eq!(config.general.dpi, Some(300));
        assert_eq!(config.general.threads, Some(4));
        assert_eq!(config.general.verbose, Some(2));
        assert_eq!(config.processing.deskew, Some(true));
        assert_eq!(config.processing.margin_trim, Some(0.5));
        assert_eq!(config.advanced.internal_resolution, Some(true));
        assert_eq!(config.ocr.language, Some("ja".to_string()));
        assert_eq!(config.output.jpeg_quality, Some(90));
        assert_eq!(config.output.skip_existing, Some(true));
    }

    // CFG-008: TOML parse (partial config)
    #[test]
    fn test_config_toml_parse_partial() {
        let toml = r#"
[general]
dpi = 600
"#;

        let config = Config::from_toml(toml).unwrap();
        assert_eq!(config.general.dpi, Some(600));
        assert_eq!(config.general.threads, None);
        assert_eq!(config.processing.deskew, None);
    }

    // CFG-009: TOML parse (empty file)
    #[test]
    fn test_config_toml_parse_empty() {
        let config = Config::from_toml("").unwrap();
        assert_eq!(config, Config::default());
    }

    // CFG-010: TOML parse (invalid format)
    #[test]
    fn test_config_toml_parse_invalid() {
        let result = Config::from_toml("this is not valid toml [[[");
        assert!(matches!(result, Err(ConfigError::TomlParse(_))));
    }

    #[test]
    fn test_config_to_toml() {
        let config = Config {
            general: GeneralConfig {
                dpi: Some(300),
                ..Default::default()
            },
            ..Default::default()
        };

        let toml_str = config.to_toml().unwrap();
        assert!(toml_str.contains("dpi = 300"));
    }

    #[test]
    fn test_cli_overrides_builder() {
        let overrides = CliOverrides::new()
            .with_dpi(600)
            .with_deskew(false)
            .with_margin_trim(1.5)
            .with_upscale(true)
            .with_gpu(false)
            .with_ocr(true);

        assert_eq!(overrides.dpi, Some(600));
        assert_eq!(overrides.deskew, Some(false));
        assert_eq!(overrides.margin_trim, Some(1.5));
        assert_eq!(overrides.upscale, Some(true));
        assert_eq!(overrides.gpu, Some(false));
        assert_eq!(overrides.ocr, Some(true));
    }

    #[test]
    fn test_config_error_display() {
        let err = ConfigError::NotFound(PathBuf::from("/test/path"));
        assert!(err.to_string().contains("Config file not found"));
    }

    #[test]
    fn test_config_new() {
        let config = Config::new();
        assert_eq!(config, Config::default());
    }

    #[test]
    fn test_config_merge_empty_cli() {
        let config = Config {
            general: GeneralConfig {
                dpi: Some(300),
                ..Default::default()
            },
            ..Default::default()
        };

        let cli = CliOverrides::new();
        let pipeline = config.merge_with_cli(&cli);
        assert_eq!(pipeline.dpi, 300); // Config value preserved
    }

    #[test]
    fn test_config_merge_partial_cli() {
        let config = Config {
            general: GeneralConfig {
                dpi: Some(300),
                threads: Some(4),
                ..Default::default()
            },
            processing: ProcessingConfig {
                deskew: Some(true),
                margin_trim: Some(0.5),
                ..Default::default()
            },
            ..Default::default()
        };

        let cli = CliOverrides::new().with_dpi(600);
        let pipeline = config.merge_with_cli(&cli);
        assert_eq!(pipeline.dpi, 600); // CLI wins
        assert_eq!(pipeline.threads, Some(4)); // Config preserved
        assert!(pipeline.deskew); // Config preserved
    }
}