Commit 41ff9ecb authored by Michael Aaron Murphy's avatar Michael Aaron Murphy
Browse files

Public items now have crate-level visibility

This is the next step towards using Ion as a library for an upcoming project.
parent a61f0ec9
/// Given an valid assignment expression, this will split it into `keys`, `operator`, `values`.
pub fn split_assignment<'a>(
pub(crate) fn split_assignment<'a>(
statement: &'a str,
) -> (Option<&'a str>, Option<&'a str>, Option<&'a str>) {
let statement = statement.trim();
......
......@@ -2,14 +2,14 @@ use parser::{expand_string, Expander};
use types::Value;
#[derive(Debug, PartialEq)]
pub enum ForExpression {
pub(crate) enum ForExpression {
Multiple(Vec<Value>),
Normal(Value),
Range(usize, usize),
}
impl ForExpression {
pub fn new<E: Expander>(expression: &[String], expanders: &E) -> ForExpression {
pub(crate) fn new<E: Expander>(expression: &[String], expanders: &E) -> ForExpression {
let output: Vec<_> = expression
.iter()
.flat_map(|expression| expand_string(expression, expanders, true))
......
......@@ -6,8 +6,8 @@ pub mod shell_expand;
mod statement;
mod quotes;
pub use self::arguments::ArgumentSplitter;
pub use self::loops::for_grammar::ForExpression;
pub use self::quotes::QuoteTerminator;
pub use self::shell_expand::{expand_string, expand_tokens, Expander, Index, Range, Select, WordIterator, WordToken};
pub use self::statement::{parse_and_validate, StatementError, StatementSplitter};
pub(crate) use self::arguments::ArgumentSplitter;
pub(crate) use self::loops::for_grammar::ForExpression;
pub(crate) use self::quotes::QuoteTerminator;
pub(crate) use self::shell_expand::{expand_string, Expander, Select};
pub(crate) use self::statement::{parse_and_validate, StatementSplitter};
......@@ -7,7 +7,7 @@ use super::{Input, Pipeline, RedirectFrom, Redirection};
use shell::{Job, JobKind};
use types::*;
pub struct Collector<'a> {
pub(crate) struct Collector<'a> {
data: &'a str,
}
......@@ -17,9 +17,9 @@ lazy_static! {
}
impl<'a> Collector<'a> {
pub fn new(data: &'a str) -> Self { Collector { data } }
pub(crate) fn new(data: &'a str) -> Self { Collector { data } }
pub fn run(data: &'a str) -> Result<Pipeline, &'static str> { Collector::new(data).parse() }
pub(crate) fn run(data: &'a str) -> Result<Pipeline, &'static str> { Collector::new(data).parse() }
fn peek(&self, index: usize) -> Option<u8> {
if index < self.data.len() {
......@@ -195,7 +195,7 @@ impl<'a> Collector<'a> {
}
}
pub fn parse(&self) -> Result<Pipeline, &'static str> {
pub(crate) fn parse(&self) -> Result<Pipeline, &'static str> {
let mut bytes = self.data.bytes().enumerate().peekable();
let mut args = Array::new();
let mut jobs: Vec<Job> = Vec::new();
......
mod collector;
pub use self::collector::*;
pub(crate) use self::collector::*;
use super::{expand_string, Expander};
use shell::{Job, JobKind};
use std::fmt;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum RedirectFrom {
pub(crate) enum RedirectFrom {
Stdout,
Stderr,
Both,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Redirection {
pub(crate) struct Redirection {
pub from: RedirectFrom,
pub file: String,
pub append: bool,
......@@ -22,7 +22,7 @@ pub struct Redirection {
/// Represents input that a process could initially receive from `stdin`
#[derive(Debug, PartialEq, Clone)]
pub enum Input {
pub(crate) enum Input {
/// A file; the contents of said file will be written to the `stdin` of a process
File(String),
/// A string literal that is written to the `stdin` of a process.
......@@ -31,14 +31,14 @@ pub enum Input {
}
#[derive(Debug, PartialEq, Clone)]
pub struct Pipeline {
pub(crate) struct Pipeline {
pub jobs: Vec<Job>,
pub stdout: Option<Redirection>,
pub stdin: Option<Input>,
}
impl Pipeline {
pub fn new(jobs: Vec<Job>, stdin: Option<Input>, stdout: Option<Redirection>) -> Self {
pub(crate) fn new(jobs: Vec<Job>, stdin: Option<Input>, stdout: Option<Redirection>) -> Self {
Pipeline {
jobs,
stdin,
......@@ -46,7 +46,7 @@ impl Pipeline {
}
}
pub fn expand<E: Expander>(&mut self, expanders: &E) {
pub(crate) fn expand<E: Expander>(&mut self, expanders: &E) {
for job in &mut self.jobs {
job.expand(expanders);
}
......@@ -68,7 +68,7 @@ impl Pipeline {
}
}
pub fn requires_piping(&self) -> bool {
pub(crate) fn requires_piping(&self) -> bool {
self.jobs.len() > 1 || self.stdin != None || self.stdout != None ||
self.jobs.last().unwrap().kind == JobKind::Background
}
......
......@@ -7,7 +7,7 @@ bitflags! {
}
}
pub struct QuoteTerminator {
pub(crate) struct QuoteTerminator {
buffer: String,
eof: Option<String>,
eof_buffer: String,
......@@ -16,7 +16,7 @@ pub struct QuoteTerminator {
}
impl QuoteTerminator {
pub fn new(input: String) -> QuoteTerminator {
pub(crate) fn new(input: String) -> QuoteTerminator {
QuoteTerminator {
buffer: input,
eof: None,
......@@ -26,7 +26,7 @@ impl QuoteTerminator {
}
}
pub fn append(&mut self, input: String) {
pub(crate) fn append(&mut self, input: String) {
if self.eof.is_none() {
self.buffer.push_str(if self.flags.contains(TRIM) { input.trim() } else { &input });
} else {
......@@ -34,7 +34,7 @@ impl QuoteTerminator {
}
}
pub fn check_termination(&mut self) -> bool {
pub(crate) fn check_termination(&mut self) -> bool {
let mut eof_line = None;
let eof = self.eof.clone();
let status = if let Some(ref eof) = eof {
......@@ -119,5 +119,5 @@ impl QuoteTerminator {
status
}
pub fn consume(self) -> String { self.buffer }
pub(crate) fn consume(self) -> String { self.buffer }
}
......@@ -2,12 +2,12 @@ use super::permutate::Permutator;
#[derive(Debug)]
/// A token primitive for the `expand_braces` function.
pub enum BraceToken {
pub(crate) enum BraceToken {
Normal(String),
Expander,
}
pub fn expand_braces(tokens: &[BraceToken], mut expanders: Vec<Vec<String>>) -> Vec<String> {
pub(crate) fn expand_braces(tokens: &[BraceToken], mut expanders: Vec<Vec<String>>) -> Vec<String> {
if expanders.len() > 1 {
let tmp: Vec<Vec<&str>> = expanders
.iter()
......
......@@ -10,20 +10,20 @@ mod ranges;
mod words;
use self::braces::BraceToken;
use self::ranges::parse_range;
pub use self::words::{Index, Range, Select, WordIterator, WordToken};
pub(crate) use self::words::{Index, Range, Select, WordIterator, WordToken};
use glob::glob;
use types::*;
/// Determines whether an input string is expression-like as compared to a
/// bare word. For example, strings starting with '"', '\'', '@', or '$' are
/// all expressions
pub fn is_expression(s: &str) -> bool {
pub(crate) fn is_expression(s: &str) -> bool {
s.starts_with('@') || s.starts_with('[') || s.starts_with('$') || s.starts_with('"') ||
s.starts_with('\'')
}
/// Trait representing different elements of string expansion
pub trait Expander {
pub(crate) trait Expander {
/// Expand a tilde form to the correct directory
fn tilde(&self, &str) -> Option<String> { None }
/// Expand an array variable with some selection
......@@ -145,7 +145,7 @@ fn slice<S: AsRef<str>>(output: &mut String, expanded: S, selection: Select) {
/// Performs shell expansions to an input string, efficiently returning the final
/// expanded form. Shells must provide their own batteries for expanding tilde
/// and variable words.
pub fn expand_string<E: Expander>(original: &str, expand_func: &E, reverse_quoting: bool) -> Array {
pub(crate) fn expand_string<E: Expander>(original: &str, expand_func: &E, reverse_quoting: bool) -> Array {
let mut token_buffer = Vec::new();
let mut contains_brace = false;
......@@ -160,7 +160,7 @@ pub fn expand_string<E: Expander>(original: &str, expand_func: &E, reverse_quoti
}
#[allow(cyclomatic_complexity)]
pub fn expand_tokens<E: Expander>(
pub(crate) fn expand_tokens<E: Expander>(
token_buffer: &[WordToken],
expand_func: &E,
reverse_quoting: bool,
......
......@@ -117,7 +117,7 @@ fn strings_to_isizes(a: &str, b: &str) -> Option<(isize, isize)> {
// Inclusive nonstepped: {start...end}
// Exclusive stepped: {start..step..end}
// Inclusive stepped: {start..step...end}
pub fn parse_range(input: &str) -> Option<Vec<String>> {
pub(crate) fn parse_range(input: &str) -> Option<Vec<String>> {
let mut read = 0;
let mut bytes_iterator = input.bytes();
while let Some(byte) = bytes_iterator.next() {
......@@ -223,7 +223,7 @@ pub fn parse_range(input: &str) -> Option<Vec<String>> {
None
}
pub fn parse_index_range(input: &str) -> Option<Range> {
pub(crate) fn parse_index_range(input: &str) -> Option<Range> {
let mut bytes_iterator = input.bytes().enumerate();
while let Some((id, byte)) = bytes_iterator.next() {
match byte {
......
......@@ -36,7 +36,7 @@ bitflags! {
/// Index into a vector-like object
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Index {
pub(crate) enum Index {
/// Index starting from the beginning of the vector, where `Forward(0)`
/// is the first element
Forward(usize),
......@@ -52,7 +52,7 @@ impl Index {
/// ```
/// assert_eq!(Index::new(-1), Index::Backward(0))
/// ```
pub fn new(input: isize) -> Index {
pub(crate) fn new(input: isize) -> Index {
if input < 0 {
Index::Backward((input.abs() as usize) - 1)
} else {
......@@ -60,7 +60,7 @@ impl Index {
}
}
pub fn resolve(&self, vector_length: usize) -> Option<usize> {
pub(crate) fn resolve(&self, vector_length: usize) -> Option<usize> {
match *self {
Index::Forward(n) => Some(n),
Index::Backward(n) => if n >= vector_length {
......@@ -74,7 +74,7 @@ impl Index {
/// A range of values in a vector-like object
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct Range {
pub(crate) struct Range {
/// Starting index
start: Index,
/// Ending index
......@@ -85,7 +85,7 @@ pub struct Range {
}
impl Range {
pub fn to(end: Index) -> Range {
pub(crate) fn to(end: Index) -> Range {
Range {
start: Index::new(0),
end,
......@@ -93,7 +93,7 @@ impl Range {
}
}
pub fn from(start: Index) -> Range {
pub(crate) fn from(start: Index) -> Range {
Range {
start,
end: Index::new(-1),
......@@ -101,7 +101,7 @@ impl Range {
}
}
pub fn inclusive(start: Index, end: Index) -> Range {
pub(crate) fn inclusive(start: Index, end: Index) -> Range {
Range {
start,
end,
......@@ -109,7 +109,7 @@ impl Range {
}
}
pub fn exclusive(start: Index, end: Index) -> Range {
pub(crate) fn exclusive(start: Index, end: Index) -> Range {
Range {
start,
end,
......@@ -128,7 +128,7 @@ impl Range {
/// let selection = vec.iter().skip(start).take(size).collect::<Vec<_>>();
/// assert_eq!(expected, selection);
/// ```
pub fn bounds(&self, vector_length: usize) -> Option<(usize, usize)> {
pub(crate) fn bounds(&self, vector_length: usize) -> Option<(usize, usize)> {
if let Some(start) = self.start.resolve(vector_length) {
if let Some(end) = self.end.resolve(vector_length) {
if end < start {
......@@ -148,17 +148,17 @@ impl Range {
}
#[derive(Debug, PartialEq, Clone)]
pub struct Key {
pub(crate) struct Key {
key: ::types::Key,
}
impl Key {
pub fn get(&self) -> &::types::Key { return &self.key; }
pub(crate) fn get(&self) -> &::types::Key { return &self.key; }
}
/// Represents a filter on a vector-like object
#[derive(Debug, PartialEq, Clone)]
pub enum Select {
pub(crate) enum Select {
/// Select no elements
None,
/// Select all elements
......@@ -171,7 +171,7 @@ pub enum Select {
Key(Key),
}
pub trait SelectWithSize {
pub(crate) trait SelectWithSize {
type Item;
fn select<O>(&mut self, Select, usize) -> O
where O: FromIterator<Self::Item>;
......@@ -227,7 +227,7 @@ enum Pattern<'a> {
#[derive(Debug, PartialEq, Clone)]
pub struct ArrayMethod<'a> {
pub(crate) struct ArrayMethod<'a> {
method: &'a str,
variable: &'a str,
pattern: Pattern<'a>,
......@@ -235,14 +235,14 @@ pub struct ArrayMethod<'a> {
}
impl<'a> ArrayMethod<'a> {
pub fn returns_array(&self) -> bool {
pub(crate) fn returns_array(&self) -> bool {
match self.method {
"split" | "chars" | "bytes" | "graphemes" => true,
_ => false,
}
}
pub fn handle<E: Expander>(&self, current: &mut String, expand_func: &E) {
pub(crate) fn handle<E: Expander>(&self, current: &mut String, expand_func: &E) {
match self.method {
"split" => {
let variable = if let Some(variable) = expand_func.variable(self.variable, false) {
......@@ -327,7 +327,7 @@ impl<'a> ArrayMethod<'a> {
}
}
pub fn handle_as_array<E: Expander>(&self, expand_func: &E) -> Array {
pub(crate) fn handle_as_array<E: Expander>(&self, expand_func: &E) -> Array {
macro_rules! resolve_var {
() => {
if let Some(variable) = expand_func.variable(self.variable, false) {
......@@ -440,7 +440,7 @@ impl<'a> ArrayMethod<'a> {
/// Represents a method that operates on and returns a string
#[derive(Debug, PartialEq, Clone)]
pub struct StringMethod<'a> {
pub(crate) struct StringMethod<'a> {
/// Name of this method: currently `join`, `len`, and `len_bytes` are the
/// supported methods
method: &'a str,
......@@ -455,7 +455,7 @@ pub struct StringMethod<'a> {
}
impl<'a> StringMethod<'a> {
pub fn handle<E: Expander>(&self, output: &mut String, expand: &E) {
pub(crate) fn handle<E: Expander>(&self, output: &mut String, expand: &E) {
let (variable, pattern) = (self.variable, self.pattern);
macro_rules! string_eval {
......@@ -634,7 +634,7 @@ impl<'a> StringMethod<'a> {
}
#[derive(Debug, PartialEq, Clone)]
pub enum WordToken<'a> {
pub(crate) enum WordToken<'a> {
/// Represents a normal string who may contain a globbing character
/// (the second element) or a tilde expression (the third element)
Normal(&'a str, bool, bool),
......@@ -651,7 +651,7 @@ pub enum WordToken<'a> {
Arithmetic(&'a str), // Glob(&'a str)
}
pub struct WordIterator<'a, E: Expander + 'a> {
pub(crate) struct WordIterator<'a, E: Expander + 'a> {
data: &'a str,
read: usize,
flags: Flags,
......@@ -659,7 +659,7 @@ pub struct WordIterator<'a, E: Expander + 'a> {
}
impl<'a, E: Expander + 'a> WordIterator<'a, E> {
pub fn new(data: &'a str, expand_processes: bool, expanders: &'a E) -> WordIterator<'a, E> {
pub(crate) fn new(data: &'a str, expand_processes: bool, expanders: &'a E) -> WordIterator<'a, E> {
let flags = if expand_processes { EXPAND_PROCESSES } else { Flags::empty() };
WordIterator {
data,
......
......@@ -2,7 +2,7 @@ use super::super::ArgumentSplitter;
use std::fmt::{self, Display, Formatter};
#[derive(Debug, PartialEq)]
pub enum CaseError<'a> {
pub(crate) enum CaseError<'a> {
NoBindVariable,
NoConditional,
ExtraBind(&'a str),
......@@ -22,7 +22,7 @@ impl<'a> Display for CaseError<'a> {
}
}
pub fn parse_case<'a>(
pub(crate) fn parse_case<'a>(
data: &'a str,
) -> Result<(Option<&'a str>, Option<&'a str>, Option<String>), CaseError<'a>> {
let mut splitter = ArgumentSplitter::new(data);
......
......@@ -4,7 +4,7 @@ use super::super::assignments::{KeyBuf, KeyIterator, TypeError};
/// The arguments expression given to a function declaration goes into here, which will be
/// converted into a tuple consisting of a `KeyIterator` iterator, which will collect type
/// information, and an optional description of the function.
pub fn parse_function<'a>(arg: &'a str) -> (KeyIterator<'a>, Option<&'a str>) {
pub(crate) fn parse_function<'a>(arg: &'a str) -> (KeyIterator<'a>, Option<&'a str>) {
let (args, description) = split_pattern(arg, "--");
(KeyIterator::new(args), description)
}
......@@ -13,7 +13,7 @@ pub fn parse_function<'a>(arg: &'a str) -> (KeyIterator<'a>, Option<&'a str>) {
/// type error is detected, then that error will be returned instead. This is required because
/// of lifetime restrictions on `KeyIterator`, which will not live for the remainder of the
/// declared function's lifetime.
pub fn collect_arguments<'a>(args: KeyIterator<'a>) -> Result<Vec<KeyBuf>, TypeError<'a>> {
pub(crate) fn collect_arguments<'a>(args: KeyIterator<'a>) -> Result<Vec<KeyBuf>, TypeError<'a>> {
// NOTE: Seems to be some kind of issue with Rust's compiler accepting this:
// Ok(args.map(|a| a.map(Into::into)?).collect::<Vec<_>>())
// Seems to think that `a` is a `KeyBuf` when it's actually a `Result<Key, _>`.
......
......@@ -3,12 +3,12 @@ mod functions;
mod parse;
mod splitter;
pub use self::parse::parse;
pub use self::splitter::{StatementError, StatementSplitter};
pub(crate) use self::parse::parse;
pub(crate) use self::splitter::{StatementError, StatementSplitter};
use shell::flow_control::Statement;
/// Parses a given statement string and return's the corresponding mapped `Statement`
pub fn parse_and_validate<'a>(statement: Result<&str, StatementError<'a>>) -> Statement {
pub(crate) fn parse_and_validate<'a>(statement: Result<&str, StatementError<'a>>) -> Statement {
match statement {
Ok(statement) => parse(statement),
Err(err) => {
......@@ -21,7 +21,7 @@ pub fn parse_and_validate<'a>(statement: Result<&str, StatementError<'a>>) -> St
/// Splits a string into two, based on a given pattern. We know that the first string will always
/// exist, but if the pattern is not found, or no string follows the pattern, then the second
/// string will not exist. Useful for splitting the function expression by the "--" pattern.
pub fn split_pattern<'a>(arg: &'a str, pattern: &str) -> (&'a str, Option<&'a str>) {
pub(crate) fn split_pattern<'a>(arg: &'a str, pattern: &str) -> (&'a str, Option<&'a str>) {
match arg.find(pattern) {
Some(pos) => {
let args = &arg[..pos].trim();
......
......@@ -19,7 +19,7 @@ fn collect<F>(arguments: &str, statement: F) -> Statement
fn is_valid_name(name: &str) -> bool { !name.chars().any(|c| !(c.is_alphanumeric() || c == '_')) }
pub fn parse(code: &str) -> Statement {
pub(crate) fn parse(code: &str) -> Statement {
let cmd = code.trim();
match cmd {
"end" => return Statement::End,
......
......@@ -24,7 +24,7 @@ bitflags! {
#[derive(Debug, PartialEq)]
pub enum StatementError<'a> {
pub(crate) enum StatementError<'a> {
IllegalCommandName(&'a str),
InvalidCharacter(char, usize),
UnterminatedSubshell,
......@@ -65,7 +65,7 @@ impl<'a> Display for StatementError<'a> {
}
}
pub struct StatementSplitter<'a> {
pub(crate) struct StatementSplitter<'a> {
data: &'a str,
read: usize,
flags: Flags,
......@@ -77,7 +77,7 @@ pub struct StatementSplitter<'a> {
}
impl<'a> StatementSplitter<'a> {
pub fn new(data: &'a str) -> StatementSplitter<'a> {
pub(crate) fn new(data: &'a str) -> StatementSplitter<'a> {
StatementSplitter {
data: data,
read: 0,
......
......@@ -45,7 +45,7 @@ fn print_arrays(list: &ArrayVariableContext) {
/// Represents: A variable store capable of setting local variables or
/// exporting variables to some global environment
pub trait VariableStore {
pub(crate) trait VariableStore {
/// Set a local variable given a binding
fn local(&mut self, &str) -> i32;
/// Export a variable to the process environment given a binding
......
......@@ -19,7 +19,7 @@ use std::process;
use sys;
use types::*;
pub trait Binary {
pub(crate) trait Binary {
/// Launches the shell, parses arguments, and then diverges into one of the `execution`
/// paths.
fn main(self);
......
......@@ -70,7 +70,7 @@ enum Mode {
#[derive(Debug, PartialEq)]
/// Stores a reprensetation of text formatting data which can be used to get an ANSI color code.
pub struct Colors {
pub(crate) struct Colors {
foreground: Option<Mode>,
background: Option<Mode>,
attributes: Option<Vec<&'static str>>,
......@@ -80,7 +80,7 @@ impl Colors {
/// Parses the given input and returns a structure obtaining the text data needed for proper
/// transformation into ANSI code parameters, which may be obtained by calling the
/// `into_string()` method on the newly-created `Colors` structure.
pub fn collect(input: &str) -> Colors {
pub(crate) fn collect(input: &str) -> Colors {
let mut colors = Colors {
foreground: None,
background: None,
......@@ -179,7 +179,7 @@ impl Colors {
/// Attempts to transform the data in the structure into the corresponding ANSI code
/// representation. It would very ugly to require shell scripters to have to interface
/// with these codes directly.
pub fn into_string(self) -> Option<String> {
pub(crate) fn into_string(self) -> Option<String> {
let mut output = String::from("\x1b[");
let foreground = match self.foreground {
......
......@@ -5,7 +5,7 @@ use liner::{Completer, FilenameCompleter};
/// Performs escaping to an inner `FilenameCompleter` to enable a handful of special cases
/// needed by the shell, such as expanding '~' to a home directory, or adding a backslash
/// when a special character is contained within an expanded filename.
pub struct IonFileCompleter {
pub(crate) struct IonFileCompleter {
/// The completer that this completer is handling.
inner: FilenameCompleter,
/// A pointer to the directory stack in the shell.
......@@ -15,7 +15,7 @@ pub struct IonFileCompleter {
}
impl IonFileCompleter {
pub fn new(
pub(crate) fn new(
path: Option<&str>,
dir_stack: *const DirectoryStack,
vars: *const Variables,
......@@ -142,7 +142,7 @@ fn unescape(input: &str) -> String {
/// A completer that combines suggestions from multiple completers.