frontmatter_gen/config.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 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
// Copyright © 2024 Shokunin Static Site Generator. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! # Configuration Module
//!
//! This module provides a robust and type-safe configuration system for the Static Site Generator.
//! It handles validation, serialization, and secure management of all configuration settings.
//!
//! ## Features
//!
//! - Type-safe configuration management
//! - Comprehensive validation of all settings
//! - Secure path handling and normalization
//! - Flexible Builder pattern for configuration creation
//! - Serialization support via serde
//! - Default values for optional settings
//!
//! ## Examples
//!
//! Basic usage (always available):
//! ```rust
//! use frontmatter_gen::config::Config;
//!
//! # fn main() -> anyhow::Result<()> {
//! let config = Config::builder()
//! .site_name("My Blog")
//! .site_title("My Awesome Blog")
//! .build()?;
//!
//! assert_eq!(config.site_name(), "My Blog");
//! # Ok(())
//! # }
//! ```
//!
//! With SSG features (requires "ssg" feature):
//! ```rust,ignore
//! use frontmatter_gen::config::Config;
//!
//! # fn main() -> anyhow::Result<()> {
//! let config = Config::builder()
//! .site_name("My Blog")
//! .site_title("My Awesome Blog")
//! .content_dir("content") // Requires "ssg" feature
//! .template_dir("templates") // Requires "ssg" feature
//! .output_dir("public") // Requires "ssg" feature
//! .build()?;
//!
//! assert_eq!(config.site_name(), "My Blog");
//! # Ok(())
//! # }
//! ```
//!
//! To use SSG-specific functionality, enable the "ssg" feature in your Cargo.toml:
//! ```toml
//! [dependencies]
//! frontmatter-gen = { version = "0.0.3", features = ["ssg"] }
//! ```
use std::fmt;
#[cfg(feature = "ssg")]
use std::path::{Path, PathBuf};
#[cfg(feature = "ssg")]
use anyhow::Context;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[cfg(feature = "ssg")]
use url::Url;
use uuid::Uuid;
#[cfg(feature = "ssg")]
use crate::utils::fs::validate_path_safety;
/// Errors specific to configuration operations
#[derive(Error, Debug)]
pub enum ConfigError {
/// Invalid site name provided
#[error("Invalid site name: {0}")]
InvalidSiteName(String),
/// Invalid directory path with detailed context
#[error("Invalid directory path '{path}': {details}")]
InvalidPath {
/// The path that was invalid
path: String,
/// Details about why the path was invalid
details: String,
},
/// Invalid URL format
#[cfg(feature = "ssg")]
#[error("Invalid URL format: {0}")]
InvalidUrl(String),
/// Invalid language code
#[cfg(feature = "ssg")]
#[error("Invalid language code '{0}': must be in format 'xx-XX'")]
InvalidLanguage(String),
/// Configuration file error
#[error("Configuration file error: {0}")]
FileError(#[from] std::io::Error),
/// TOML parsing error
#[error("TOML parsing error: {0}")]
TomlError(#[from] toml::de::Error),
/// Server configuration error
#[cfg(feature = "ssg")]
#[error("Server configuration error: {0}")]
ServerError(String),
}
/// Core configuration structure.
///
/// This structure defines the configuration options for the Static Site Generator.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// Unique identifier for the configuration.
#[serde(default = "Uuid::new_v4")]
id: Uuid,
/// Name of the site.
pub site_name: String,
/// Title of the site, displayed in the browser's title bar.
#[serde(default = "default_site_title")]
pub site_title: String,
/// Description of the site.
#[cfg(feature = "ssg")]
#[serde(default = "default_site_description")]
pub site_description: String,
/// Language of the site (e.g., "en" for English).
#[cfg(feature = "ssg")]
#[serde(default = "default_language")]
pub language: String,
/// Base URL of the site.
#[cfg(feature = "ssg")]
#[serde(default = "default_base_url")]
pub base_url: String,
/// Path to the directory containing content files.
#[cfg(feature = "ssg")]
#[serde(default = "default_content_dir")]
pub content_dir: PathBuf,
/// Path to the directory where the generated output will be stored.
#[cfg(feature = "ssg")]
#[serde(default = "default_output_dir")]
pub output_dir: PathBuf,
/// Path to the directory containing templates.
#[cfg(feature = "ssg")]
#[serde(default = "default_template_dir")]
pub template_dir: PathBuf,
/// Optional directory to serve during development.
#[cfg(feature = "ssg")]
#[serde(default)]
pub serve_dir: Option<PathBuf>,
/// Flag to enable or disable the development server.
#[cfg(feature = "ssg")]
#[serde(default)]
pub server_enabled: bool,
/// Port for the development server.
#[cfg(feature = "ssg")]
#[serde(default = "default_port")]
pub server_port: u16,
}
// Default value functions for serde
fn default_site_title() -> String {
"My Shokunin Site".to_string()
}
#[cfg(feature = "ssg")]
fn default_site_description() -> String {
"A site built with Shokunin".to_string()
}
#[cfg(feature = "ssg")]
fn default_language() -> String {
"en-GB".to_string()
}
#[cfg(feature = "ssg")]
fn default_base_url() -> String {
"http://localhost:8000".to_string()
}
#[cfg(feature = "ssg")]
fn default_content_dir() -> PathBuf {
PathBuf::from("content")
}
#[cfg(feature = "ssg")]
fn default_output_dir() -> PathBuf {
PathBuf::from("public")
}
#[cfg(feature = "ssg")]
fn default_template_dir() -> PathBuf {
PathBuf::from("templates")
}
#[cfg(feature = "ssg")]
fn default_port() -> u16 {
8000
}
impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Site: {} ({})", self.site_name, self.site_title)?;
#[cfg(feature = "ssg")]
write!(
f,
"\nContent: {}\nOutput: {}\nTemplates: {}",
self.content_dir.display(),
self.output_dir.display(),
self.template_dir.display()
)?;
Ok(())
}
}
impl Config {
/// Creates a new ConfigBuilder instance for fluent configuration creation
///
/// # Examples
///
/// Basic usage (always available):
/// ```rust
/// use frontmatter_gen::config::Config;
///
/// let config = Config::builder()
/// .site_name("My Site")
/// .build()
/// .unwrap();
/// ```
///
/// With SSG features (requires "ssg" feature):
/// ```rust,ignore
/// use frontmatter_gen::config::Config;
///
/// let config = Config::builder()
/// .site_name("My Site")
/// .content_dir("content") // Only available with "ssg" feature
/// .template_dir("templates") // Only available with "ssg" feature
/// .build()
/// .unwrap();
/// ```
pub fn builder() -> ConfigBuilder {
ConfigBuilder::default()
}
/// Loads configuration from a TOML file
///
/// # Arguments
///
/// * `path` - Path to the TOML configuration file
///
/// # Returns
///
/// Returns a Result containing the loaded Config or an error
///
/// # Errors
///
/// Will return an error if:
/// - File cannot be read
/// - TOML parsing fails
/// - Configuration validation fails
///
/// # Examples
///
/// ```no_run
/// use frontmatter_gen::config::Config;
/// use std::path::Path;
///
/// let config = Config::from_file(Path::new("config.toml")).unwrap();
/// ```
#[cfg(feature = "ssg")]
pub fn from_file(path: &Path) -> Result<Self> {
let content =
std::fs::read_to_string(path).with_context(|| {
format!(
"Failed to read config file: {}",
path.display()
)
})?;
let mut config: Config = toml::from_str(&content)
.context("Failed to parse TOML configuration")?;
// Ensure we have a unique ID
config.id = Uuid::new_v4();
// Validate the loaded configuration
config.validate()?;
Ok(config)
}
/// Validates the configuration settings
///
/// # Returns
///
/// Returns Ok(()) if validation passes, or an error if validation fails
///
/// # Errors
///
/// Will return an error if:
/// - Required fields are empty
/// - Paths are invalid or unsafe
/// - URLs are malformed
/// - Language code format is invalid
pub fn validate(&self) -> Result<()> {
if self.site_name.trim().is_empty() {
return Err(ConfigError::InvalidSiteName(
"Site name cannot be empty".to_string(),
)
.into());
}
#[cfg(feature = "ssg")]
{
// SSG-specific validation
self.validate_path(&self.content_dir, "content_dir")?;
self.validate_path(&self.output_dir, "output_dir")?;
self.validate_path(&self.template_dir, "template_dir")?;
if let Some(serve_dir) = &self.serve_dir {
self.validate_path(serve_dir, "serve_dir")?;
}
let _ = Url::parse(&self.base_url).map_err(|_| {
ConfigError::InvalidUrl(self.base_url.clone())
})?;
if !self.is_valid_language_code(&self.language) {
return Err(ConfigError::InvalidLanguage(
self.language.clone(),
)
.into());
}
if self.server_enabled
&& !self.is_valid_port(self.server_port)
{
return Err(ConfigError::ServerError(format!(
"Invalid port number: {}",
self.server_port
))
.into());
}
}
Ok(())
}
/// Validates a path for safety and accessibility
#[cfg(feature = "ssg")]
fn validate_path(&self, path: &Path, name: &str) -> Result<()> {
validate_path_safety(path).with_context(|| {
format!("Invalid {} path: {}", name, path.display())
})
}
/// Checks if a language code is valid (format: xx-XX)
#[cfg(feature = "ssg")]
fn is_valid_language_code(&self, code: &str) -> bool {
let parts: Vec<&str> = code.split('-').collect();
if parts.len() != 2 {
return false;
}
let (lang, region) = (parts[0], parts[1]);
lang.len() == 2
&& region.len() == 2
&& lang.chars().all(|c| c.is_ascii_lowercase())
&& region.chars().all(|c| c.is_ascii_uppercase())
}
/// Checks if a port number is valid
#[cfg(feature = "ssg")]
fn is_valid_port(&self, port: u16) -> bool {
port >= 1024
}
/// Gets the unique identifier for this configuration
pub fn id(&self) -> Uuid {
self.id
}
/// Gets the site name
pub fn site_name(&self) -> &str {
&self.site_name
}
/// Gets whether the development server is enabled
#[cfg(feature = "ssg")]
pub fn server_enabled(&self) -> bool {
self.server_enabled
}
/// Gets the server port if the server is enabled
#[cfg(feature = "ssg")]
pub fn server_port(&self) -> Option<u16> {
if self.server_enabled {
Some(self.server_port)
} else {
None
}
}
}
/// Builder for creating Config instances
#[derive(Default, Debug)]
pub struct ConfigBuilder {
site_name: Option<String>,
site_title: Option<String>,
#[cfg(feature = "ssg")]
site_description: Option<String>,
#[cfg(feature = "ssg")]
language: Option<String>,
#[cfg(feature = "ssg")]
base_url: Option<String>,
#[cfg(feature = "ssg")]
content_dir: Option<PathBuf>,
#[cfg(feature = "ssg")]
output_dir: Option<PathBuf>,
#[cfg(feature = "ssg")]
template_dir: Option<PathBuf>,
#[cfg(feature = "ssg")]
serve_dir: Option<PathBuf>,
#[cfg(feature = "ssg")]
server_enabled: bool,
#[cfg(feature = "ssg")]
server_port: Option<u16>,
}
impl ConfigBuilder {
// Core builder methods
/// Sets the site name
pub fn site_name<S: Into<String>>(mut self, name: S) -> Self {
self.site_name = Some(name.into());
self
}
/// Sets the site title
pub fn site_title<S: Into<String>>(mut self, title: S) -> Self {
self.site_title = Some(title.into());
self
}
// SSG-specific builder methods
#[cfg(feature = "ssg")]
/// Sets the site description
pub fn site_description<S: Into<String>>(
mut self,
desc: S,
) -> Self {
self.site_description = Some(desc.into());
self
}
/// Sets the language code
#[cfg(feature = "ssg")]
pub fn language<S: Into<String>>(mut self, lang: S) -> Self {
self.language = Some(lang.into());
self
}
/// Sets the base URL
#[cfg(feature = "ssg")]
pub fn base_url<S: Into<String>>(mut self, url: S) -> Self {
self.base_url = Some(url.into());
self
}
/// Sets the content directory
#[cfg(feature = "ssg")]
pub fn content_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.content_dir = Some(path.into());
self
}
/// Sets the output directory
#[cfg(feature = "ssg")]
pub fn output_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.output_dir = Some(path.into());
self
}
/// Sets the template directory
#[cfg(feature = "ssg")]
pub fn template_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.template_dir = Some(path.into());
self
}
/// Sets the serve directory
#[cfg(feature = "ssg")]
pub fn serve_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.serve_dir = Some(path.into());
self
}
/// Enables or disables the development server
#[cfg(feature = "ssg")]
pub fn server_enabled(mut self, enabled: bool) -> Self {
self.server_enabled = enabled;
self
}
/// Sets the server port
#[cfg(feature = "ssg")]
pub fn server_port(mut self, port: u16) -> Self {
self.server_port = Some(port);
self
}
/// Builds the Config instance
///
/// # Returns
///
/// Returns a Result containing the built Config or an error
///
/// # Errors
///
/// Will return an error if:
/// - Required fields are missing
/// - Validation fails
pub fn build(self) -> Result<Config> {
let config = Config {
id: Uuid::new_v4(),
site_name: self.site_name.unwrap_or_default(),
site_title: self
.site_title
.unwrap_or_else(default_site_title),
#[cfg(feature = "ssg")]
site_description: self
.site_description
.unwrap_or_else(default_site_description),
#[cfg(feature = "ssg")]
language: self.language.unwrap_or_else(default_language),
#[cfg(feature = "ssg")]
base_url: self.base_url.unwrap_or_else(default_base_url),
#[cfg(feature = "ssg")]
content_dir: self
.content_dir
.unwrap_or_else(default_content_dir),
#[cfg(feature = "ssg")]
output_dir: self
.output_dir
.unwrap_or_else(default_output_dir),
#[cfg(feature = "ssg")]
template_dir: self
.template_dir
.unwrap_or_else(default_template_dir),
#[cfg(feature = "ssg")]
serve_dir: self.serve_dir,
#[cfg(feature = "ssg")]
server_enabled: self.server_enabled,
#[cfg(feature = "ssg")]
server_port: self.server_port.unwrap_or_else(default_port),
};
config.validate()?;
Ok(config)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "ssg")]
use tempfile::tempdir;
/// Tests for default value functions
mod default_values_tests {
use super::*;
#[test]
fn test_default_site_title() {
assert_eq!(default_site_title(), "My Shokunin Site");
}
}
// SSG-specific tests
#[cfg(feature = "ssg")]
mod ssg_tests {
use crate::config::default_base_url;
use crate::config::default_content_dir;
use crate::config::default_language;
use crate::config::default_output_dir;
use crate::config::default_site_description;
use crate::config::default_template_dir;
use crate::config::PathBuf;
#[test]
fn test_default_site_description() {
assert_eq!(
default_site_description(),
"A site built with Shokunin"
);
}
#[test]
fn test_default_language() {
assert_eq!(default_language(), "en-GB");
}
#[test]
fn test_default_base_url() {
assert_eq!(default_base_url(), "http://localhost:8000");
}
#[test]
fn test_default_content_dir() {
assert_eq!(default_content_dir(), PathBuf::from("content"));
}
#[test]
fn test_default_output_dir() {
assert_eq!(default_output_dir(), PathBuf::from("public"));
}
#[test]
fn test_default_template_dir() {
assert_eq!(
default_template_dir(),
PathBuf::from("templates")
);
}
}
/// Tests for the `ConfigBuilder` functionality
mod builder_tests {
use super::*;
#[test]
fn test_builder_initialization() {
let builder = Config::builder();
assert_eq!(builder.site_name, None);
assert_eq!(builder.site_title, None);
#[cfg(feature = "ssg")]
assert_eq!(builder.site_description, None);
#[cfg(feature = "ssg")]
assert_eq!(builder.language, None);
#[cfg(feature = "ssg")]
assert_eq!(builder.base_url, None);
#[cfg(feature = "ssg")]
assert_eq!(builder.content_dir, None);
#[cfg(feature = "ssg")]
assert_eq!(builder.output_dir, None);
#[cfg(feature = "ssg")]
assert_eq!(builder.template_dir, None);
#[cfg(feature = "ssg")]
assert_eq!(builder.serve_dir, None);
#[cfg(feature = "ssg")]
assert!(!builder.server_enabled);
#[cfg(feature = "ssg")]
assert_eq!(builder.server_port, None);
}
#[test]
fn test_builder_defaults_applied() {
let config = Config::builder()
.site_name("Test Site")
.build()
.unwrap();
assert_eq!(config.site_title, default_site_title());
#[cfg(feature = "ssg")]
assert_eq!(
config.site_description,
default_site_description()
);
#[cfg(feature = "ssg")]
assert_eq!(config.language, default_language());
#[cfg(feature = "ssg")]
assert_eq!(config.base_url, default_base_url());
#[cfg(feature = "ssg")]
assert_eq!(config.content_dir, default_content_dir());
#[cfg(feature = "ssg")]
assert_eq!(config.output_dir, default_output_dir());
#[cfg(feature = "ssg")]
assert_eq!(config.template_dir, default_template_dir());
#[cfg(feature = "ssg")]
assert_eq!(config.server_port, default_port());
#[cfg(feature = "ssg")]
assert!(!config.server_enabled);
#[cfg(feature = "ssg")]
assert!(config.serve_dir.is_none());
}
#[test]
fn test_builder_missing_site_name() {
let result = Config::builder().build();
assert!(
result.is_err(),
"Builder should fail without site_name"
);
}
#[test]
fn test_builder_empty_values() {
let result =
Config::builder().site_name("").site_title("").build();
assert!(
result.is_err(),
"Empty values should fail validation"
);
}
#[test]
fn test_unique_id_generation() -> Result<()> {
let config1 =
Config::builder().site_name("Site 1").build()?;
let config2 =
Config::builder().site_name("Site 2").build()?;
assert_ne!(
config1.id(),
config2.id(),
"IDs should be unique"
);
Ok(())
}
#[test]
fn test_builder_long_values() {
let long_string = "a".repeat(256);
let result = Config::builder()
.site_name(&long_string)
.site_title(&long_string)
.build();
assert!(
result.is_ok(),
"Long values should not cause validation errors"
);
}
}
/// Tests for configuration validation
mod validation_tests {
use super::*;
#[test]
fn test_empty_site_name() {
let result = Config::builder().site_name("").build();
assert!(
result.is_err(),
"Empty site name should fail validation"
);
}
#[cfg(feature = "ssg")]
#[test]
fn test_empty_site_name_ssg() {
let result = Config::builder()
.site_name("")
.content_dir("content")
.build();
assert!(
result.is_err(),
"Empty site name should fail validation"
);
}
#[cfg(feature = "ssg")]
#[test]
fn test_invalid_url_format() {
let invalid_urls = vec![
"not-a-url",
"http://",
"://invalid",
"http//missing-colon",
];
for url in invalid_urls {
let result = Config::builder()
.site_name("Test Site")
.base_url(url)
.build();
assert!(
result.is_err(),
"URL '{}' should fail validation",
url
);
}
}
#[cfg(feature = "ssg")]
#[test]
fn test_validate_path_safety_mocked() {
let path = PathBuf::from("valid/path");
let result = Config::builder()
.site_name("Test Site")
.content_dir(path)
.build();
assert!(
result.is_ok(),
"Valid path should pass validation"
);
}
}
/// Tests for `ConfigError` variants
mod config_error_tests {
use super::*;
#[test]
fn test_config_error_display() {
let error =
ConfigError::InvalidSiteName("Empty name".to_string());
assert_eq!(
format!("{}", error),
"Invalid site name: Empty name"
);
}
#[test]
fn test_invalid_path_error() {
let error = ConfigError::InvalidPath {
path: "invalid/path".to_string(),
details: "Unsafe path detected".to_string(),
};
assert_eq!(
format!("{}", error),
"Invalid directory path 'invalid/path': Unsafe path detected"
);
}
#[test]
fn test_file_error_conversion() {
let io_error = std::io::Error::new(
std::io::ErrorKind::NotFound,
"File not found",
);
let error: ConfigError = io_error.into();
assert_eq!(
format!("{}", error),
"Configuration file error: File not found"
);
}
}
/// Tests for helper methods
mod helper_method_tests {
#[cfg(feature = "ssg")]
use super::*;
#[cfg(feature = "ssg")]
#[test]
fn test_is_valid_language_code() {
let config =
Config::builder().site_name("Test").build().unwrap();
assert!(config.is_valid_language_code("en-US"));
assert!(!config.is_valid_language_code("invalid-code"));
}
#[cfg(feature = "ssg")]
#[test]
fn test_is_valid_port() {
let config =
Config::builder().site_name("Test").build().unwrap();
assert!(config.is_valid_port(1024));
assert!(!config.is_valid_port(1023));
}
}
/// Tests for serialization and deserialization
mod serialization_tests {
use super::*;
#[test]
fn test_serialization_roundtrip() -> Result<()> {
let original = Config::builder()
.site_name("Test Site")
.site_title("Roundtrip Test")
.build()?;
let serialized = toml::to_string(&original)?;
let deserialized: Config = toml::from_str(&serialized)?;
assert_eq!(original.site_name, deserialized.site_name);
assert_eq!(original.site_title, deserialized.site_title);
assert_eq!(original.id(), deserialized.id());
Ok(())
}
}
/// Tests for file operations
mod file_tests {
#[cfg(feature = "ssg")]
use super::*;
#[cfg(feature = "ssg")]
#[test]
fn test_missing_config_file() {
let result =
Config::from_file(Path::new("nonexistent.toml"));
assert!(
result.is_err(),
"Missing file should fail to load"
);
}
#[cfg(feature = "ssg")]
#[test]
fn test_invalid_toml_file() -> Result<()> {
let dir = tempdir()?;
let config_path = dir.path().join("invalid_config.toml");
std::fs::write(&config_path, "invalid_toml_syntax")?;
let result = Config::from_file(&config_path);
assert!(result.is_err(), "Invalid TOML syntax should fail");
Ok(())
}
}
/// Miscellaneous utility tests
mod utility_tests {
use super::*;
#[cfg(feature = "ssg")]
#[test]
fn test_config_display_format() {
let config = Config::builder()
.site_name("Test Site")
.site_title("Display Title")
.content_dir("test_content")
.output_dir("test_output")
.template_dir("test_templates")
.build()
.unwrap();
let display = format!("{}", config);
assert!(display.contains("Site: Test Site (Display Title)"));
assert!(display.contains("Content: test_content"));
assert!(display.contains("Output: test_output"));
assert!(display.contains("Templates: test_templates"));
}
#[test]
fn test_clone_retains_all_fields() -> Result<()> {
let original = Config::builder()
.site_name("Original")
.site_title("Clone Test")
.build()?;
let cloned = original.clone();
assert_eq!(original.site_name, cloned.site_name);
assert_eq!(original.site_title, cloned.site_title);
assert_eq!(original.id(), cloned.id());
Ok(())
}
}
}