-
-
Notifications
You must be signed in to change notification settings - Fork 237
Add groups export. #1214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yarwin
wants to merge
2
commits into
godot-rust:master
Choose a base branch
from
Yarwin:add-group-export
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add groups export. #1214
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) godot-rust; Bromeon and contributors. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use crate::class::Field; | ||
use crate::util::bail; | ||
use crate::ParseResult; | ||
use proc_macro2::{Punct, TokenStream}; | ||
|
||
pub struct Fields { | ||
/// All fields except `base_field`. | ||
pub all_fields: Vec<Field>, | ||
|
||
/// The field with type `Base<T>`, if available. | ||
pub base_field: Option<Field>, | ||
|
||
/// Deprecation warnings. | ||
pub deprecations: Vec<TokenStream>, | ||
|
||
/// Errors during macro evaluation that shouldn't abort the execution of the macro. | ||
pub errors: Vec<venial::Error>, | ||
} | ||
|
||
/// Fetches data for all named fields for a struct. | ||
/// | ||
/// Errors if `class` is a tuple struct. | ||
pub fn named_fields( | ||
class: &venial::Struct, | ||
derive_macro_name: &str, | ||
) -> ParseResult<Vec<(venial::NamedField, Punct)>> { | ||
// This is separate from parse_fields to improve compile errors. The errors from here demand larger and more non-local changes from the API | ||
// user than those from parse_struct_attributes, so this must be run first. | ||
match &class.fields { | ||
// TODO disallow unit structs in the future | ||
// It often happens that over time, a registered class starts to require a base field. | ||
// Extending a {} struct requires breaking less code, so we should encourage it from the start. | ||
venial::Fields::Unit => Ok(vec![]), | ||
venial::Fields::Tuple(_) => bail!( | ||
&class.fields, | ||
"{derive_macro_name} is not supported for tuple structs", | ||
)?, | ||
venial::Fields::Named(fields) => Ok(fields.fields.inner.clone()), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) godot-rust; Bromeon and contributors. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
// Note: group membership for properties in Godot is based on the order of their registration. | ||
// All the properties belong to group or subgroup registered beforehand, identically as in GDScript. | ||
// Initial implementation providing clap-like API with an explicit sorting | ||
// & groups/subgroups declared for each field (`#[export(group = ..., subgroup = ...)]` | ||
// can be found at: https://github.com/godot-rust/gdext/pull/1214. | ||
|
||
use crate::util::{bail, KvParser}; | ||
use crate::ParseResult; | ||
use proc_macro2::Literal; | ||
|
||
/// Specifies group or subgroup which starts with a given field. | ||
/// Group membership for properties in Godot is based on the order of their registration – | ||
/// i.e. given field belongs to group declared beforehand (for example with some previous field). | ||
pub struct FieldGroup { | ||
pub(crate) name: Literal, | ||
pub(crate) prefix: Literal, | ||
} | ||
|
||
impl FieldGroup { | ||
pub(crate) fn new_from_kv(parser: &mut KvParser) -> ParseResult<Self> { | ||
let Some(name) = parser.handle_literal("name", "String")? else { | ||
return bail!(parser.span(), "missing required argument: `name = \"...\"."); | ||
}; | ||
|
||
let prefix = parser | ||
.handle_literal("prefix", "String")? | ||
.unwrap_or(Literal::string("")); | ||
|
||
Ok(Self { name, prefix }) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is unrelated to the PR, right? Is it a span improvement?
Note that
#[init(default)]
will be removed in v0.4; others like#[init(val)]
would benefit more from this. But if you plan bigger changes, maybe a separate PR is worth it 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's a span improvement – mainly it makes
handle_literal
(which is also used by other parsers) return correct position of said literal.default
using correct span as well is a side effect.