├── .gitignore ├── Cargo.toml ├── README.md └── src ├── lib.rs └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | *.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "prlink" 3 | version = "0.1.0" 4 | authors = ["Pradeep Gowda "] 5 | 6 | 7 | [dependencies] 8 | glob = "*" 9 | clap = "*" 10 | clippy = { version = "*", optional = true } 11 | 12 | [dependencies.url] 13 | version = "1.7.0" 14 | features = ["query_encoding"] 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PRLink 2 | 3 | Read all the Rust files in a directory and create 4 | [play.rust-lang.org](http://play.rust-lang.org) links in Markdown 5 | format. 6 | 7 | That is, if a directory has a file `hello.rs` with the following 8 | content: 9 | 10 | ```rust 11 | fn main() { 12 | println!("Hello, world!"); 13 | } 14 | ``` 15 | 16 | Running `prlink` should result in : 17 | 18 | ``` 19 | - ["hello.rs"](http://play.rust-lang.org/?code=fn+main%28%29+%7B%0A++++println%21%28%22Hello%2C+world%21%22%29%3B%0A%7D%0A) 20 | ``` 21 | 22 | Click on the link below to see the source code of this console program 23 | in rust playground. 24 | 25 | 26 | - ["main.rs"](http://play.rust-lang.org/?code=%2F%2F+%23%21%5Bfeature%28plugin%29%5D%0A%2F%2F+%23%21%5Bplugin%28clippy%29%5D%0A%0A%0Aextern+crate+clap%3B%0Aextern+crate+glob%3B%0Aextern+crate+url%3B%0A%0Ause+clap%3A%3A%7BApp%2C+Arg%7D%3B%0Ause+std%3A%3Aerror%3A%3AError%3B%0Ause+std%3A%3Afs%3A%3AFile%3B%0Ause+std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause+url%3A%3Aform_urlencoded%3A%3A%7Bserialize%7D%3B%0Ause+glob%3A%3Aglob%3B%0Ause+std%3A%3Apath%3A%3APathBuf%3B%0A%0A%0Afn+read_file%28path%3A+String%29+-%3E+String+%7B%0A++++let+mut+file+%3D+match+File%3A%3Aopen%28%26path%29+%7B%0A++++++++Err%28why%29+%3D%3E+panic%21%28%22couldn%27t+open+%7B%7D%3A+%7B%7D%22%2C+path%2C+Error%3A%3Adescription%28%26why%29%29%2C%0A++++++++Ok%28file%29+%3D%3E+file%2C%0A++++%7D%3B%0A++++let+mut+s+%3D+String%3A%3Anew%28%29%3B%0A++++match+file.read_to_string%28%26mut+s%29+%7B%0A++++++++Err%28why%29+%3D%3E+panic%21%28%22couldn%27t+read+%7B%7D%3A+%7B%7D%22%2C+path%2C+Error%3A%3Adescription%28%26why%29%29%2C%0A++++++++Ok%28_%29+%3D%3E+s%2C%0A++++%7D%0A%7D%0A%0A%0Afn+main%28%29+%7B%0A++++let+matches+%3D+App%3A%3Anew%28%22prlink%22%29%0A++++++++.version%28%221.0%22%29%0A++++++++.author%28%22Pradeep+Gowda+%3Cpradeep%40btbytes.com%3E%22%29%0A++++++++.about%28%22Print+Rust+playground+links%22%29%0A++++++++.arg%28Arg%3A%3Awith_name%28%22INPUT%22%29%0A+++++++++++++.help%28%22Sets+the+input+path+to+use%22%29%0A+++++++++++++.required%28true%29%0A+++++++++++++.index%281%29%29%0A++++++++.get_matches%28%29%3B%0A++++let+dirpath+%3D+matches.value_of%28%22INPUT%22%29.unwrap%28%29%3B%0A++++let+relative_path+%3D+PathBuf%3A%3Afrom%28dirpath%29%3B%0A++++let+mut+absolute_path+%3D+std%3A%3Aenv%3A%3Acurrent_dir%28%29.unwrap%28%29%3B%0A++++absolute_path.push%28relative_path%29%3B%0A++++absolute_path.push%28%22*.rs%22%29%3B%0A%0A++++for+entry+in+glob%28absolute_path.to_str%28%29.unwrap%28%29%29.unwrap%28%29+%7B%0A++++++++match+entry+%7B%0A++++++++++++Ok%28path%29+%3D%3E+%7B%0A++++++++++++++++let+data+%3D+%26%5B%28%22code%22.to_string%28%29%2C+read_file%28path.to_str%28%29.unwrap%28%29.to_string%28%29%29%29%5D%3B%0A++++++++++++++++let+s+%3D+serialize%28data%29%3B%0A++++++++++++++++let+fname+%3D+path.to_str%28%29.unwrap%28%29.to_string%28%29.split%28%27%2F%27%29.last%28%29.unwrap%28%29.to_string%28%29%3B%0A++++++++++++++++println%21%28%22-+%5B%7B%3A%3F%7D%5D%28http%3A%2F%2Fplay.rust-lang.org%2F%3F%7B%7D%29%22%2C+fname%2C+s%29%3B%0A++++++++++++%7D%2C%0A++++++++++++Err%28e%29+%3D%3E+println%21%28%22%7B%3A%3F%7D%22%2C+e%29%2C%0A++++++++%7D%0A++++%7D%0A%7D%0A) 27 | 28 | 29 | The above link was generated by running: 30 | 31 | ```shell 32 | ./target/debug/prlink ./src 33 | ``` 34 | 35 | **Update 2015/9/17**: I wrote this program essentially to make it easy for [https://github.com/carols10cents/rustlings](https://github.com/carols10cents/rustlings) to create links to the Rust Playground. And [Carol](https://github.com/carols10cents) is using it. Yay! 36 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(feature="clippy", feature(plugin))] 2 | #![cfg_attr(feature="clippy", plugin(clippy))] 3 | 4 | extern crate url; 5 | 6 | use url::form_urlencoded::Serializer; 7 | use std::error::Error; 8 | use std::fs::File; 9 | use std::io::prelude::*; 10 | use std::path::PathBuf; 11 | 12 | pub fn linkify_file(path: &PathBuf) -> String { 13 | let data = &[("code".to_owned(), read_file(path.to_str().unwrap()))]; 14 | let s = Serializer::new(String::new()) 15 | .extend_pairs(data) 16 | .finish(); 17 | let fname = path.file_name().unwrap().to_string_lossy(); 18 | format!("- [{:?}](https://play.rust-lang.org/?{})", fname, s) 19 | } 20 | 21 | fn read_file(path: &str) -> String { 22 | let mut file = match File::open(&path) { 23 | Err(why) => panic!("couldn't open {}: {}", path, Error::description(&why)), 24 | Ok(file) => file, 25 | }; 26 | let mut s = String::new(); 27 | match file.read_to_string(&mut s) { 28 | Err(why) => panic!("couldn't read {}: {}", path, Error::description(&why)), 29 | Ok(_) => s, 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(feature="clippy", feature(plugin))] 2 | #![cfg_attr(feature="clippy", plugin(clippy))] 3 | 4 | extern crate clap; 5 | extern crate glob; 6 | extern crate prlink; 7 | 8 | use clap::{App, Arg}; 9 | use glob::glob; 10 | use std::path::PathBuf; 11 | 12 | fn main() { 13 | let matches = App::new("prlink") 14 | .version("1.0") 15 | .author("Pradeep Gowda ") 16 | .about("Print Rust playground links") 17 | .arg(Arg::with_name("INPUT") 18 | .help("Sets the input path to use") 19 | .required(true) 20 | .index(1)) 21 | .get_matches(); 22 | let dirpath = matches.value_of("INPUT").unwrap(); 23 | let relative_path = PathBuf::from(dirpath); 24 | let mut absolute_path = std::env::current_dir().unwrap(); 25 | absolute_path.push(relative_path); 26 | absolute_path.push("*.rs"); 27 | 28 | for entry in glob(absolute_path.to_str().unwrap()).unwrap() { 29 | match entry { 30 | Ok(path) => { 31 | println!("{}", prlink::linkify_file(&path)); 32 | }, 33 | Err(e) => println!("{:?}", e), 34 | } 35 | } 36 | } 37 | --------------------------------------------------------------------------------