Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --all
- run: cargo test --all --all-features
msrv:
name: "Check MSRV: 1.82.0"
runs-on: ubuntu-latest
Expand All @@ -29,7 +30,7 @@ jobs:
toolchain: 1.82.0 # MSRV
- uses: Swatinem/rust-cache@v2
- name: Default features
run: cargo test --all
run: cargo test --all --all-features
rustfmt:
name: rustfmt
runs-on: ubuntu-latest
Expand All @@ -43,7 +44,7 @@ jobs:
components: rustfmt
- uses: Swatinem/rust-cache@v2
- name: Check formatting
run: cargo fmt --all -- --check
run: cargo fmt --all --all-features -- --check
clippy:
name: cargo clippy (forbidden methods)
runs-on: ubuntu-latest
Expand All @@ -52,4 +53,4 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
# We just use clippy to spot forbidden methods.
# We disable the default lint set which has much questionable output.
- run: cargo clippy --all -- -A clippy::all -D clippy::disallowed_methods
- run: cargo clippy --all --all-features -- -A clippy::all -D clippy::disallowed_methods
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ readme = "./README.md"
rust-version = "1.82.0"

[dependencies]
proc-macro2 = { version = "1.0.0", optional = true }
toml_edit = { version = "0.25.0", default-features = false, features = [
"parse",
] }
quote = { version = "1.0.0", optional = true }

[dev-dependencies]
quote = "1.0.39"
syn = "2.0.99"
proc-macro2 = "1.0.94"

[features]
to_tokens = ["dep:proc-macro2", "dep:quote"]
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ use std::{
time::SystemTime,
};

#[cfg(feature = "to_tokens")]
use proc_macro2::{Ident, Span, TokenStream};
#[cfg(feature = "to_tokens")]
use quote::{quote, ToTokens};
use toml_edit::{DocumentMut, Item, TableLike, TomlError};

/// Error type used by this crate.
Expand Down Expand Up @@ -152,6 +156,22 @@ pub enum FoundCrate {
Name(String),
}

#[cfg(feature = "to_tokens")]
impl ToTokens for FoundCrate {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Itself => {
quote!(crate)
},
Self::Name(name) => {
let name = Ident::new_raw(name, Span::call_site());
quote!(::#name)
},
}
.to_tokens(tokens);
}
}

// In a rustc invocation, there will only ever be one entry in this map, since every crate is
// compiled with its own rustc process. However, the same is not (currently) the case for
// rust-analyzer.
Expand Down Expand Up @@ -574,4 +594,19 @@ mod tests {
"#,
Ok(Some(FoundCrate::Name(name))) if name == "my_crate_cool"
}

#[cfg(feature = "to_tokens")]
#[test]
fn itself_to_tokens() {
assert_eq!(quote!(crate).to_string(), FoundCrate::Itself.into_token_stream().to_string());
}

#[cfg(feature = "to_tokens")]
#[test]
fn name_to_tokens() {
assert_eq!(
quote!(::r#my_crate).to_string(),
FoundCrate::Name("my_crate".to_string()).into_token_stream().to_string()
);
}
}