html_generator/
performance.rs

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
// Copyright © 2025 HTML Generator. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Performance optimization functionality for HTML processing.
//!
//! This module provides optimized utilities for HTML minification and generation,
//! with both synchronous and asynchronous interfaces. The module focuses on:
//!
//! - Efficient HTML minification with configurable options
//! - Non-blocking asynchronous HTML generation
//! - Memory-efficient string handling
//! - Thread-safe operations
//!
//! # Performance Characteristics
//!
//! - Minification: O(n) time complexity, ~1.5x peak memory usage
//! - HTML Generation: O(n) time complexity, proportional memory usage
//! - All operations are thread-safe and support concurrent access
//!
//! # Examples
//!
//! Basic HTML minification:
//! ```no_run
//! # use html_generator::performance::minify_html;
//! # use std::path::Path;
//! # fn example() -> Result<(), html_generator::error::HtmlError> {
//! let path = Path::new("index.html");
//! let minified = minify_html(path)?;
//! println!("Minified size: {} bytes", minified.len());
//! # Ok(())
//! # }
//! ```

use crate::{HtmlError, Result};
use comrak::{markdown_to_html, ComrakOptions};
use minify_html::{minify, Cfg};
use std::{fs, path::Path};
use tokio::task;

/// Maximum allowed file size for minification (10 MB).
pub const MAX_FILE_SIZE: usize = 10 * 1024 * 1024;

/// Initial capacity for string buffers (1 KB).
const INITIAL_HTML_CAPACITY: usize = 1024;

/// Configuration for HTML minification with optimized defaults.
///
/// Provides a set of minification options that preserve HTML semantics
/// while reducing file size. The configuration balances compression
/// with standards compliance.
#[derive(Clone)]
struct MinifyConfig {
    /// Internal minification configuration from minify-html crate
    cfg: Cfg,
}

impl Default for MinifyConfig {
    fn default() -> Self {
        let mut cfg = Cfg::new();
        // Preserve HTML semantics and compatibility
        cfg.do_not_minify_doctype = true;
        cfg.ensure_spec_compliant_unquoted_attribute_values = true;
        cfg.keep_closing_tags = true;
        cfg.keep_html_and_head_opening_tags = true;
        cfg.keep_spaces_between_attributes = true;
        // Enable safe minification for non-structural elements
        cfg.keep_comments = false;
        cfg.minify_css = true;
        cfg.minify_js = true;
        cfg.remove_bangs = true;
        cfg.remove_processing_instructions = true;

        Self { cfg }
    }
}

impl std::fmt::Debug for MinifyConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MinifyConfig")
            .field(
                "do_not_minify_doctype",
                &self.cfg.do_not_minify_doctype,
            )
            .field("minify_css", &self.cfg.minify_css)
            .field("minify_js", &self.cfg.minify_js)
            .field("keep_comments", &self.cfg.keep_comments)
            .finish()
    }
}

/// Minifies HTML content from a file with optimized performance.
///
/// Reads an HTML file and applies efficient minification techniques to reduce
/// its size while maintaining functionality and standards compliance.
///
/// # Arguments
///
/// * `file_path` - Path to the HTML file to minify
///
/// # Returns
///
/// Returns the minified HTML content as a string if successful.
///
/// # Errors
///
/// Returns [`HtmlError`] if:
/// - File reading fails
/// - File size exceeds [`MAX_FILE_SIZE`]
/// - Content is not valid UTF-8
/// - Minification process fails
///
/// # Examples
///
/// ```no_run
/// # use html_generator::performance::minify_html;
/// # use std::path::Path;
/// # fn example() -> Result<(), html_generator::error::HtmlError> {
/// let path = Path::new("index.html");
/// let minified = minify_html(path)?;
/// println!("Minified HTML: {} bytes", minified.len());
/// # Ok(())
/// # }
/// ```
pub fn minify_html(file_path: &Path) -> Result<String> {
    let metadata = fs::metadata(file_path).map_err(|e| {
        HtmlError::MinificationError(format!(
            "Failed to read file metadata for '{}': {e}",
            file_path.display()
        ))
    })?;

    let file_size = metadata.len() as usize;
    if file_size > MAX_FILE_SIZE {
        return Err(HtmlError::MinificationError(format!(
            "File size {file_size} bytes exceeds maximum of {MAX_FILE_SIZE} bytes"
        )));
    }

    let content = fs::read_to_string(file_path).map_err(|e| {
        if e.to_string().contains("stream did not contain valid UTF-8")
        {
            HtmlError::MinificationError(format!(
                "Invalid UTF-8 in input file '{}': {e}",
                file_path.display()
            ))
        } else {
            HtmlError::MinificationError(format!(
                "Failed to read file '{}': {e}",
                file_path.display()
            ))
        }
    })?;

    let config = MinifyConfig::default();
    let minified = minify(content.as_bytes(), &config.cfg);

    String::from_utf8(minified).map_err(|e| {
        HtmlError::MinificationError(format!(
            "Invalid UTF-8 in minified content: {e}"
        ))
    })
}

/// Asynchronously generates HTML from Markdown content.
///
/// Processes Markdown in a separate thread to avoid blocking the async runtime,
/// optimized for efficient memory usage with larger content.
///
/// # Arguments
///
/// * `markdown` - Markdown content to convert to HTML
///
/// # Returns
///
/// Returns the generated HTML content if successful.
///
/// # Errors
///
/// Returns [`HtmlError`] if:
/// - Thread spawning fails
/// - Markdown processing fails
///
/// # Examples
///
/// ```
/// # use html_generator::performance::async_generate_html;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), html_generator::error::HtmlError> {
/// let markdown = "# Hello\n\nThis is a test.";
/// let html = async_generate_html(markdown).await?;
/// println!("Generated HTML length: {}", html.len());
/// # Ok(())
/// # }
/// ```
pub async fn async_generate_html(markdown: &str) -> Result<String> {
    // Optimize string allocation based on content size
    let markdown = if markdown.len() < INITIAL_HTML_CAPACITY {
        markdown.to_string()
    } else {
        // Pre-allocate for larger content
        let mut string = String::with_capacity(markdown.len());
        string.push_str(markdown);
        string
    };

    task::spawn_blocking(move || {
        let options = ComrakOptions::default();
        Ok(markdown_to_html(&markdown, &options))
    })
    .await
    .map_err(|e| HtmlError::MarkdownConversion {
        message: format!("Asynchronous HTML generation failed: {e}"),
        source: Some(std::io::Error::new(
            std::io::ErrorKind::Other,
            e.to_string(),
        )),
    })?
}

/// Synchronously generates HTML from Markdown content.
///
/// Provides a simple, synchronous interface for Markdown to HTML conversion
/// when asynchronous processing isn't required.
///
/// # Arguments
///
/// * `markdown` - Markdown content to convert to HTML
///
/// # Returns
///
/// Returns the generated HTML content if successful.
///
/// # Examples
///
/// ```
/// # use html_generator::performance::generate_html;
/// # fn example() -> Result<(), html_generator::error::HtmlError> {
/// let markdown = "# Hello\n\nThis is a test.";
/// let html = generate_html(markdown)?;
/// println!("Generated HTML length: {}", html.len());
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn generate_html(markdown: &str) -> Result<String> {
    Ok(markdown_to_html(markdown, &ComrakOptions::default()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;

    /// Helper function to create a temporary HTML file for testing.
    ///
    /// # Arguments
    ///
    /// * `content` - HTML content to write to the file.
    ///
    /// # Returns
    ///
    /// A tuple containing the temporary directory and file path.
    fn create_test_file(
        content: &str,
    ) -> (tempfile::TempDir, std::path::PathBuf) {
        let dir = tempdir().expect("Failed to create temp directory");
        let file_path = dir.path().join("test.html");
        let mut file = File::create(&file_path)
            .expect("Failed to create test file");
        file.write_all(content.as_bytes())
            .expect("Failed to write test content");
        (dir, file_path)
    }

    mod minify_html_tests {
        use super::*;

        #[test]
        fn test_minify_basic_html() {
            let html =
                "<html>  <body>    <p>Test</p>  </body>  </html>";
            let (dir, file_path) = create_test_file(html);
            let result = minify_html(&file_path);
            assert!(result.is_ok());
            assert_eq!(
                result.unwrap(),
                "<html><body><p>Test</p></body></html>"
            );
            drop(dir);
        }

        #[test]
        fn test_minify_with_comments() {
            let html =
                "<html><!-- Comment --><body><p>Test</p></body></html>";
            let (dir, file_path) = create_test_file(html);
            let result = minify_html(&file_path);
            assert!(result.is_ok());
            assert_eq!(
                result.unwrap(),
                "<html><body><p>Test</p></body></html>"
            );
            drop(dir);
        }

        #[test]
        fn test_minify_invalid_path() {
            let result = minify_html(Path::new("nonexistent.html"));
            assert!(result.is_err());
            assert!(matches!(
                result,
                Err(HtmlError::MinificationError(_))
            ));
        }

        #[test]
        fn test_minify_exceeds_max_size() {
            let large_content = "a".repeat(MAX_FILE_SIZE + 1);
            let (dir, file_path) = create_test_file(&large_content);
            let result = minify_html(&file_path);
            assert!(matches!(
                result,
                Err(HtmlError::MinificationError(_))
            ));
            let err_msg = result.unwrap_err().to_string();
            assert!(err_msg.contains("exceeds maximum"));
            drop(dir);
        }

        #[test]
        fn test_minify_invalid_utf8() {
            let dir =
                tempdir().expect("Failed to create temp directory");
            let file_path = dir.path().join("invalid.html");
            {
                let mut file = File::create(&file_path)
                    .expect("Failed to create test file");
                file.write_all(&[0xFF, 0xFF])
                    .expect("Failed to write test content");
            }

            let result = minify_html(&file_path);
            assert!(matches!(
                result,
                Err(HtmlError::MinificationError(_))
            ));
            let err_msg = result.unwrap_err().to_string();
            assert!(err_msg.contains("Invalid UTF-8 in input file"));
            drop(dir);
        }

        #[test]
        fn test_minify_utf8_content() {
            let html = "<html><body><p>Test 你好 🦀</p></body></html>";
            let (dir, file_path) = create_test_file(html);
            let result = minify_html(&file_path);
            assert!(result.is_ok());
            assert_eq!(
                result.unwrap(),
                "<html><body><p>Test 你好 🦀</p></body></html>"
            );
            drop(dir);
        }
    }

    mod async_generate_html_tests {
        use super::*;

        #[tokio::test]
        async fn test_async_generate_html() {
            let markdown = "# Test\n\nThis is a test.";
            let result = async_generate_html(markdown).await;
            assert!(result.is_ok());
            let html = result.unwrap();
            assert!(html.contains("<h1>Test</h1>"));
            assert!(html.contains("<p>This is a test.</p>"));
        }

        #[tokio::test]
        async fn test_async_generate_html_empty() {
            let result = async_generate_html("").await;
            assert!(result.is_ok());
            assert!(result.unwrap().is_empty());
        }

        #[tokio::test]
        async fn test_async_generate_html_large_content() {
            let large_markdown =
                "# Test\n\n".to_string() + &"Content\n".repeat(10_000);
            let result = async_generate_html(&large_markdown).await;
            assert!(result.is_ok());
            let html = result.unwrap();
            assert!(html.contains("<h1>Test</h1>"));
        }
    }

    mod generate_html_tests {
        use super::*;

        #[test]
        fn test_sync_generate_html() {
            let markdown = "# Test\n\nThis is a test.";
            let result = generate_html(markdown);
            assert!(result.is_ok());
            let html = result.unwrap();
            assert!(html.contains("<h1>Test</h1>"));
            assert!(html.contains("<p>This is a test.</p>"));
        }

        #[test]
        fn test_sync_generate_html_empty() {
            let result = generate_html("");
            assert!(result.is_ok());
            assert!(result.unwrap().is_empty());
        }

        #[test]
        fn test_sync_generate_html_large_content() {
            let large_markdown =
                "# Test\n\n".to_string() + &"Content\n".repeat(10_000);
            let result = generate_html(&large_markdown);
            assert!(result.is_ok());
            let html = result.unwrap();
            assert!(html.contains("<h1>Test</h1>"));
        }
    }

    mod additional_tests {
        use super::*;
        use std::fs::File;
        use std::io::Write;
        use tempfile::tempdir;

        /// Test for default MinifyConfig values.
        #[test]
        fn test_minify_config_default() {
            let config = MinifyConfig::default();
            assert!(config.cfg.do_not_minify_doctype);
            assert!(config.cfg.minify_css);
            assert!(config.cfg.minify_js);
            assert!(!config.cfg.keep_comments);
        }

        /// Test for custom MinifyConfig values.
        #[test]
        fn test_minify_config_custom() {
            let mut config = MinifyConfig::default();
            config.cfg.keep_comments = true;
            assert!(config.cfg.keep_comments);
        }

        /// Test for uncommon HTML structures in minify_html.
        #[test]
        fn test_minify_html_uncommon_structures() {
            let html = r#"<div><span>Test<div><p>Nested</p></div></span></div>"#;
            let (dir, file_path) = create_test_file(html);
            let result = minify_html(&file_path);
            assert!(result.is_ok());
            assert_eq!(
                result.unwrap(),
                r#"<div><span>Test<div><p>Nested</p></div></span></div>"#
            );
            drop(dir);
        }

        /// Test for mixed encodings in minify_html.
        #[test]
        fn test_minify_html_mixed_encodings() {
            let dir =
                tempdir().expect("Failed to create temp directory");
            let file_path = dir.path().join("mixed_encoding.html");
            {
                let mut file = File::create(&file_path)
                    .expect("Failed to create test file");
                file.write_all(&[0xFF, b'T', b'e', b's', b't', 0xFE])
                    .expect("Failed to write test content");
            }
            let result = minify_html(&file_path);
            assert!(matches!(
                result,
                Err(HtmlError::MinificationError(_))
            ));
            drop(dir);
        }

        /// Test for extremely large Markdown content in async_generate_html.
        #[tokio::test]
        async fn test_async_generate_html_extremely_large() {
            let large_markdown = "# Large Content
"
            .to_string()
                + &"Content
"
                .repeat(100_000);
            let result = async_generate_html(&large_markdown).await;
            assert!(result.is_ok());
            let html = result.unwrap();
            assert!(html.contains("<h1>Large Content</h1>"));
        }

        /// Test for very small Markdown content in generate_html.
        #[test]
        fn test_generate_html_very_small() {
            let markdown = "A";
            let result = generate_html(markdown);
            assert!(result.is_ok());
            assert_eq!(
                result.unwrap(),
                "<p>A</p>
"
            );
        }

        #[tokio::test]
        async fn test_async_generate_html_spawn_blocking_failure() {
            use tokio::task;

            // Simulate failure by forcing a panic inside the `spawn_blocking` task
            let _markdown = "# Valid Markdown"; // Normally valid Markdown

            // Override the `spawn_blocking` behavior to simulate a failure
            let result = task::spawn_blocking(|| {
                panic!("Simulated task failure"); // Force the closure to fail
            })
            .await;

            // Explicitly use `std::result::Result` to avoid alias conflicts
            let converted_result: std::result::Result<
                String,
                HtmlError,
            > = match result {
                Err(e) => Err(HtmlError::MarkdownConversion {
                    message: format!(
                        "Asynchronous HTML generation failed: {e}"
                    ),
                    source: Some(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        e.to_string(),
                    )),
                }),
                Ok(_) => panic!("Expected a simulated failure"),
            };

            // Check that the error matches `HtmlError::MarkdownConversion`
            assert!(matches!(
                converted_result,
                Err(HtmlError::MarkdownConversion { .. })
            ));

            if let Err(HtmlError::MarkdownConversion {
                message,
                source,
            }) = converted_result
            {
                assert!(message
                    .contains("Asynchronous HTML generation failed"));
                assert!(source.is_some());

                // Relax the assertion to match the general pattern of the panic message
                let source_message = source.unwrap().to_string();
                assert!(
                    source_message.contains("Simulated task failure"),
                    "Unexpected source message: {source_message}"
                );
            }
        }

        #[test]
        fn test_minify_html_empty_content() {
            let html = "";
            let (dir, file_path) = create_test_file(html);
            let result = minify_html(&file_path);
            assert!(result.is_ok());
            assert!(
                result.unwrap().is_empty(),
                "Minified content should be empty"
            );
            drop(dir);
        }

        #[test]
        fn test_minify_html_unusual_whitespace() {
            let html =
                "<html>\n\n\t<body>\t<p>Test</p>\n\n</body>\n\n</html>";
            let (dir, file_path) = create_test_file(html);
            let result = minify_html(&file_path);
            assert!(result.is_ok());
            assert_eq!(
                result.unwrap(),
                "<html><body><p>Test</p></body></html>",
                "Unexpected minified result for unusual whitespace"
            );
            drop(dir);
        }

        #[test]
        fn test_minify_html_with_special_characters() {
            let html = "<div>&lt;Special&gt; &amp; Characters</div>";
            let (dir, file_path) = create_test_file(html);
            let result = minify_html(&file_path);
            assert!(result.is_ok());
            assert_eq!(
        result.unwrap(),
        "<div>&LTSpecial> & Characters</div>",
        "Special characters were unexpectedly modified during minification"
    );
            drop(dir);
        }

        #[tokio::test]
        async fn test_async_generate_html_with_special_characters() {
            let markdown =
                "# Special & Characters\n\nContent with < > & \" '";
            let result = async_generate_html(markdown).await;
            assert!(result.is_ok());
            let html = result.unwrap();
            assert!(
                html.contains("&lt;"),
                "Less than sign not escaped"
            );
            assert!(
                html.contains("&gt;"),
                "Greater than sign not escaped"
            );
            assert!(html.contains("&amp;"), "Ampersand not escaped");
            assert!(
                html.contains("&quot;"),
                "Double quote not escaped"
            );
            assert!(
                html.contains("&#39;") || html.contains("'"),
                "Single quote not handled as expected"
            );
        }
    }
}