Skip to content

Commit 362af16

Browse files
authored
Support stdin for test data command (#71)
* Exclude assets from crates.io * Support stdin for test data
1 parent 7246a9c commit 362af16

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "leetup"
3-
version = "1.2.1"
3+
version = "1.2.3"
44
authors = ["dragfire <[email protected]>"]
55
edition = "2018"
66
description = "Leetcode cli"
@@ -10,6 +10,9 @@ homepage = "https://github.com/dragfire/leetup"
1010
repository = "https://github.com/dragfire/leetup"
1111
keywords = ["cli", "leetcode"]
1212
categories = ["command-line-utilities"]
13+
exclude = [
14+
"assets/*"
15+
]
1316

1417
[dependencies]
1518
leetup-cache = { path = "./cache", version = "0.1.0"}

src/cmd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct Test {
7474

7575
/// Custom test cases.
7676
#[structopt(short)]
77-
pub test_data: String,
77+
pub test_data: Option<Option<String>>,
7878
}
7979

8080
#[derive(Debug, StructOpt)]

src/service/leetcode.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cmp::Ord;
22
use std::collections::HashMap;
33
use std::env;
44
use std::fs::{self, File};
5-
use std::io::prelude::*;
5+
use std::io::{prelude::*, stdin};
66
use std::ops::Deref;
77
use std::path::{Path, PathBuf};
88

@@ -203,7 +203,9 @@ impl<'a> ServiceProvider<'a> for Leetcode<'a> {
203203

204204
async fn problem_test(&self, test: cmd::Test) -> Result<()> {
205205
let problem = service::extract_problem(test.filename)?;
206-
let test_data = test.test_data.replace("\\n", "\n");
206+
207+
let test_data = self.get_test_data(test.test_data);
208+
debug!("Test data: {:?}", test_data);
207209
let typed_code = parse_code(problem.typed_code.as_ref().expect("Expected typed_code"));
208210
let body = json!({
209211
"lang": problem.lang.to_owned(),
@@ -624,4 +626,31 @@ impl<'a> Leetcode<'a> {
624626

625627
Ok(())
626628
}
629+
630+
/*
631+
* Parse Option<Option<String>> from structopt
632+
*
633+
* Get string from command line if provided, otherwise try to get string from stdin
634+
*
635+
* We can provide test data as multiline input using stdin.
636+
*
637+
* # Example:
638+
* ```bash
639+
* leetup test 3sum.java -t << END
640+
* [1,-1,0]
641+
* [0, 1, 1, 1, 2, -3, -1]
642+
* [1,2,3]
643+
* END
644+
* ```
645+
*/
646+
fn get_test_data(&self, test_data: Option<Option<String>>) -> String {
647+
test_data.unwrap().unwrap_or_else(|| {
648+
let mut buf = String::new();
649+
stdin()
650+
.lock()
651+
.read_to_string(&mut buf)
652+
.expect("test input expected from stdin");
653+
buf
654+
})
655+
}
627656
}

0 commit comments

Comments
 (0)