Skip to content

Commit

Permalink
refactor(frontmatter-gen): 🎨 refactor code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Nov 17, 2024
1 parent 7bb677c commit 6a25f82
Show file tree
Hide file tree
Showing 23 changed files with 4,866 additions and 739 deletions.
35 changes: 25 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[package]
name = "frontmatter-gen"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
rust-version = "1.56.0"
license = "MIT OR Apache-2.0"
Expand All @@ -30,37 +30,52 @@ categories = [

keywords = ["frontmatter", "yaml", "toml", "json", "frontmatter-gen"]

# The library file that contains the main logic for the binary.
[lib]
name = "frontmatter_gen"
path = "src/lib.rs"

# The main file that contains the entry point for the binary.
[[bin]]
name = "frontmatter_gen"
path = "src/main.rs"

# -----------------------------------------------------------------------------
# Dependencies
# -----------------------------------------------------------------------------

[dependencies]

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
anyhow = "1.0.93"
clap = { version = "4.5.21", features = ["derive", "color", "help", "suggestions"] }
dtt = "0.0.8"
log = "0.4.22"
pretty_assertions = "1.4.1"
pulldown-cmark = "0.12.2"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.132"
serde_yml = "0.0.12"
thiserror = "2.0"
toml = "0.8"
tokio = { version = "1.0", features = ["full"] }
tera = "1.20.0"
thiserror = "2.0.3"
tokio = { version = "1.41.1", features = ["full"] }
toml = "0.8.19"
url = "2.5.3"
uuid = { version = "1.11.0", features = ["v4", "serde"] }

# -----------------------------------------------------------------------------
# Build Dependencies
# -----------------------------------------------------------------------------

[build-dependencies]
version_check = "0.9"
version_check = "0.9.5"

# -----------------------------------------------------------------------------
# Development Dependencies
# -----------------------------------------------------------------------------

[dev-dependencies]
criterion = "0.5"
serde = { version = "1.0", features = ["derive"] }
criterion = "0.5.1"
serde = { version = "1.0.215", features = ["derive"] }
tempfile = "3.14.0"

# -----------------------------------------------------------------------------
# Examples
Expand Down
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
frontmatter-gen = "0.0.2"
frontmatter-gen = "0.0.3"
```

## Usage
Expand All @@ -52,7 +52,7 @@ use frontmatter_gen::extract;

let content = r#"---
title: My Post
date: 2023-05-20
date: 2024-11-16
---
Content here"#;

Expand All @@ -68,25 +68,25 @@ use frontmatter_gen::{Frontmatter, Format, Value, to_format};

let mut frontmatter = Frontmatter::new();
frontmatter.insert("title".to_string(), Value::String("My Post".to_string()));
frontmatter.insert("date".to_string(), Value::String("2023-05-20".to_string()));
frontmatter.insert("date".to_string(), Value::String("2024-11-16".to_string()));

let yaml = to_format(&frontmatter, Format::Yaml).unwrap();
assert!(yaml.contains("title: My Post"));
assert!(yaml.contains("date: '2023-05-20'"));
assert!(yaml.contains("date: '2024-11-16'"));
```

### Parsing Different Formats

```rust
use frontmatter_gen::{parser, Format};

let yaml = "title: My Post\ndate: 2023-05-20\n";
let yaml = "title: My Post\ndate: 2024-11-16\n";
let frontmatter = parser::parse(yaml, Format::Yaml).unwrap();

let toml = "title = \"My Post\"\ndate = 2023-05-20\n";
let toml = "title = \"My Post\"\ndate = 2024-11-16\n";
let frontmatter = parser::parse(toml, Format::Toml).unwrap();

let json = r#"{"title": "My Post", "date": "2023-05-20"}"#;
let json = r#"{"title": "My Post", "date": "2024-11-16"}"#;
let frontmatter = parser::parse(json, Format::Json).unwrap();
```

Expand Down Expand Up @@ -125,6 +125,32 @@ Available examples:
- lib
- parser
- types
- first-post.md (Sample markdown file with frontmatter)

```markdown
---
title: My First Post
date: 2024-11-16
tags:
- rust
- programming
template: post
draft: false
---

# My First Post

This is the content of my first post.
```

To extract frontmatter from this example file:

```shell
# Try different formats
cargo run -- extract examples/first-post.md yaml
cargo run -- extract examples/first-post.md toml
cargo run -- extract examples/first-post.md json
```

## Contributing

Expand Down Expand Up @@ -158,5 +184,5 @@ Special thanks to all contributors who have helped build the `frontmatter-gen` l
[crates-badge]: https://img.shields.io/crates/v/frontmatter-gen.svg?style=for-the-badge&color=fc8d62&logo=rust
[docs-badge]: https://img.shields.io/badge/docs.rs-frontmatter--gen-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
[github-badge]: https://img.shields.io/badge/github-sebastienrousseau/frontmatter--gen-8da0cb?style=for-the-badge&labelColor=555555&logo=github
[libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.2-orange.svg?style=for-the-badge
[libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.3-orange.svg?style=for-the-badge
[made-with-rust]: https://img.shields.io/badge/rust-f04041?style=for-the-badge&labelColor=c0282d&logo=rust
2 changes: 1 addition & 1 deletion TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ A robust Rust library for parsing and serializing frontmatter in various formats
[crates-badge]: https://img.shields.io/crates/v/frontmatter-gen.svg?style=for-the-badge&color=fc8d62&logo=rust "Crates.io"
[docs-badge]: https://img.shields.io/badge/docs.rs-frontmatter--gen-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs "Docs.rs"
[github-badge]: https://img.shields.io/badge/github-sebastienrousseau/frontmatter--gen-8da0cb?style=for-the-badge&labelColor=555555&logo=github "GitHub"
[libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.2-orange.svg?style=for-the-badge "View on lib.rs"
[libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.3-orange.svg?style=for-the-badge "View on lib.rs"
[made-with-rust]: https://img.shields.io/badge/rust-f04041?style=for-the-badge&labelColor=c0282d&logo=rust 'Made With Rust'

## Changelog 📚
54 changes: 40 additions & 14 deletions benches/frontmatter_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{
black_box, criterion_group, criterion_main, Criterion,
};
use frontmatter_gen::{extract, parser, Format, Frontmatter, Value};

fn benchmark_extract(c: &mut Criterion) {
let content = r#"---
title: My Post
date: 2023-05-20
date: 2024-11-16
tags:
- rust
- benchmarking
Expand All @@ -19,7 +21,7 @@ This is the content of the post."#;
fn benchmark_parse_yaml(c: &mut Criterion) {
let yaml = r#"
title: My Post
date: 2023-05-20
date: 2024-11-16
tags:
- rust
- benchmarking
Expand All @@ -33,7 +35,7 @@ tags:
fn benchmark_parse_toml(c: &mut Criterion) {
let toml = r#"
title = "My Post"
date = 2023-05-20
date = 2024-11-16
tags = ["rust", "benchmarking"]
"#;

Expand All @@ -46,7 +48,7 @@ fn benchmark_parse_json(c: &mut Criterion) {
let json = r#"
{
"title": "My Post",
"date": "2023-05-20",
"date": "2024-11-16",
"tags": ["rust", "benchmarking"]
}
"#;
Expand All @@ -58,23 +60,47 @@ fn benchmark_parse_json(c: &mut Criterion) {

fn benchmark_to_format(c: &mut Criterion) {
let mut frontmatter = Frontmatter::new();
frontmatter.insert("title".to_string(), Value::String("My Post".to_string()));
frontmatter.insert("date".to_string(), Value::String("2023-05-20".to_string()));
frontmatter.insert("tags".to_string(), Value::Array(vec![
Value::String("rust".to_string()),
Value::String("benchmarking".to_string()),
]));
frontmatter.insert(
"title".to_string(),
Value::String("My Post".to_string()),
);
frontmatter.insert(
"date".to_string(),
Value::String("2024-11-16".to_string()),
);
frontmatter.insert(
"tags".to_string(),
Value::Array(vec![
Value::String("rust".to_string()),
Value::String("benchmarking".to_string()),
]),
);

c.bench_function("convert to YAML", |b| {
b.iter(|| frontmatter_gen::to_format(black_box(&frontmatter), Format::Yaml))
b.iter(|| {
frontmatter_gen::to_format(
black_box(&frontmatter),
Format::Yaml,
)
})
});

c.bench_function("convert to TOML", |b| {
b.iter(|| frontmatter_gen::to_format(black_box(&frontmatter), Format::Toml))
b.iter(|| {
frontmatter_gen::to_format(
black_box(&frontmatter),
Format::Toml,
)
})
});

c.bench_function("convert to JSON", |b| {
b.iter(|| frontmatter_gen::to_format(black_box(&frontmatter), Format::Json))
b.iter(|| {
frontmatter_gen::to_format(
black_box(&frontmatter),
Format::Json,
)
})
});
}

Expand Down
Loading

0 comments on commit 6a25f82

Please sign in to comment.