Skip to content

Commit 187b37d

Browse files
authored
Add scope, relative parameters (#29)
Also uses matrix strategy to build using stable and 1.85 toolchains.
1 parent a9f612d commit 187b37d

File tree

11 files changed

+358
-52
lines changed

11 files changed

+358
-52
lines changed

.cspell.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"doctest",
1616
"mkdn",
1717
"repr",
18+
"rustc",
1819
"rustlang",
1920
"rustup"
2021
],
@@ -31,6 +32,9 @@
3132
"filename": "Cargo.toml",
3233
"dictionaries": [
3334
"crates"
35+
],
36+
"words": [
37+
"cfgs"
3438
]
3539
},
3640
{

.github/workflows/ci.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ jobs:
3030
- macos-latest
3131
- ubuntu-latest
3232
- windows-latest
33+
toolchain:
34+
- stable
35+
include:
36+
- os: ubuntu-latest
37+
toolchain: '1.85'
3338
steps:
3439
- name: Checkout
3540
uses: actions/checkout@v4
@@ -42,9 +47,9 @@ jobs:
4247
~/.cargo/registry/cache/
4348
~/.cargo/git/db/
4449
target/
45-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
50+
key: ${{ runner.os }}-cargo-${{ matrix.toolchain }}-${{ hashFiles('**/Cargo.lock') }}
4651
- name: Set up toolchain
47-
run: rustup install
52+
run: rustup override set ${{ matrix.toolchain }}
4853
- name: Test
4954
run: cargo test --all-features --workspace
5055

@@ -62,7 +67,7 @@ jobs:
6267
~/.cargo/registry/cache/
6368
~/.cargo/git/db/
6469
target/
65-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
70+
key: ${{ runner.os }}-cargo-stable-${{ hashFiles('**/Cargo.lock') }}
6671
- name: Set up toolchain
6772
run: rustup show
6873
- name: Check formatting

.github/workflows/release.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ jobs:
3131
uses: actions/checkout@v4
3232
with:
3333
fetch-depth: 0
34+
- name: Cache
35+
uses: actions/cache@v4
36+
with:
37+
path: |
38+
~/.cargo/bin/
39+
~/.cargo/registry/index/
40+
~/.cargo/registry/cache/
41+
~/.cargo/git/db/
42+
target/
43+
key: ${{ runner.os }}-cargo-stable-${{ hashFiles('**/Cargo.lock') }}
3444
- name: Set up toolchain
3545
run: rustup install
3646
- name: Release

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
2+
"rust-analyzer.cargo.cfgs": [
3+
"debug_assertions",
4+
"miri",
5+
"rust_analyzer"
6+
],
27
"rust-analyzer.cargo.features": "all",
38
"rust-analyzer.check.command": "clippy",
49
"rust-analyzer.checkOnSave": true,
@@ -8,4 +13,4 @@
813
"[toml]": {
914
"editor.formatOnSave": true
1015
}
11-
}
16+
}

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "include-file"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
description = "Include sections of files into Rust source code"
55
readme = "README.md"
66
authors = ["Heath Stewart (https://github.com/heaths)"]
@@ -15,11 +15,17 @@ license = "MIT"
1515
proc-macro = true
1616

1717
[dependencies]
18-
proc-macro2 = "1.0.103"
18+
proc-macro2 = { version = "1.0.103", features = ["span-locations"] }
1919
syn = "2.0.109"
2020

2121
[dev-dependencies]
2222
quote = "1.0.42"
2323

2424
[lints.clippy]
2525
test_attr_in_doctest = "allow"
26+
27+
[lints.rust]
28+
unexpected_cfgs = { level = "allow", check-cfg = [
29+
"cfg(rust_analyzer)",
30+
"cfg(span_locations)",
31+
] }

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ You can demonstrate just the code you want in markdown while maintaining the ben
1010

1111
## Macros
1212

13-
* `include_asciidoc!(path, name)` includes Rust snippets from AsciiDoc files, commonly with `.asciidoc`, `.adoc`, or `.asc` extensions.
14-
* `include_markdown!(path, name)` includes Rust snippets from Markdown files, commonly with `.markdown`, `.mdown`, `.mkdn`, or `.md` extensions.
15-
* `include_org!(path, name)` includes Rust snippets from Org files, commonly with `.org` extension.
16-
* `include_textile!(path, name)` includes Rust snippets from Textile files, commonly with `.textile` extension.
13+
Macro | Description
14+
------------------------------- | ---
15+
`include_asciidoc!(path, name)` | Includes Rust snippets from AsciiDoc files, commonly with `.asciidoc`, `.adoc`, or `.asc` extensions.
16+
`include_markdown!(path, name)` | Includes Rust snippets from Markdown files, commonly with `.markdown`, `.mdown`, `.mkdn`, or `.md` extensions.
17+
`include_org!(path, name)` | Includes Rust snippets from Org files, commonly with `.org` extension.
18+
`include_textile!(path, name)` | Includes Rust snippets from Textile files, commonly with `.textile` extension.
19+
20+
All of these macros also support the following parameters:
21+
22+
Parameter | Description
23+
---------- | ---
24+
`relative` | (*Requires rustc 1.88 or newer*) The path is relative to the source file calling the macro. May show an error in rust-analyzer until [rust-lang/rust-analyzer#15950](https://github.com/rust-lang/rust-analyzer/issues/15950) is fixed.
25+
`scope` | Includes the Rust snippet in braces `{ .. }`.
1726

1827
## Examples
1928

build.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,88 @@
11
// Copyright 2025 Heath Stewart.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
use std::{cmp::Ordering, env, process::Command, str::FromStr};
5+
6+
const MIN_SPAN_LOCATIONS_VER: Version = Version::new(1, 88, 0);
7+
48
fn main() {
59
println!("cargo::rerun-if-changed=README.md");
10+
if matches!(rustc_version(), Ok(version) if version >= MIN_SPAN_LOCATIONS_VER) {
11+
println!("cargo::rustc-cfg=span_locations");
12+
}
13+
}
14+
15+
fn rustc_version() -> Result<Version, Box<dyn std::error::Error>> {
16+
let output = Command::new(env::var("RUSTC")?).arg("--version").output()?;
17+
let stdout = String::from_utf8(output.stdout)?;
18+
let mut words = stdout.split_whitespace();
19+
words.next().ok_or("expected `rustc`")?;
20+
21+
let version: Version = words.next().ok_or("expected version")?.parse()?;
22+
Ok(version)
23+
}
24+
25+
#[derive(Debug, Default, Eq)]
26+
struct Version {
27+
major: u16,
28+
minor: u16,
29+
patch: u16,
30+
}
31+
32+
impl Version {
33+
const fn new(major: u16, minor: u16, patch: u16) -> Self {
34+
Self {
35+
major,
36+
minor,
37+
patch,
38+
}
39+
}
40+
}
41+
42+
impl FromStr for Version {
43+
type Err = String;
44+
fn from_str(s: &str) -> Result<Self, Self::Err> {
45+
// cspell:ignore splitn
46+
let mut values = s.splitn(3, ".").map(str::parse::<u16>);
47+
Ok(Self {
48+
major: values
49+
.next()
50+
.ok_or("no major version")?
51+
.map_err(|err| err.to_string())?,
52+
minor: values
53+
.next()
54+
.ok_or("no minor version")?
55+
.map_err(|err| err.to_string())?,
56+
patch: values
57+
.next()
58+
.ok_or("no patch version")?
59+
.map_err(|err| err.to_string())?,
60+
})
61+
}
62+
}
63+
64+
impl PartialEq for Version {
65+
fn eq(&self, other: &Self) -> bool {
66+
self.major == other.major && self.minor == other.minor && self.patch == other.patch
67+
}
68+
}
69+
70+
impl Ord for Version {
71+
fn cmp(&self, other: &Self) -> Ordering {
72+
let cmp = self.major.cmp(&other.major);
73+
if cmp != Ordering::Equal {
74+
return cmp;
75+
}
76+
let cmp = self.minor.cmp(&other.minor);
77+
if cmp != Ordering::Equal {
78+
return cmp;
79+
}
80+
self.patch.cmp(&other.patch)
81+
}
82+
}
83+
84+
impl PartialOrd for Version {
85+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
86+
Some(self.cmp(other))
87+
}
688
}

0 commit comments

Comments
 (0)