Skip to content

Properly format macros in baked data #3490

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

Closed
wants to merge 3 commits into from

Conversation

Manishearth
Copy link
Member

Fixes #3482

This is a bit clunky but overall not too bad.

@Manishearth Manishearth requested review from sffc, robertbastian and a team as code owners June 6, 2023 05:30
@sffc sffc removed their request for review June 6, 2023 06:16
@robertbastian
Copy link
Member

Could you post an example for one key?

let any_body = self.tokens_to_string(any_body, true, 4)?;

// This function uses string interpolation instead of quote!() since
// it rustfmt cannot format inside macro bodies (on stable, there is a nightly
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it actually work on nightly? Maybe we should just use that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems to work fine.

@sffc thoughts on having databake call rustfmt +nightly? This will break on systems not using rustup

(We can potentially allow for an env var override)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, huh, that option works on stable too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess "unstable" in rustfmt's context does not mean "unavailable on stable"

Copy link
Member Author

@Manishearth Manishearth Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though the docs claim:

Each configuration option is either stable or unstable. Stable options can always be used, while unstable options are only available on a nightly toolchain and must be opted into. To enable unstable options, set unstable_features = true in rustfmt.toml or pass --unstable-features to rustfmt.

The line about it only being available on nightly is gone on the v2.0-rc docs, but it still says you need to opt in to unstable options. Unsure what's going on here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clients probably won't use pretty on a regular basis because it's slow, we mainly want it for development.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work with older compilers too. Hmm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3491

Review and merge whichever one you prefer.

@Manishearth
Copy link
Member Author

Here's `calendar/japanese@1`
#[doc(hidden)]
#[macro_export]
macro_rules! __singleton_calendar_japanese_v1 {
    () => {
        icu_calendar::provider::JapaneseErasV1 { dates_to_eras: unsafe { zerovec::ZeroVec::from_bytes_unchecked(b"L\x07\0\0\t\x08meiji\0\0\0\0\0\0\0\0\0\0\0x\x07\0\0\x07\x1Etaisho\0\0\0\0\0\0\0\0\0\0\x86\x07\0\0\x0C\x19showa\0\0\0\0\0\0\0\0\0\0\0\xC5\x07\0\0\x01\x08heisei\0\0\0\0\0\0\0\0\0\0\xE3\x07\0\0\x05\x01reiwa\0\0\0\0\0\0\0\0\0\0\0") } }
    
    }
}
#[doc(hidden)]
pub use __singleton_calendar_japanese_v1 as singleton_calendar_japanese_v1;


/// Implement [`DataProvider<JapaneseErasV1Marker>`](icu_provider::DataProvider) on the given struct using the data
/// hardcoded in this file. This allows the struct to be used with
/// `icu`'s `_unstable` constructors.
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_calendar_japanese_v1 {
    ($provider:path) => {
        #[clippy::msrv = "1.61"]
        impl icu_provider::DataProvider<icu_calendar :: provider :: JapaneseErasV1Marker> for $provider {
            fn load(
                &self,
                req: icu_provider::DataRequest,
            ) -> Result<icu_provider::DataResponse<icu_calendar :: provider :: JapaneseErasV1Marker>, icu_provider::DataError> {
                req.locale.is_empty().then(|| {
                    static ANCHOR: <icu_calendar::provider::JapaneseErasV1Marker as icu_provider::DataMarker>::Yokeable = singleton_calendar_japanese_v1!();
                    &ANCHOR
                })
            
                    .map(icu_provider::prelude::zerofrom::ZeroFrom::zero_from)
                    .map(icu_provider::DataPayload::from_owned)
                    .map(|payload| {
                        icu_provider::DataResponse {
                            metadata: Default::default(),
                            payload: Some(payload),
                        }
                    })
                    .ok_or_else(|| icu_provider::DataErrorKind::MissingLocale.with_req(<icu_calendar :: provider :: JapaneseErasV1Marker as icu_provider::KeyedDataMarker>::KEY, req))
                }
        }
    }
}
#[doc(inline)]
pub use __impl_calendar_japanese_v1 as impl_calendar_japanese_v1;
Here's `plurals/cardinal@1`
/// Implement [`DataProvider<CardinalV1Marker>`](icu_provider::DataProvider) on the given struct using the data
/// hardcoded in this file. This allows the struct to be used with
/// `icu`'s `_unstable` constructors.
#[doc(hidden)]
#[macro_export]
macro_rules! __impl_plurals_cardinal_v1 {
    ($provider:path) => {
        #[clippy::msrv = "1.61"]
        impl icu_provider::DataProvider<icu_plurals :: provider :: CardinalV1Marker> for $provider {
            fn load(
                &self,
                req: icu_provider::DataRequest,
            ) -> Result<icu_provider::DataResponse<icu_plurals :: provider :: CardinalV1Marker>, icu_provider::DataError> {
                ["ar", "bn", "en", "es", "fil", "fr", "ja", "ru", "sr", "th", "tr", "und"].binary_search_by(|k| req.locale.strict_cmp(k.as_bytes()).reverse()).ok().map(|i| unsafe {
                    static JA: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: None, two: None, few: None, many: None };
                    static TR: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC0\0\0\0\0\x01\0\0\0\x01\0\0\0") })), two: None, few: None, many: None };
                    static ES: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC0\0\0\0\0\x01\0\0\0\x01\0\0\0") })), two: None, few: None, many: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x05\0\0\0\0\0\r\0\x1A\0'\x004\0\xC7\0\0\0\0\0\0\0\0\0\0\0\0\x81\0\0\0\0\0\0\0\0\0\0\0\0\xC1@B\x0F\0\0\0\0\0\0\0\0\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\x07\0\0\0\0\0\0\0\0\x05\0\0\0") })) };
                    static FR: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC1\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0") })), two: None, few: None, many: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x05\0\0\0\0\0\r\0\x1A\0'\x004\0\xC7\0\0\0\0\0\0\0\0\0\0\0\0\x81\0\0\0\0\0\0\0\0\0\0\0\0\xC1@B\x0F\0\0\0\0\0\0\0\0\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\x07\0\0\0\0\0\0\0\0\x05\0\0\0") })) };
                    static BN: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x02\0\0\0\0\0\r\0\xC1\0\0\0\0\0\0\0\0\0\0\0\0@\0\0\0\0\x01\0\0\0\x01\0\0\0") })), two: None, few: None, many: None };
                    static EN: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x02\0\0\0\0\0\r\0\xC1\0\0\0\0\x01\0\0\0\x01\0\0\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0") })), two: None, few: None, many: None };
                    static RU: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x03\0\0\0\0\0\r\0\x1A\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\xC1\n\0\0\0\x01\0\0\0\x01\0\0\0\x81d\0\0\0\x0B\0\0\0\x0B\0\0\0") })), two: None, few: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x03\0\0\0\0\0\r\0\x1A\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\xC1\n\0\0\0\x02\0\0\0\x04\0\0\0\x81d\0\0\0\x0C\0\0\0\x0E\0\0\0") })), many: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x06\0\0\0\0\0\r\0\x1A\0'\x004\0A\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\xC1\n\0\0\0\0\0\0\0\0\0\0\0B\0\0\0\0\0\0\0\0\0\0\0\0\xC1\n\0\0\0\x05\0\0\0\t\0\0\0B\0\0\0\0\0\0\0\0\0\0\0\0\xC1d\0\0\0\x0B\0\0\0\x0E\0\0\0") })) };
                    static SR: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x05\0\0\0\0\0\r\0\x1A\0'\x004\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\xC1\n\0\0\0\x01\0\0\0\x01\0\0\0\x81d\0\0\0\x0B\0\0\0\x0B\0\0\0D\n\0\0\0\x01\0\0\0\x01\0\0\0\x84d\0\0\0\x0B\0\0\0\x0B\0\0\0") })), two: None, few: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x05\0\0\0\0\0\r\0\x1A\0'\x004\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\xC1\n\0\0\0\x02\0\0\0\x04\0\0\0\x81d\0\0\0\x0C\0\0\0\x0E\0\0\0D\n\0\0\0\x02\0\0\0\x04\0\0\0\x84d\0\0\0\x0C\0\0\0\x0E\0\0\0") })), many: None };
                    static FIL: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: None, one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x06\0\0\0\0\0\r\0*\x007\0T\0a\0\xC2\0\0\0\0\0\0\0\0\0\0\0\0\xC1\0\0\0\0\x01\0\0\0\x01\0\0\0\x02\0\0\0\x02\0\0\0\x03\0\0\0\x03\0\0\0B\0\0\0\0\0\0\0\0\0\0\0\0\x81\n\0\0\0\x04\0\0\0\x04\0\0\0\x06\0\0\0\x06\0\0\0\t\0\0\0\t\0\0\0\x02\0\0\0\0\0\0\0\0\0\0\0\0\x84\n\0\0\0\x04\0\0\0\x04\0\0\0\x06\0\0\0\x06\0\0\0\t\0\0\0\t\0\0\0") })), two: None, few: None, many: None };
                    static AR: <icu_plurals::provider::CardinalV1Marker as icu_provider::DataMarker>::Yokeable = icu_plurals::provider::PluralRulesV1 { zero: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC0\0\0\0\0\0\0\0\0\0\0\0\0") })), one: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC0\0\0\0\0\x01\0\0\0\x01\0\0\0") })), two: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC0\0\0\0\0\x02\0\0\0\x02\0\0\0") })), few: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC0d\0\0\0\x03\0\0\0\n\0\0\0") })), many: Some(icu_plurals::rules::runtime::ast::Rule(unsafe { zerovec::VarZeroVec::from_bytes_unchecked(b"\x01\0\0\0\0\0\xC0d\0\0\0\x0B\0\0\0c\0\0\0") })) };
                    *[&AR, &BN, &EN, &ES, &FIL, &FR, &JA, &RU, &SR, &JA, &TR, &JA].get_unchecked(i)
                })
            
                    .map(icu_provider::prelude::zerofrom::ZeroFrom::zero_from)
                    .map(icu_provider::DataPayload::from_owned)
                    .map(|payload| {
                        icu_provider::DataResponse {
                            metadata: Default::default(),
                            payload: Some(payload),
                        }
                    })
                    .ok_or_else(|| icu_provider::DataErrorKind::MissingLocale.with_req(<icu_plurals :: provider :: CardinalV1Marker as icu_provider::KeyedDataMarker>::KEY, req))
                }
        }
    }
}
#[doc(inline)]
pub use __impl_plurals_cardinal_v1 as impl_plurals_cardinal_v1;

@Manishearth
Copy link
Member Author

Closed in favor of #3491

@Manishearth Manishearth closed this Jun 6, 2023
@Manishearth Manishearth reopened this Jun 6, 2023
@Manishearth
Copy link
Member Author

Reopening since #3491 has discovered a new problem

@Manishearth
Copy link
Member Author

Re-closing, this is broken on windows for the same reason as #3491, so that's a clear win.

(Plus that PR has done a bunch of other nice things like splitting into files)

@Manishearth Manishearth closed this Jun 6, 2023
@Manishearth Manishearth deleted the baked-fmt branch June 6, 2023 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix pretty printing for baked data
2 participants