Skip to content

Commit 2087266

Browse files
committed
playwarn
1 parent 1e57d5a commit 2087266

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn app() -> Result<()> {
113113
if config.eval {
114114
// rust playground
115115
cmds.add(
116-
"?play mode={} edition={} channel={} ```\ncode```",
116+
"?play mode={} edition={} channel={} warn={} ```\ncode```",
117117
playground::run,
118118
);
119119
cmds.add("?play code...", playground::err);
@@ -124,15 +124,15 @@ fn app() -> Result<()> {
124124
);
125125

126126
cmds.add(
127-
"?eval mode={} edition={} channel={} ```\ncode```",
127+
"?eval mode={} edition={} channel={} warn={} ```\ncode```",
128128
playground::eval,
129129
);
130130
cmds.add(
131-
"?eval mode={} edition={} channel={} ```code```",
131+
"?eval mode={} edition={} channel={} warn={} ```code```",
132132
playground::eval,
133133
);
134134
cmds.add(
135-
"?eval mode={} edition={} channel={} `code`",
135+
"?eval mode={} edition={} channel={} warn={} `code`",
136136
playground::eval,
137137
);
138138
cmds.add("?eval code...", playground::eval_err);

src/playground.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use serde::{Deserialize, Serialize};
1010
use std::collections::HashMap;
1111
use std::str::FromStr;
1212

13+
const MAX_OUTPUT_LINES: usize = 45;
14+
1315
#[derive(Debug, Serialize)]
1416
struct PlaygroundCode<'a> {
1517
channel: Channel,
@@ -135,6 +137,7 @@ struct PlayResult {
135137
fn run_code(args: &Args, code: &str) -> Result<String> {
136138
let mut errors = String::new();
137139

140+
let warnings = args.params.get("warn").unwrap_or_else(|| &"false");
138141
let channel = args.params.get("channel").unwrap_or_else(|| &"nightly");
139142
let mode = args.params.get("mode").unwrap_or_else(|| &"debug");
140143
let edition = args.params.get("edition").unwrap_or_else(|| &"2018");
@@ -168,23 +171,29 @@ fn run_code(args: &Args, code: &str) -> Result<String> {
168171

169172
let result: PlayResult = resp.json()?;
170173

171-
let result = if result.success {
174+
let result = if *warnings == "true" {
175+
format!("{}\n{}", result.stderr, result.stdout)
176+
} else if result.success {
172177
result.stdout
173178
} else {
174179
result.stderr
175180
};
176181

177-
Ok(if result.len() + errors.len() > 1994 {
178-
format!(
179-
"{}Output too large. Playground link: {}",
180-
errors,
181-
get_playground_link(args, code, &request)?
182-
)
183-
} else if result.len() == 0 {
184-
format!("{}compilation succeded.", errors)
185-
} else {
186-
format!("{}```{}```", errors, result)
187-
})
182+
let lines = result.lines().count();
183+
184+
Ok(
185+
if result.len() + errors.len() > 1993 || lines > MAX_OUTPUT_LINES {
186+
format!(
187+
"{}Output too large. Playground link: {}",
188+
errors,
189+
get_playground_link(args, code, &request)?
190+
)
191+
} else if result.len() == 0 {
192+
format!("{}compilation succeded.", errors)
193+
} else {
194+
format!("{}```\n{}```", errors, result)
195+
},
196+
)
188197
}
189198

190199
fn get_playground_link(args: &Args, code: &str, request: &PlaygroundCode) -> Result<String> {
@@ -220,11 +229,12 @@ pub fn run(args: Args) -> Result<()> {
220229
pub fn help(args: Args, name: &str) -> Result<()> {
221230
let message = format!(
222231
"Compile and run rust code. All code is executed on https://play.rust-lang.org.
223-
```?{} mode={{}} channel={{}} edition={{}} ``\u{200B}`code``\u{200B}` ```
232+
```?{} mode={{}} channel={{}} edition={{}} warn={{}} ``\u{200B}`code``\u{200B}` ```
224233
Optional arguments:
225234
\tmode: debug, release (default: debug)
226235
\tchannel: stable, beta, nightly (default: nightly)
227236
\tedition: 2015, 2018 (default: 2018)
237+
\twarn: boolean flag to enable compilation warnings
228238
",
229239
name
230240
);

0 commit comments

Comments
 (0)