1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! # Additional toolings for rust
//! rust_hero is a rust assistant that utilizes NLP to program rust more efficiently. It supports `unsafe` and `lifetime` (todo) prediction.
//! # `unsafe` prediction
//! For each function in Rust, the `unsafe` keyword utilizes the unsafe superpowers. However, the `unsafe` keyword is not necessary if it can be taken out while the program is compiled successfully.
//!
//! `rust_hero` infers the necessity of `unsafe` keywords without the need of recompiling. `rust_hero` trains a [microsoft/codebert](https://github.com/microsoft/CodeBERT) based model and take advantage of bert's strong reasoning capability to inference the necessity of `unsafe`.
//!
//! # `lifetime` prediction
//! todo
//!
//! # Usage
//!
//! ```no_run
//!
//! use anyhow::{Context, Result};
//! use rust_hero::query::{Invocation, QueryFormat};
//! use rust_hero::safe::{show_languages, SafeLanguageModel};
//! use std::env;
//! use std::io::{self, BufWriter, Write};
//!
//! pub fn main() {
//! let mut buffer = BufWriter::new(io::stdout());
//!
//! if let Err(error) = try_main(env::args().collect(), &mut buffer) {
//! if let Some(err) = error.downcast_ref::<io::Error>() {
//! // a broken pipe is totally normal and fine. It's what we get when
//! // we pipe to something like `head` that only takes a certain number
//! // of lines.
//! if err.kind() == io::ErrorKind::BrokenPipe {
//! std::process::exit(0);
//! }
//! }
//!
//! if let Some(clap_error) = error.downcast_ref::<clap::Error>() {
//! // Clap errors (--help or misuse) are already well-formatted,
//! // so we don't have to do any additional work.
//! eprint!("{}", clap_error);
//! } else {
//! eprintln!("{:?}", error);
//! }
//!
//! std::process::exit(1);
//! }
//!
//! buffer.flush().expect("failed to flush buffer!");
//! }
//!
//! pub fn try_main(args: Vec<String>, out: impl Write) -> Result<()> {
//! let invocation = Invocation::from_args(args)
//! .context("couldn't get a valid configuration from the command-line options")?;
//! match invocation {
//! Invocation::DoQuery(query_opts) => {
//! let safe_model = SafeLanguageModel::new(query_opts)?;
//! match safe_model.get_opt().format {
//! QueryFormat::Classes => {
//! let output = safe_model
//! .predict()
//! .context("couldn't perform the prediction")?;
//! for label in output {
//! println!("{:?}", label);
//! }
//! Ok(())
//! }
//! _ => safe_model.do_query(out),
//! }
//! }
//! Invocation::ShowLanguages => {
//! show_languages(out).context("couldn't show the list of languages")
//! }
//! }
//! }
//! ```
//!