Skip to content

Commit

Permalink
Make enum variants a bit easier on the eyes
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Feb 7, 2024
1 parent dbe3cf5 commit f08fa19
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 72 deletions.
7 changes: 5 additions & 2 deletions crates/header-translator/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clang::token::TokenKind;
use clang::{Entity, EntityKind, EntityVisitResult, EvaluationResult};

use crate::rust_type::Ty;
use crate::stmt::new_enum_id;
use crate::stmt::{enum_constant_name, new_enum_id};
use crate::{Context, ItemIdentifier};

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -215,7 +215,10 @@ impl fmt::Display for Expr {
write!(f, "{name}")
}
}
Self::Enum { id, variant } => write!(f, "{}::{variant}.0", id.name),
Self::Enum { id, variant } => {
let pretty_name = enum_constant_name(&id.name, variant);
write!(f, "{}::{pretty_name}.0", id.name)
}
Self::Const(id) => write!(f, "{}", id.name),
Self::Var { id, ty } => {
if ty.is_enum_through_typedef() {
Expand Down
15 changes: 14 additions & 1 deletion crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,15 @@ pub(crate) fn new_enum_id(
id
}

/// Quick n' dirty logic for minimizing the enum constant name.
pub(crate) fn enum_constant_name<'a>(enum_name: &str, constant_name: &'a str) -> &'a str {
let res = constant_name.trim_start_matches(enum_name);
if res.starts_with(|c: char| c.is_numeric()) {
return constant_name;
}
res
}

impl Stmt {
pub fn parse(entity: &Entity<'_>, context: &Context<'_>) -> Vec<Self> {
let _span = debug_span!(
Expand Down Expand Up @@ -1880,7 +1889,11 @@ impl fmt::Display for Stmt {
writeln!(f, " pub enum {} {{", id.name)?;
for (name, availability, expr) in variants {
write!(f, "{availability}")?;
writeln!(f, " {name} = {expr},")?;
let pretty_name = enum_constant_name(&id.name, name);
if pretty_name != name {
writeln!(f, " #[doc(alias = \"{name}\")]")?;
}
writeln!(f, " {pretty_name} = {expr},")?;
}
writeln!(f, " }}")?;
writeln!(f, ");")?;
Expand Down
9 changes: 2 additions & 7 deletions crates/header-translator/translation-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,8 @@ skipped = true
skipped = true
[struct.NSRect]
skipped = true

# References CGRectEdge
[enum.NSRectEdge]
skipped = true
[enum.anonymous.constants.NSRectEdgeMinX]
Expand Down Expand Up @@ -1585,13 +1587,6 @@ skipped-protocols = ["MDLMeshBuffer", "MDLNamed"]
[class.MTKView]
skipped-protocols = ["CALayerDelegate"]

# produces wrong values for `LACredentialTypeSmartCardPIN` case; manually define for now
[enum.LACredentialType]
skipped = true
# produces wrong values for most cases; manually define for now
[enum.LAError]
skipped = true

# fails to strip const from `typedef const NSString *const MXLaunchTaskID;`
[typedef.MXLaunchTaskID]
skipped = true
Expand Down
9 changes: 6 additions & 3 deletions crates/icrate/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
View the release notes to learn more details:
- [15.1](https://developer.apple.com/documentation/xcode-release-notes/xcode-15_1-release-notes)
- [15.2](https://developer.apple.com/documentation/xcode-release-notes/xcode-15_2-release-notes)
* Marked `NSView::isFlipped`, `NSView::convertRect_toView`,
`NSWindow::convertRectToScreen` and `NSWindow::convertPointFromScreen` as
safe.
* **BREAKING**: Changed how categories are handled; now, when a library has
defined methods on a class defined in a different framework, a helper trait
is output with the methods, instead of the methods being implemented
directly on the type.
* **BREAKING**: Changed how enums are handled; now a newtype is generated for
each enum, with the enum variants as constants on that newtype, instead of
cluttering the top-level namespace.
* Marked `NSView::isFlipped`, `NSView::convertRect_toView`,
`NSWindow::convertRectToScreen` and `NSWindow::convertPointFromScreen` as
safe.
* Renamed the `block` and `objective-c` feature flags to `block2` and `objc2`.

The old feature flags are kept as deprecated.
Expand Down
21 changes: 14 additions & 7 deletions crates/icrate/src/additions/AppKit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,27 @@ ns_enum!(
#[underlying(NSInteger)]
#[allow(clippy::bool_to_int_with_if)]
pub enum NSImageResizingMode {
NSImageResizingModeStretch = if TARGET_ABI_USES_IOS_VALUES { 0 } else { 1 },
NSImageResizingModeTile = if TARGET_ABI_USES_IOS_VALUES { 1 } else { 0 },
#[doc(alias = "NSImageResizingModeStretch")]
Stretch = if TARGET_ABI_USES_IOS_VALUES { 0 } else { 1 },
#[doc(alias = "NSImageResizingModeTile")]
Tile = if TARGET_ABI_USES_IOS_VALUES { 1 } else { 0 },
}
);

ns_enum!(
#[underlying(NSInteger)]
#[allow(clippy::bool_to_int_with_if)]
pub enum NSTextAlignment {
NSTextAlignmentLeft = 0,
NSTextAlignmentRight = if TARGET_ABI_USES_IOS_VALUES { 2 } else { 1 },
NSTextAlignmentCenter = if TARGET_ABI_USES_IOS_VALUES { 1 } else { 2 },
NSTextAlignmentJustified = 3,
NSTextAlignmentNatural = 4,
#[doc(alias = "NSTextAlignmentLeft")]
Left = 0,
#[doc(alias = "NSTextAlignmentRight")]
Right = if TARGET_ABI_USES_IOS_VALUES { 2 } else { 1 },
#[doc(alias = "NSTextAlignmentCenter")]
Center = if TARGET_ABI_USES_IOS_VALUES { 1 } else { 2 },
#[doc(alias = "NSTextAlignmentJustified")]
Justified = 3,
#[doc(alias = "NSTextAlignmentNatural")]
Natural = 4,
}
);

Expand Down
20 changes: 12 additions & 8 deletions crates/icrate/src/additions/Foundation/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,18 @@ pub type NSRect = CGRect;
ns_enum!(
#[underlying(NSUInteger)]
pub enum NSRectEdge {
NSRectEdgeMinX = 0,
NSRectEdgeMinY = 1,
NSRectEdgeMaxX = 2,
NSRectEdgeMaxY = 3,
NSMinXEdge = Self::NSRectEdgeMinX.0,
NSMinYEdge = Self::NSRectEdgeMinY.0,
NSMaxXEdge = Self::NSRectEdgeMaxX.0,
NSMaxYEdge = Self::NSRectEdgeMaxY.0,
#[doc(alias = "NSRectEdgeMinX")]
MinX = 0,
#[doc(alias = "NSRectEdgeMinY")]
MinY = 1,
#[doc(alias = "NSRectEdgeMaxX")]
MaxX = 2,
#[doc(alias = "NSRectEdgeMaxY")]
MaxY = 3,
NSMinXEdge = NSRectEdge::MinX.0,
NSMinYEdge = NSRectEdge::MinY.0,
NSMaxXEdge = NSRectEdge::MaxX.0,
NSMaxYEdge = NSRectEdge::MaxY.0,
}
);

Expand Down
39 changes: 0 additions & 39 deletions crates/icrate/src/additions/LocalAuthentication/mod.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/icrate/src/additions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pub mod Foundation;
pub mod GameController;
#[cfg(feature = "InputMethodKit")]
pub mod InputMethodKit;
#[cfg(feature = "LocalAuthentication")]
pub mod LocalAuthentication;
#[cfg(feature = "MapKit")]
pub mod MapKit;
#[cfg(feature = "Metal")]
Expand Down
2 changes: 1 addition & 1 deletion crates/icrate/src/generated
4 changes: 2 additions & 2 deletions crates/icrate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ pub use self::additions::Foundation;
pub use self::additions::GameController;
#[cfg(feature = "InputMethodKit")]
pub use self::additions::InputMethodKit;
#[cfg(feature = "LocalAuthentication")]
pub use self::additions::LocalAuthentication;
#[cfg(feature = "MapKit")]
pub use self::additions::MapKit;
#[cfg(feature = "Metal")]
Expand Down Expand Up @@ -242,6 +240,8 @@ pub use self::generated::HealthKit;
pub use self::generated::IdentityLookup;
#[cfg(feature = "LinkPresentation")]
pub use self::generated::LinkPresentation;
#[cfg(feature = "LocalAuthentication")]
pub use self::generated::LocalAuthentication;
#[cfg(feature = "LocalAuthenticationEmbeddedUI")]
pub use self::generated::LocalAuthenticationEmbeddedUI;
#[cfg(feature = "MailKit")]
Expand Down

0 comments on commit f08fa19

Please sign in to comment.