|
| 1 | +use super::{operate, BytesArgument}; |
| 2 | +use nu_engine::CallExt; |
| 3 | +use nu_protocol::ast::Call; |
| 4 | +use nu_protocol::ast::CellPath; |
| 5 | +use nu_protocol::engine::{Command, EngineState, Stack}; |
| 6 | +use nu_protocol::Category; |
| 7 | +use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value}; |
| 8 | + |
| 9 | +struct Arguments { |
| 10 | + pattern: Vec<u8>, |
| 11 | + column_paths: Option<Vec<CellPath>>, |
| 12 | +} |
| 13 | + |
| 14 | +impl BytesArgument for Arguments { |
| 15 | + fn take_column_paths(&mut self) -> Option<Vec<CellPath>> { |
| 16 | + self.column_paths.take() |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Clone)] |
| 21 | + |
| 22 | +pub struct BytesStartsWith; |
| 23 | + |
| 24 | +impl Command for BytesStartsWith { |
| 25 | + fn name(&self) -> &str { |
| 26 | + "bytes starts-with" |
| 27 | + } |
| 28 | + |
| 29 | + fn signature(&self) -> Signature { |
| 30 | + Signature::build("bytes starts-with") |
| 31 | + .required("pattern", SyntaxShape::Binary, "the pattern to match") |
| 32 | + .rest( |
| 33 | + "rest", |
| 34 | + SyntaxShape::CellPath, |
| 35 | + "optionally matches prefix of text by column paths", |
| 36 | + ) |
| 37 | + .category(Category::Bytes) |
| 38 | + } |
| 39 | + |
| 40 | + fn usage(&self) -> &str { |
| 41 | + "Check if bytes starts with a pattern" |
| 42 | + } |
| 43 | + |
| 44 | + fn search_terms(&self) -> Vec<&str> { |
| 45 | + vec!["pattern", "match", "find", "search"] |
| 46 | + } |
| 47 | + |
| 48 | + fn run( |
| 49 | + &self, |
| 50 | + engine_state: &EngineState, |
| 51 | + stack: &mut Stack, |
| 52 | + call: &Call, |
| 53 | + input: PipelineData, |
| 54 | + ) -> Result<PipelineData, ShellError> { |
| 55 | + let pattern: Vec<u8> = call.req(engine_state, stack, 0)?; |
| 56 | + let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?; |
| 57 | + let column_paths = if column_paths.is_empty() { |
| 58 | + None |
| 59 | + } else { |
| 60 | + Some(column_paths) |
| 61 | + }; |
| 62 | + let arg = Arguments { |
| 63 | + pattern, |
| 64 | + column_paths, |
| 65 | + }; |
| 66 | + operate( |
| 67 | + starts_with, |
| 68 | + arg, |
| 69 | + input, |
| 70 | + call.head, |
| 71 | + engine_state.ctrlc.clone(), |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + fn examples(&self) -> Vec<Example> { |
| 76 | + vec![ |
| 77 | + Example { |
| 78 | + description: "Checks if binary starts with `0x[1F FF AA]`", |
| 79 | + example: "0x[1F FF AA AA] | bytes starts-with 0x[1F FF AA]", |
| 80 | + result: Some(Value::Bool { |
| 81 | + val: true, |
| 82 | + span: Span::test_data(), |
| 83 | + }), |
| 84 | + }, |
| 85 | + Example { |
| 86 | + description: "Checks if binary starts with `0x[1F]`", |
| 87 | + example: "0x[1F FF AA AA] | bytes starts-with 0x[1F]", |
| 88 | + result: Some(Value::Bool { |
| 89 | + val: true, |
| 90 | + span: Span::test_data(), |
| 91 | + }), |
| 92 | + }, |
| 93 | + Example { |
| 94 | + description: "Checks if binary starts with `0x[1F]`", |
| 95 | + example: "0x[1F FF AA AA] | bytes starts-with 0x[11]", |
| 96 | + result: Some(Value::Bool { |
| 97 | + val: false, |
| 98 | + span: Span::test_data(), |
| 99 | + }), |
| 100 | + }, |
| 101 | + ] |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fn starts_with(input: &[u8], Arguments { pattern, .. }: &Arguments, span: Span) -> Value { |
| 106 | + Value::Bool { |
| 107 | + val: input.starts_with(pattern), |
| 108 | + span, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use super::*; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_examples() { |
| 118 | + use crate::test_examples; |
| 119 | + |
| 120 | + test_examples(BytesStartsWith {}) |
| 121 | + } |
| 122 | +} |
0 commit comments