Skip to content

Commit bdcd536

Browse files
authored
Merge pull request #1007 from rust-lang/rustfmt-channel
Use the currently selected channel to format code
2 parents b4b8caf + bbb69a8 commit bdcd536

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

ui/frontend/reducers/output/format.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2+
import * as z from 'zod';
23

34
import { adaptFetchError, jsonPost, routes } from '../../actions';
45
import { formatRequestSelector } from '../../selectors';
@@ -17,23 +18,27 @@ interface State {
1718
}
1819

1920
interface FormatRequestBody {
20-
code: string;
21+
channel: string;
2122
edition: string;
22-
}
23-
24-
interface FormatResponseBody {
25-
success: boolean;
2623
code: string;
27-
stdout: string;
28-
stderr: string;
2924
}
3025

26+
const FormatResponseBody = z.object({
27+
success: z.boolean(),
28+
code: z.string(),
29+
stdout: z.string(),
30+
stderr: z.string(),
31+
});
32+
33+
type FormatResponseBody = z.infer<typeof FormatResponseBody>;
34+
3135
export const performFormat = createAsyncThunk<FormatResponseBody, void, { state: RootState }>(
3236
sliceName,
3337
async (_arg: void, { getState }) => {
3438
const body: FormatRequestBody = formatRequestSelector(getState());
3539

36-
return adaptFetchError(() => jsonPost<FormatResponseBody>(routes.format, body));
40+
const d = await adaptFetchError(() => jsonPost(routes.format, body));
41+
return FormatResponseBody.parseAsync(d);
3742
},
3843
);
3944

ui/frontend/selectors/index.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,27 @@ const LABELS: { [index in PrimaryActionCore]: string } = {
9999

100100
export const getExecutionLabel = createSelector(primaryActionSelector, primaryAction => LABELS[primaryAction]);
101101

102+
const channelSelector = (state: State) => state.configuration.channel;
103+
104+
const selectedChannelVersionsSelector = createSelector(
105+
channelSelector,
106+
(state: State) => state.versions,
107+
(channel, versions) => {
108+
switch (channel) {
109+
case Channel.Stable:
110+
return versions.stable;
111+
case Channel.Beta:
112+
return versions.beta;
113+
case Channel.Nightly:
114+
return versions.nightly;
115+
}
116+
},
117+
)
118+
102119
const getStable = (state: State) => state.versions.stable?.rustc;
103120
const getBeta = (state: State) => state.versions.beta?.rustc;
104121
const getNightly = (state: State) => state.versions.nightly?.rustc;
105-
const getRustfmt = (state: State) => state.versions.nightly?.rustfmt;
122+
const getRustfmt = createSelector(selectedChannelVersionsSelector, (versions) => versions?.rustfmt);
106123
const getClippy = (state: State) => state.versions.nightly?.clippy;
107124
const getMiri = (state: State) => state.versions?.nightly?.miri;
108125

@@ -123,8 +140,9 @@ export const miriVersionDetailsText = createSelector(getMiri, versionDetails);
123140

124141
const editionSelector = (state: State) => state.configuration.edition;
125142

126-
export const isNightlyChannel = (state: State) => (
127-
state.configuration.channel === Channel.Nightly
143+
export const isNightlyChannel = createSelector(
144+
channelSelector,
145+
(channel) => channel === Channel.Nightly,
128146
);
129147
export const isHirAvailable = isNightlyChannel;
130148

@@ -142,10 +160,7 @@ export const getModeLabel = (state: State) => {
142160
return `${mode}`;
143161
};
144162

145-
export const getChannelLabel = (state: State) => {
146-
const { configuration: { channel } } = state;
147-
return `${channel}`;
148-
};
163+
export const getChannelLabel = createSelector(channelSelector, (channel) => `${channel}`);
149164

150165
export const isEditionDefault = createSelector(
151166
editionSelector,
@@ -311,9 +326,10 @@ export const clippyRequestSelector = createSelector(
311326
);
312327

313328
export const formatRequestSelector = createSelector(
314-
codeSelector,
329+
channelSelector,
315330
editionSelector,
316-
(code, edition) => ({ code, edition }),
331+
codeSelector,
332+
(channel, edition, code) => ({ channel, edition, code }),
317333
);
318334

319335
const focus = (state: State) => state.output.meta.focus;
@@ -387,11 +403,12 @@ export const websocketStatusSelector = createSelector(
387403

388404
export const executeRequestPayloadSelector = createSelector(
389405
codeSelector,
406+
channelSelector,
390407
(state: State) => state.configuration,
391408
getBacktraceSet,
392409
(_state: State, { crateType, tests }: { crateType: string, tests: boolean }) => ({ crateType, tests }),
393-
(code, configuration, backtrace, { crateType, tests }) => ({
394-
channel: configuration.channel,
410+
(code, channel, configuration, backtrace, { crateType, tests }) => ({
411+
channel,
395412
mode: configuration.mode,
396413
edition: configuration.edition,
397414
crateType,
@@ -403,13 +420,14 @@ export const executeRequestPayloadSelector = createSelector(
403420

404421
export const compileRequestPayloadSelector = createSelector(
405422
codeSelector,
423+
channelSelector,
406424
(state: State) => state.configuration,
407425
getCrateType,
408426
runAsTest,
409427
getBacktraceSet,
410428
(_state: State, { target }: { target: string }) => ({ target }),
411-
(code, configuration, crateType, tests, backtrace, { target }) => ({
412-
channel: configuration.channel,
429+
(code, channel, configuration, crateType, tests, backtrace, { target }) => ({
430+
channel,
413431
mode: configuration.mode,
414432
edition: configuration.edition,
415433
crateType,

ui/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,11 @@ struct ExecuteResponse {
355355

356356
#[derive(Debug, Clone, Deserialize)]
357357
struct FormatRequest {
358-
code: String,
358+
#[serde(default)]
359+
channel: Option<String>,
359360
#[serde(default)]
360361
edition: String,
362+
code: String,
361363
}
362364

363365
#[derive(Debug, Clone, Serialize)]

ui/src/server_axum.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,10 +1052,19 @@ pub(crate) mod api_orchestrator_integration_impls {
10521052
type Error = ParseFormatRequestError;
10531053

10541054
fn try_from(other: crate::FormatRequest) -> std::result::Result<Self, Self::Error> {
1055-
let crate::FormatRequest { code, edition } = other;
1055+
let crate::FormatRequest {
1056+
channel,
1057+
edition,
1058+
code,
1059+
} = other;
1060+
1061+
let channel = match channel {
1062+
Some(c) => parse_channel(&c)?,
1063+
None => Channel::Nightly,
1064+
};
10561065

10571066
Ok(FormatRequest {
1058-
channel: Channel::Nightly, // TODO: use what user has submitted
1067+
channel,
10591068
crate_type: CrateType::Binary, // TODO: use what user has submitted
10601069
edition: parse_edition(&edition)?,
10611070
code,
@@ -1065,6 +1074,9 @@ pub(crate) mod api_orchestrator_integration_impls {
10651074

10661075
#[derive(Debug, Snafu)]
10671076
pub(crate) enum ParseFormatRequestError {
1077+
#[snafu(context(false))]
1078+
Channel { source: ParseChannelError },
1079+
10681080
#[snafu(context(false))]
10691081
Edition { source: ParseEditionError },
10701082
}

0 commit comments

Comments
 (0)