nu_plugin_from_sqlite/nu/
mod.rs1#[cfg(test)]
2mod tests;
3
4use crate::FromSqlite;
5use nu_errors::ShellError;
6use nu_plugin::Plugin;
7use nu_protocol::{CallInfo, Primitive, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value};
8use nu_source::Tag;
9
10fn convert_columns(columns: &[Value]) -> Result<Vec<String>, ShellError> {
12    let res = columns
13        .iter()
14        .map(|value| match &value.value {
15            UntaggedValue::Primitive(Primitive::String(s)) => Ok(s.clone()),
16            _ => Err(ShellError::labeled_error(
17                "Incorrect column format",
18                "Only string as column name",
19                &value.tag,
20            )),
21        })
22        .collect::<Result<Vec<String>, _>>()?;
23
24    Ok(res)
25}
26
27impl Plugin for FromSqlite {
28    fn config(&mut self) -> Result<Signature, ShellError> {
29        Ok(Signature::build("from sqlite")
30            .named(
31                "tables",
32                SyntaxShape::Table,
33                "Only convert specified tables",
34                Some('t'),
35            )
36            .desc("Convert from sqlite binary into table")
37            .filter())
38    }
39
40    fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
41        self.name_tag = call_info.name_tag;
42
43        if let Some(t) = call_info.args.get("tables") {
44            if let UntaggedValue::Table(columns) = t.value.clone() {
45                self.tables = convert_columns(columns.as_slice())?;
46            }
47        }
48        Ok(vec![])
49    }
50
51    fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
52        match input {
53            Value {
54                value: UntaggedValue::Primitive(Primitive::Binary(b)),
55                ..
56            } => {
57                self.state.extend_from_slice(&b);
58            }
59            Value { tag, .. } => {
60                return Err(ShellError::labeled_error_with_secondary(
61                    "Expected binary from pipeline",
62                    "requires binary input",
63                    self.name_tag.clone(),
64                    "value originates from here",
65                    tag,
66                ));
67            }
68        }
69        Ok(vec![])
70    }
71
72    fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
73        crate::from_sqlite::from_sqlite(self.state.clone(), Tag::unknown(), self.tables.clone())
74    }
75}