Skip to content

Commit

Permalink
refactor(ssg): 🎨 refactoring and optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Oct 2, 2024
1 parent 9e9e8ec commit 4884c7f
Show file tree
Hide file tree
Showing 5 changed files with 2,241 additions and 219 deletions.
158 changes: 158 additions & 0 deletions ssg-frontmatter/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use serde_json::Error as JsonError;
use serde_yml::Error as YamlError;
use thiserror::Error;

/// Represents errors that can occur during frontmatter parsing, conversion, and extraction.
///
/// This enum uses the `thiserror` crate to provide clear and structured error messages,
/// making it easier to debug and handle issues that arise when processing frontmatter.
#[derive(Error, Debug)]

Check warning on line 9 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L9

Added line #L9 was not covered by tests
pub enum FrontmatterError {
/// Error occurred while parsing YAML.
#[error("Failed to parse YAML: {source}")]

Check warning on line 12 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L12

Added line #L12 was not covered by tests
YamlParseError {
/// The source error from the YAML parser.
source: YamlError,

Check warning on line 15 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L15

Added line #L15 was not covered by tests
},

/// Error occurred while parsing TOML.
#[error("Failed to parse TOML: {0}")]

Check warning on line 19 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L19

Added line #L19 was not covered by tests
TomlParseError(#[from] toml::de::Error),

/// Error occurred while parsing JSON.
#[error("Failed to parse JSON: {0}")]

Check warning on line 23 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L23

Added line #L23 was not covered by tests
JsonParseError(#[from] JsonError),

/// The frontmatter format is invalid or unsupported.
#[error("Invalid frontmatter format")]
InvalidFormat,

/// Error occurred during conversion between formats.
#[error("Failed to convert frontmatter: {0}")]
ConversionError(String),

Check warning on line 32 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L31-L32

Added lines #L31 - L32 were not covered by tests

/// Error occurred during extraction of frontmatter.
#[error("Failed to extract frontmatter: {0}")]
ExtractionError(String),

Check warning on line 36 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L35-L36

Added lines #L35 - L36 were not covered by tests

/// Generic parse error.
#[error("Failed to parse frontmatter: {0}")]
ParseError(String),

Check warning on line 40 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L39-L40

Added lines #L39 - L40 were not covered by tests

/// Error for unsupported or unknown frontmatter format.
#[error("Unsupported frontmatter format detected at line {line}")]

Check warning on line 43 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L43

Added line #L43 was not covered by tests
UnsupportedFormat {
/// The line number where the unsupported format was detected.
line: usize,

Check warning on line 46 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L46

Added line #L46 was not covered by tests
},
}

impl Clone for FrontmatterError {
fn clone(&self) -> Self {
match self {

Check warning on line 52 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L51-L52

Added lines #L51 - L52 were not covered by tests
FrontmatterError::YamlParseError { .. } => {
FrontmatterError::InvalidFormat

Check warning on line 54 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L54

Added line #L54 was not covered by tests
} // Custom fallback logic
FrontmatterError::TomlParseError(e) => {
FrontmatterError::TomlParseError(e.clone())
}

Check warning on line 58 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L56-L58

Added lines #L56 - L58 were not covered by tests
FrontmatterError::JsonParseError(_) => {
FrontmatterError::InvalidFormat

Check warning on line 60 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L60

Added line #L60 was not covered by tests
} // Custom fallback logic
FrontmatterError::InvalidFormat => {
FrontmatterError::InvalidFormat

Check warning on line 63 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L63

Added line #L63 was not covered by tests
}
FrontmatterError::ConversionError(msg) => {
FrontmatterError::ConversionError(msg.clone())
}
FrontmatterError::ExtractionError(msg) => {
FrontmatterError::ExtractionError(msg.clone())
}
FrontmatterError::ParseError(msg) => {
FrontmatterError::ParseError(msg.clone())
}

Check warning on line 73 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L65-L73

Added lines #L65 - L73 were not covered by tests
FrontmatterError::UnsupportedFormat { .. } => {
FrontmatterError::UnsupportedFormat { line: 0 }

Check warning on line 75 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L75

Added line #L75 was not covered by tests
}
}
}

Check warning on line 78 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L78

Added line #L78 was not covered by tests
}

impl FrontmatterError {
/// Helper function to create a generic parse error with a custom message.
///
/// # Arguments
///
/// * `message` - A string slice containing the error message.
///
/// # Example
///
/// ```rust
/// use ssg_frontmatter::error::FrontmatterError;
/// let error = FrontmatterError::generic_parse_error("Failed to parse at line 10");
/// ```
pub fn generic_parse_error(message: &str) -> FrontmatterError {
FrontmatterError::ParseError(message.to_string())
}

/// Helper function to create an `UnsupportedFormat` error with a given line number.
///
/// # Arguments
///
/// * `line` - The line number where the unsupported format was detected.
///
/// # Example
///
/// ```rust
/// use ssg_frontmatter::error::FrontmatterError;
/// let error = FrontmatterError::unsupported_format(12);
/// ```
pub fn unsupported_format(line: usize) -> FrontmatterError {
FrontmatterError::UnsupportedFormat { line }
}
}

/// Example usage of the `FrontmatterError` enum.
///
/// This function demonstrates how you might handle various errors during frontmatter parsing.
///
/// # Returns
///
/// Returns a `Result` demonstrating a parsing error.
pub fn example_usage() -> Result<(), FrontmatterError> {
let example_toml = "invalid toml content";

Check warning on line 123 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L122-L123

Added lines #L122 - L123 were not covered by tests

// Attempt to parse TOML and handle errors
match toml::from_str::<toml::Value>(example_toml) {
Ok(_) => Ok(()),
Err(e) => Err(FrontmatterError::TomlParseError(e)),

Check warning on line 128 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L126-L128

Added lines #L126 - L128 were not covered by tests
}
}

Check warning on line 130 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L130

Added line #L130 was not covered by tests

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

#[test]
fn test_generic_parse_error() {
let error =
FrontmatterError::generic_parse_error("Parsing failed");
match error {
FrontmatterError::ParseError(msg) => {
assert_eq!(msg, "Parsing failed")
}
_ => panic!("Expected ParseError"),

Check warning on line 144 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L144

Added line #L144 was not covered by tests
}
}

#[test]
fn test_unsupported_format_error() {
let error = FrontmatterError::unsupported_format(10);
match error {
FrontmatterError::UnsupportedFormat { line } => {
assert_eq!(line, 10)
}
_ => panic!("Expected UnsupportedFormat"),

Check warning on line 155 in ssg-frontmatter/src/error.rs

View check run for this annotation

Codecov / codecov/patch

ssg-frontmatter/src/error.rs#L155

Added line #L155 was not covered by tests
}
}
}
Loading

0 comments on commit 4884c7f

Please sign in to comment.