@@ -8,12 +8,12 @@ use crate::{
8
8
sandbox:: { self , Channel , Sandbox , DOCKER_PROCESS_TIMEOUT_SOFT } ,
9
9
CachingSnafu , ClippyRequest , ClippyResponse , CompilationSnafu , CompileRequest , CompileResponse ,
10
10
CompileSnafu , Config , CreateCoordinatorSnafu , Error , ErrorJson , EvaluateRequest ,
11
- EvaluateResponse , EvaluationSnafu , ExecuteRequest , ExecuteResponse , ExecuteSnafu ,
12
- ExecutionSnafu , ExpansionSnafu , FormatRequest , FormatResponse , FormattingSnafu , GhToken ,
13
- GistCreationSnafu , GistLoadingSnafu , InterpretingSnafu , LintingSnafu , MacroExpansionRequest ,
14
- MacroExpansionResponse , MetaCratesResponse , MetaGistCreateRequest , MetaGistResponse ,
15
- MetaVersionResponse , MetricsToken , MiriRequest , MiriResponse , Result , SandboxCreationSnafu ,
16
- ShutdownCoordinatorSnafu , TimeoutSnafu ,
11
+ EvaluateResponse , EvaluateSnafu , EvaluationSnafu , ExecuteRequest , ExecuteResponse ,
12
+ ExecuteSnafu , ExecutionSnafu , ExpansionSnafu , FormatRequest , FormatResponse , FormattingSnafu ,
13
+ GhToken , GistCreationSnafu , GistLoadingSnafu , InterpretingSnafu , LintingSnafu ,
14
+ MacroExpansionRequest , MacroExpansionResponse , MetaCratesResponse , MetaGistCreateRequest ,
15
+ MetaGistResponse , MetaVersionResponse , MetricsToken , MiriRequest , MiriResponse , Result ,
16
+ SandboxCreationSnafu , ShutdownCoordinatorSnafu , TimeoutSnafu ,
17
17
} ;
18
18
use async_trait:: async_trait;
19
19
use axum:: {
@@ -151,15 +151,24 @@ async fn rewrite_help_as_index<B>(
151
151
152
152
// This is a backwards compatibilty shim. The Rust documentation uses
153
153
// this to run code in place.
154
- async fn evaluate ( Json ( req) : Json < EvaluateRequest > ) -> Result < Json < EvaluateResponse > > {
155
- with_sandbox_force_endpoint (
156
- req,
157
- Endpoint :: Evaluate ,
158
- |sb, req| async move { sb. execute ( req) . await } . boxed ( ) ,
159
- EvaluationSnafu ,
160
- )
161
- . await
162
- . map ( Json )
154
+ async fn evaluate (
155
+ Extension ( use_orchestrator) : Extension < OrchestratorEnabled > ,
156
+ Json ( req) : Json < EvaluateRequest > ,
157
+ ) -> Result < Json < EvaluateResponse > > {
158
+ if use_orchestrator. 0 {
159
+ with_coordinator ( req, |c, req| c. execute ( req) . context ( EvaluateSnafu ) . boxed ( ) )
160
+ . await
161
+ . map ( Json )
162
+ } else {
163
+ with_sandbox_force_endpoint (
164
+ req,
165
+ Endpoint :: Evaluate ,
166
+ |sb, req| async move { sb. execute ( req) . await } . boxed ( ) ,
167
+ EvaluationSnafu ,
168
+ )
169
+ . await
170
+ . map ( Json )
171
+ }
163
172
}
164
173
165
174
async fn compile (
@@ -283,6 +292,10 @@ pub(crate) trait HasEndpoint {
283
292
const ENDPOINT : Endpoint ;
284
293
}
285
294
295
+ impl HasEndpoint for EvaluateRequest {
296
+ const ENDPOINT : Endpoint = Endpoint :: Evaluate ;
297
+ }
298
+
286
299
impl HasEndpoint for CompileRequest {
287
300
const ENDPOINT : Endpoint = Endpoint :: Compile ;
288
301
}
@@ -800,6 +813,83 @@ pub(crate) mod api_orchestrator_integration_impls {
800
813
use snafu:: prelude:: * ;
801
814
use std:: convert:: TryFrom ;
802
815
816
+ impl TryFrom < crate :: EvaluateRequest > for ExecuteRequest {
817
+ type Error = ParseEvaluateRequestError ;
818
+
819
+ fn try_from ( other : crate :: EvaluateRequest ) -> Result < Self , Self :: Error > {
820
+ let crate :: EvaluateRequest {
821
+ version,
822
+ optimize,
823
+ code,
824
+ edition,
825
+ tests,
826
+ } = other;
827
+
828
+ let mode = if optimize != "0" {
829
+ Mode :: Release
830
+ } else {
831
+ Mode :: Debug
832
+ } ;
833
+
834
+ let edition = if edition. trim ( ) . is_empty ( ) {
835
+ Edition :: Rust2015
836
+ } else {
837
+ parse_edition ( & edition) ?
838
+ } ;
839
+
840
+ Ok ( ExecuteRequest {
841
+ channel : parse_channel ( & version) ?,
842
+ mode,
843
+ edition,
844
+ crate_type : CrateType :: Binary ,
845
+ tests,
846
+ backtrace : false ,
847
+ code,
848
+ } )
849
+ }
850
+ }
851
+
852
+ #[ derive( Debug , Snafu ) ]
853
+ pub ( crate ) enum ParseEvaluateRequestError {
854
+ #[ snafu( context( false ) ) ]
855
+ Channel { source : ParseChannelError } ,
856
+
857
+ #[ snafu( context( false ) ) ]
858
+ Edition { source : ParseEditionError } ,
859
+ }
860
+
861
+ impl From < WithOutput < ExecuteResponse > > for crate :: EvaluateResponse {
862
+ fn from ( other : WithOutput < ExecuteResponse > ) -> Self {
863
+ let WithOutput {
864
+ response,
865
+ stdout,
866
+ stderr,
867
+ } = other;
868
+
869
+ // The old playground didn't use Cargo, so it never had the
870
+ // Cargo output ("Compiling playground...") which is printed
871
+ // to stderr. Since this endpoint is used to inline results on
872
+ // the page, don't include the stderr unless an error
873
+ // occurred.
874
+ if response. success {
875
+ crate :: EvaluateResponse {
876
+ result : stdout,
877
+ error : None ,
878
+ }
879
+ } else {
880
+ // When an error occurs, *some* consumers check for an
881
+ // `error` key, others assume that the error is crammed in
882
+ // the `result` field and then they string search for
883
+ // `error:` or `warning:`. Ew. We can put it in both.
884
+ let result = stderr + & stdout;
885
+ crate :: EvaluateResponse {
886
+ result : result. clone ( ) ,
887
+ error : Some ( result) ,
888
+ }
889
+ }
890
+ }
891
+ }
892
+
803
893
impl TryFrom < crate :: CompileRequest > for CompileRequest {
804
894
type Error = ParseCompileRequestError ;
805
895
0 commit comments