mirror of
https://github.com/nushell/nushell.git
synced 2025-06-11 12:36:48 +02:00
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR replaces the usage of `proc-macro-error` with `proc-macro-error2`. At the time of writing `nu-derive-value` this wasn't an option, at least it wasn't clear that it is the direction to go. This shouldn't change any of the usage of `nu-derive-value` in any way but removes one security warning. `proc-macro-error` depends on `syn 1`, that's why I initially had the default features for `proc-macro-error` disabled. `proc-macro-error2` uses `syn 2` as mostly everything. So we can use that. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> Same interface, no changes. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` The tests for `nu-derive-value` do not test spans, so maybe something changed now but probably not. # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> We still have `quickcheck` which depends on `syn 1` but it seems we need that for `nu-cmd-lang`. Would be great if, in the future, we can get rid of `syn 1` as that should improve build times a bit.
105 lines
3.8 KiB
Rust
105 lines
3.8 KiB
Rust
use std::{any, fmt::Debug, marker::PhantomData};
|
|
|
|
use proc_macro2::Span;
|
|
use proc_macro_error2::{Diagnostic, Level};
|
|
|
|
#[derive(Debug)]
|
|
pub enum DeriveError<M> {
|
|
/// Marker variant, makes the `M` generic parameter valid.
|
|
_Marker(PhantomData<M>),
|
|
|
|
/// Parsing errors thrown by `syn`.
|
|
Syn(syn::parse::Error),
|
|
|
|
/// `syn::DeriveInput` was a union, currently not supported
|
|
UnsupportedUnions,
|
|
|
|
/// Only plain enums are supported right now.
|
|
UnsupportedEnums { fields_span: Span },
|
|
|
|
/// Found a `#[nu_value(x)]` attribute where `x` is unexpected.
|
|
UnexpectedAttribute { meta_span: Span },
|
|
|
|
/// Found a `#[nu_value(x)]` attribute at a invalid position.
|
|
InvalidAttributePosition { attribute_span: Span },
|
|
|
|
/// Found a valid `#[nu_value(x)]` attribute but the passed values is invalid.
|
|
InvalidAttributeValue {
|
|
value_span: Span,
|
|
value: Box<dyn Debug>,
|
|
},
|
|
|
|
/// Two keys or variants are called the same name breaking bidirectionality.
|
|
NonUniqueName {
|
|
name: String,
|
|
first: Span,
|
|
second: Span,
|
|
},
|
|
}
|
|
|
|
impl<M> From<syn::parse::Error> for DeriveError<M> {
|
|
fn from(value: syn::parse::Error) -> Self {
|
|
Self::Syn(value)
|
|
}
|
|
}
|
|
|
|
impl<M> From<DeriveError<M>> for Diagnostic {
|
|
fn from(value: DeriveError<M>) -> Self {
|
|
let derive_name = any::type_name::<M>().split("::").last().expect("not empty");
|
|
match value {
|
|
DeriveError::_Marker(_) => panic!("used marker variant"),
|
|
|
|
DeriveError::Syn(e) => Diagnostic::spanned(e.span(), Level::Error, e.to_string()),
|
|
|
|
DeriveError::UnsupportedUnions => Diagnostic::new(
|
|
Level::Error,
|
|
format!("`{derive_name}` cannot be derived from unions"),
|
|
)
|
|
.help("consider refactoring to a struct".to_string())
|
|
.note("if you really need a union, consider opening an issue on Github".to_string()),
|
|
|
|
DeriveError::UnsupportedEnums { fields_span } => Diagnostic::spanned(
|
|
fields_span,
|
|
Level::Error,
|
|
format!("`{derive_name}` can only be derived from plain enums"),
|
|
)
|
|
.help(
|
|
"consider refactoring your data type to a struct with a plain enum as a field"
|
|
.to_string(),
|
|
)
|
|
.note("more complex enums could be implemented in the future".to_string()),
|
|
|
|
DeriveError::InvalidAttributePosition { attribute_span } => Diagnostic::spanned(
|
|
attribute_span,
|
|
Level::Error,
|
|
"invalid attribute position".to_string(),
|
|
)
|
|
.help(format!(
|
|
"check documentation for `{derive_name}` for valid placements"
|
|
)),
|
|
|
|
DeriveError::UnexpectedAttribute { meta_span } => {
|
|
Diagnostic::spanned(meta_span, Level::Error, "unknown attribute".to_string()).help(
|
|
format!("check documentation for `{derive_name}` for valid attributes"),
|
|
)
|
|
}
|
|
|
|
DeriveError::InvalidAttributeValue { value_span, value } => {
|
|
Diagnostic::spanned(value_span, Level::Error, format!("invalid value {value:?}"))
|
|
.help(format!(
|
|
"check documentation for `{derive_name}` for valid attribute values"
|
|
))
|
|
}
|
|
|
|
DeriveError::NonUniqueName {
|
|
name,
|
|
first,
|
|
second,
|
|
} => Diagnostic::new(Level::Error, format!("non-unique name {name:?} found"))
|
|
.span_error(first, "first occurrence found here".to_string())
|
|
.span_error(second, "second occurrence found here".to_string())
|
|
.help("use `#[nu_value(rename = \"...\")]` to ensure unique names".to_string()),
|
|
}
|
|
}
|
|
}
|