-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpq.rs
93 lines (76 loc) · 2.25 KB
/
pq.rs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::convert::TryFrom;
use std::io::{self, Read};
use std::path::PathBuf;
use std::str::FromStr;
use structopt::StructOpt;
use piqel::lang::{Lang, LangType};
use piqel::parser;
use piqel::planner::evaluate;
use piqel::sql::Sql;
fn read_from_stdin() -> anyhow::Result<String> {
let mut buf = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
handle.read_to_string(&mut buf)?;
Ok(buf)
}
#[derive(StructOpt, Debug)]
struct Opt {
/// source text: file or standard input
#[structopt()]
file_or_stdin: Option<PathBuf>,
/// sql
#[structopt(short, long)]
query: Option<String>,
/// target config file
#[structopt(short, long, possible_values(&["csv", "json", "toml", "yaml", "xml"]))]
from: Option<String>,
/// target config file
#[structopt(short, long, possible_values(&["csv", "json", "toml", "yaml", "xml"]))]
to: Option<String>,
/// sort keys of objects on output. it on works when --to option is json, currently
#[structopt(short = "S", long)]
sort_keys: bool,
/// compact instead of pretty-printed output, only when outputting in JSON
#[structopt(short, long)]
compact: bool,
}
fn main() -> anyhow::Result<()> {
let Opt {
file_or_stdin,
query,
from,
to,
sort_keys,
compact,
} = Opt::from_args();
let _ = {
let input = if let Some(file) = file_or_stdin {
std::fs::read_to_string(file)?
} else {
read_from_stdin()?
};
let mut lang = if let Some(s_lang_type) = from {
let lang_type = LangType::from_str(&s_lang_type)?;
Lang::from_as(&input, lang_type)?
} else {
Lang::from_str(&input)?
};
if let Some(t) = to {
match LangType::from_str(&t) {
Ok(lang_type) => lang.to = lang_type,
Err(err) => eprintln!("not support"),
}
}
if let Some(q) = query {
let sql = Sql::from_str(&q)?;
let result = evaluate(sql, lang.data);
lang.data = result;
}
if lang.to == LangType::Json && sort_keys {
lang.sort_keys();
}
lang.print(compact);
};
Ok(())
}