Skip to content

Commit

Permalink
Use the currently selected channel to format code
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed Dec 1, 2023
1 parent ad26813 commit bbb69a8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion ui/frontend/reducers/output/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ interface State {
}

interface FormatRequestBody {
code: string;
channel: string;
edition: string;
code: string;
}

const FormatResponseBody = z.object({
Expand Down
26 changes: 21 additions & 5 deletions ui/frontend/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,27 @@ const LABELS: { [index in PrimaryActionCore]: string } = {

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

const channelSelector = (state: State) => state.configuration.channel;

const selectedChannelVersionsSelector = createSelector(
channelSelector,
(state: State) => state.versions,
(channel, versions) => {
switch (channel) {
case Channel.Stable:
return versions.stable;
case Channel.Beta:
return versions.beta;
case Channel.Nightly:
return versions.nightly;
}
},
)

const getStable = (state: State) => state.versions.stable?.rustc;
const getBeta = (state: State) => state.versions.beta?.rustc;
const getNightly = (state: State) => state.versions.nightly?.rustc;
const getRustfmt = (state: State) => state.versions.nightly?.rustfmt;
const getRustfmt = createSelector(selectedChannelVersionsSelector, (versions) => versions?.rustfmt);
const getClippy = (state: State) => state.versions.nightly?.clippy;
const getMiri = (state: State) => state.versions?.nightly?.miri;

Expand All @@ -123,8 +140,6 @@ export const miriVersionDetailsText = createSelector(getMiri, versionDetails);

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

const channelSelector = (state: State) => state.configuration.channel;

export const isNightlyChannel = createSelector(
channelSelector,
(channel) => channel === Channel.Nightly,
Expand Down Expand Up @@ -311,9 +326,10 @@ export const clippyRequestSelector = createSelector(
);

export const formatRequestSelector = createSelector(
codeSelector,
channelSelector,
editionSelector,
(code, edition) => ({ code, edition }),
codeSelector,
(channel, edition, code) => ({ channel, edition, code }),
);

const focus = (state: State) => state.output.meta.focus;
Expand Down
4 changes: 3 additions & 1 deletion ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,11 @@ struct ExecuteResponse {

#[derive(Debug, Clone, Deserialize)]
struct FormatRequest {
code: String,
#[serde(default)]
channel: Option<String>,
#[serde(default)]
edition: String,
code: String,
}

#[derive(Debug, Clone, Serialize)]
Expand Down
16 changes: 14 additions & 2 deletions ui/src/server_axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,10 +1052,19 @@ pub(crate) mod api_orchestrator_integration_impls {
type Error = ParseFormatRequestError;

fn try_from(other: crate::FormatRequest) -> std::result::Result<Self, Self::Error> {
let crate::FormatRequest { code, edition } = other;
let crate::FormatRequest {
channel,
edition,
code,
} = other;

let channel = match channel {
Some(c) => parse_channel(&c)?,
None => Channel::Nightly,
};

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

#[derive(Debug, Snafu)]
pub(crate) enum ParseFormatRequestError {
#[snafu(context(false))]
Channel { source: ParseChannelError },

#[snafu(context(false))]
Edition { source: ParseEditionError },
}
Expand Down

0 comments on commit bbb69a8

Please sign in to comment.