pub struct Awk { /* private fields */ }Expand description
High-level wrapper for compiling and running an AWK script.
The script is parsed once on construction and can then be executed against any number of independent inputs without re-parsing.
§Examples
Print every line of input unchanged:
use rawk_core::awk::Awk;
let awk = Awk::new("{ print }").unwrap();
let (output, error) = awk.run(vec!["hello world".into()], None, None);
assert_eq!(output, vec!["hello world".to_string()]);
assert!(error.is_none());Supply a filename so that the FILENAME built-in variable is populated:
use rawk_core::awk::Awk;
let awk = Awk::new("{ print FILENAME }").unwrap();
let (output, error) = awk.run(vec!["ignored".into()], Some("data/input.txt".into()), None);
assert_eq!(output, vec!["data/input.txt".to_string()]);
assert!(error.is_none());Use a custom field separator to parse CSV-style input:
use rawk_core::awk::Awk;
let awk = Awk::new("{ print $1 }").unwrap();
let (output, error) = awk.run(vec!["Alice,30,engineer".into()], None, Some(",".into()));
assert_eq!(output, vec!["Alice".to_string()]);
assert!(error.is_none());Implementations§
Source§impl Awk
impl Awk
Sourcepub fn new(script: impl Into<String>) -> Result<Self, ParseError<'static>>
pub fn new(script: impl Into<String>) -> Result<Self, ParseError<'static>>
Parse an AWK script into an executable program.
The script is stored with a static lifetime to keep the AST valid. Returns a parse error if the script is not valid AWK according to this parser.
Sourcepub fn run(
&self,
input: Vec<String>,
filename: Option<String>,
field_separator: Option<String>,
) -> (Vec<String>, Option<String>)
pub fn run( &self, input: Vec<String>, filename: Option<String>, field_separator: Option<String>, ) -> (Vec<String>, Option<String>)
Execute the compiled program against the given input lines.
filename— exposed as theFILENAMEbuilt-in variable inside the script. PassNoneto use the default value"-"(conventional stdin placeholder).field_separator— overrides theFSbuilt-in variable used to split each input record into fields ($1,$2, …). PassNoneto use the default" ", which splits on runs of whitespace.
Returns (output_lines, runtime_error). When a runtime error occurs, output
collected before the error is still returned alongside the error message.
Auto Trait Implementations§
impl Freeze for Awk
impl RefUnwindSafe for Awk
impl Send for Awk
impl Sync for Awk
impl Unpin for Awk
impl UnsafeUnpin for Awk
impl UnwindSafe for Awk
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more