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 94 95 96 97 98 99 100 101 102
use indexmap::IndexMap; use nu_errors::ShellError; use nu_protocol::{UntaggedValue, Value}; use std::cell::RefCell; use std::ffi::{OsStr, OsString}; #[derive(Debug, Clone)] pub struct CliOptions { pub config: Option<OsString>, pub stdin: bool, pub scripts: Vec<NuScript>, pub save_history: bool, pub perf: bool, } impl Default for CliOptions { fn default() -> Self { Self::new() } } impl CliOptions { pub fn new() -> Self { Self { config: None, stdin: false, scripts: vec![], save_history: true, perf: false, } } } #[derive(Debug)] pub struct Options { inner: RefCell<IndexMap<String, Value>>, } impl Options { pub fn default() -> Self { Self { inner: RefCell::new(IndexMap::default()), } } pub fn get(&self, key: &str) -> Option<Value> { self.inner.borrow().get(key).map(Clone::clone) } pub fn put(&self, key: &str, value: Value) { self.inner.borrow_mut().insert(key.into(), value); } pub fn shift(&self) { if let Some(Value { value: UntaggedValue::Table(ref mut args), .. }) = self.inner.borrow_mut().get_mut("args") { args.remove(0); } } pub fn swap(&self, other: &Options) { self.inner.swap(&other.inner); } } #[derive(Debug, Clone)] pub struct NuScript { pub filepath: Option<OsString>, pub contents: String, } impl NuScript { pub fn code(content: &str) -> Result<Self, ShellError> { Ok(Self { filepath: None, contents: content.to_string(), }) } pub fn get_code(&self) -> &str { &self.contents } pub fn source_file(path: &OsStr) -> Result<Self, ShellError> { use std::fs::File; use std::io::Read; let path = path.to_os_string(); let mut file = File::open(&path)?; let mut buffer = String::new(); file.read_to_string(&mut buffer)?; Ok(Self { filepath: Some(path), contents: buffer, }) } }