Skip to content

Commit

Permalink
Feature: Add changelog support in the #[since()] macro
Browse files Browse the repository at this point in the history
This commit introduces the ability to include changelogs directly in the
`#[since()]` macro. For example:

```rust
#[since(version = "0.1.0", change = "add new argument `a: u64`", change = "add T")]
```

Generates the following documentation line:

```rust
/// Since: 0.1.0: add new argument `a: u64`; add T
```

Key updates:

- Multiple `change` attributes are supported within the same `#[since()]` macro.
- Changelogs are concatenated into a single documentation line.

This enhancement makes it easier to document changes alongside version annotations.
  • Loading branch information
drmingdrmer committed Jan 27, 2025
1 parent 5127df0 commit eef79b3
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions macros/src/since.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::utils;
pub struct Since {
pub(crate) version: Option<String>,
pub(crate) date: Option<String>,
/// The change message.
pub(crate) change: Vec<String>,
}

impl Since {
Expand All @@ -19,6 +21,7 @@ impl Since {
let mut since = Since {
version: None,
date: None,
change: vec![],
};

type AttributeArgs = syn::punctuated::Punctuated<syn::Meta, syn::Token![,]>;
Expand All @@ -44,6 +47,10 @@ impl Since {
since.set_date(namevalue.value.clone(), Spanned::span(&namevalue.value))?;
}

"change" => {
since.set_change(namevalue.value.clone(), Spanned::span(&namevalue.value))?;
}

name => {
let msg = format!(
"Unknown attribute {} is specified; expected one of: `version`, `date`",
Expand Down Expand Up @@ -132,6 +139,10 @@ impl Since {
if let Some(date) = &self.date {
s.push_str(&format!(", Date({})", date));
}

if !self.change.is_empty() {
s.push_str(&format!(": {}", self.change.join("; ")));
}
s
}

Expand Down Expand Up @@ -172,6 +183,14 @@ impl Since {
Ok(())
}

pub(crate) fn set_change(&mut self, change_lit: syn::Expr, span: Span) -> Result<(), syn::Error> {
let change_str = Self::parse_str(change_lit, "change", span)?;

self.change.push(change_str);

Ok(())
}

/// Extract string from `foo` or `"foo"`
fn parse_str(expr: syn::Expr, field: &str, span: Span) -> Result<String, syn::Error> {
let s = match expr {
Expand Down
4 changes: 4 additions & 0 deletions macros/tests/since/expand/multi_since.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Since: 1.1.0, Date(2021-02-01): add Foo; add Bar; add Baz
///
/// Since: 1.0.0, Date(2021-01-01): Added
const A: i32 = 0;
9 changes: 9 additions & 0 deletions macros/tests/since/expand/multi_since.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[openraft_macros::since(
version = "1.1.0",
date = "2021-02-01",
change = "add Foo",
change = "add Bar",
change = "add Baz"
)]
#[openraft_macros::since(version = "1.0.0", date = "2021-01-01", change = "Added")]
const A: i32 = 0;
2 changes: 2 additions & 0 deletions macros/tests/since/expand/with_change.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Since: 1.0.0: add Foo
const A: i32 = 0;
2 changes: 2 additions & 0 deletions macros/tests/since/expand/with_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[openraft_macros::since(version = "1.0.0", change = "add Foo")]
const A: i32 = 0;
2 changes: 2 additions & 0 deletions macros/tests/since/expand/with_date_change.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Since: 1.0.0, Date(2021-01-01): add Foo
const A: i32 = 0;
2 changes: 2 additions & 0 deletions macros/tests/since/expand/with_date_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[openraft_macros::since(version = "1.0.0", date = "2021-01-01", change = "add Foo")]
const A: i32 = 0;
2 changes: 2 additions & 0 deletions macros/tests/since/expand/with_date_many_change.expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Since: 1.0.0, Date(2021-01-01): add Foo; add Bar; add Baz
const A: i32 = 0;
8 changes: 8 additions & 0 deletions macros/tests/since/expand/with_date_many_change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[openraft_macros::since(
version = "1.0.0",
date = "2021-01-01",
change = "add Foo",
change = "add Bar",
change = "add Baz"
)]
const A: i32 = 0;

0 comments on commit eef79b3

Please sign in to comment.