Clap raw bytes arguments - rust

I would like to use raw byte argument with clap
For example --raw $(echo -n -e 'B\x10CC\x01\xff') will give me the following bytes array [66, 16, 67, 67, 1, 239, 191, 189] (using to_string_lossy().to_bytes()).
Is there a way to get exact bytes array using clap?
EDIT
let cmd = Command::new(
env!("CARGO_CRATE_NAME")
).bin_name(
env!("CARGO_CRATE_NAME")
).arg(
Arg::new("raw").long("raw").takes_value(true).allow_invalid_utf8(true)
);
let matches = cmd.get_matches();
match matches.value_of_os("raw") {
Some(s) => {
match s.to_str() {
Some(s3) => {
let v2: &[u8] = s3.as_bytes();
println!("OsStr(bytes):{:?}", v2);
},
None => {},
}
let s2 = s.to_string_lossy();
println!("string_from_OsString:{}", s2);
let v3: &[u8] = s2.as_bytes();
println!("OsString.to_lossy(bytes):{:?}", v3);
},
None => {},
}
return for input --raw $(echo -n -e 'B\x10CC\x01\xff')
string_from_OsString:BCC�
OsString.to_lossy(bytes):[66, 16, 67, 67, 1, 239, 191, 189]
Thank you

clap is platform agnostic and therefore uses abstractions like OsString (which is the type of your s variable).
There seems to be no generic as_bytes() method attached to OsString, because not on every operating system OsString is actually a raw bytes array.
Here is a lot more discussion about this very topic: How can I convert OsStr to &[u8]/Vec<u8> on Windows?
So to solve your problem, it seems necessary that you narrow your compatibility down to a specific operating system. In your case, it seems that you are using Unix. Which is great, because for Unix, such a method does exist!
Here you go:
use clap::{Arg, Command};
use std::os::unix::ffi::OsStrExt;
fn main() {
let cmd = Command::new(env!("CARGO_CRATE_NAME"))
.bin_name(env!("CARGO_CRATE_NAME"))
.arg(
Arg::new("raw")
.long("raw")
.takes_value(true)
.allow_invalid_utf8(true),
);
let matches = cmd.get_matches();
match matches.value_of_os("raw") {
Some(s) => {
println!(".as_bytes(): {:?}", s.as_bytes());
}
None => {}
}
}
.as_bytes(): [66, 16, 67, 67, 1, 255]
Note that the use std::os::unix::ffi::OsStrExt; will add the .as_bytes() functionality to OsString, but will fail to compile on non-unix systems.

Related

How to use values from an array in matching ranges of values with ..= in rust?

I'm learning rust and I found something I can't just find in google.
I was experimenting with match and I wanted to use values from an array with the ..= syntax.
I know I'm doing something wrong, but I only know Js and Python and I feel I'm missing something basic that it's just known but not explained.
pub fn match_statement() {
println!("Match statement----------------------------------");
let mut country_code=0;
let country_codes_range: [i64; 4] = [1,999,50,66];
let country = match country_code {
34 => "Spain",
46 => "Sweden",
country_codes_range[0]..=country_codes_range[1] => "unknown",
_ => "invalid",
};
country_code=invalid_country;
println!(
"The {} country code is {} because is out of the range [{},{}]",
country, invalid_country, country_codes_range[0], country_codes_range[1]
);
}
the error I get is:
expected one of =>, #, if, or |, found [
on the line
country_codes_range[0]..=country_codes_range[1] => "unknown"
I don't know if the issue lies in my calling of items of the array, an incorrect use of ..= or another thing
Also, I guess I would get a similar error if I used a tuple instead of an array?
Thanks for your help
Rust needs to know the "values" of each match arm at compile time, so what you're describing isn't possible, instead you'll get an error saying runtime values cannot be references in patterns.
If you know what country_codes_range will be at compile time, you can make it available at compile time using const:
fn match_statement() {
let country_code = 123;
const COUNTRY_CODES_RANGE: [i64; 4] = [1, 999, 50, 66];
const FIRST: i64 = COUNTRY_CODES_RANGE[0];
const SECOND: i64 = COUNTRY_CODES_RANGE[1];
let country = match country_code {
34 => "spain",
46 => "sweden",
FIRST..=SECOND => "unknown",
_ => "invalid",
};
// ...
}
Note, the intermediate consts FIRST and SECOND are needed because currently Rust's parser doesn't support the a[i] syntax in patterns, though that is a separate problem to having a match use runtime values

wgpu WGSL compute shader does not appear to be doing anything

I'm trying to follow along the "hello compute" example from wgpu on Windows 10 (with some minor modifications, mainly gutting the shader so it does basically no actual computing), but when I read the buffer at the end, it's always zeroed out.
This is the shader I'm trying to run, it compiles fine and I think it's correct
[[block]]
struct Numbers
{
data: [[stride(4)]] array<u32>;
};
[[group(0), binding(0)]]
var<storage, read_write> numbers: Numbers;
[[stage(compute), workgroup_size(1)]]
fn main()
{
numbers.data[0] = numbers.data[0] + u32(1);
numbers.data[1] = numbers.data[1] + u32(1);
numbers.data[2] = numbers.data[2] + u32(1);
}
As for the wgpu code, it follows the tutorial quite closely:
I get the instance, device, and queue
let instance = Instance::new(Backends::PRIMARY);
let adapter = block_on(instance
.request_adapter(&RequestAdapterOptions
{
power_preference: PowerPreference::default(),
compatible_surface: None,
}))
.unwrap();
let (device, queue) = block_on(adapter
.request_device(&Default::default(), None))
.unwrap();
Compile the shader and make a pipeline:
let shader = device.create_shader_module(&ShaderModuleDescriptor
{
label: Some("shader"),
source: ShaderSource::Wgsl(shader_src.into()),
});
let pipeline = device.create_compute_pipeline(&ComputePipelineDescriptor
{
label: None,
layout: None,
module: &shader,
entry_point: "main",
});
Make the staging and storage buffer. The dbg!(size) prints 12, which should be correct for a 3-length array for 4-byte u32s.
let buffer = [1u32, 2, 3];
let size = std::mem::size_of_val(&buffer) as u64;
dbg!(size);
let staging_buffer = device.create_buffer(&BufferDescriptor
{
label: None,
size: size,
usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let storage_buffer = device.create_buffer_init(&BufferInitDescriptor
{
label: Some("storage buffer"),
contents: cast_slice(&buffer),
usage: BufferUsages::STORAGE
| BufferUsages::COPY_DST
| BufferUsages::COPY_SRC,
});
set up the bind group:
let bg_layout = pipeline.get_bind_group_layout(0);
let bind_group = device.create_bind_group(&BindGroupDescriptor
{
label: None,
layout: &bg_layout,
entries: &[BindGroupEntry
{
binding: 0,
resource: storage_buffer.as_entire_binding(),
}]
});
Get the encoder and create the compute pass. The copy_buffer_to_buffer should copy the storage buffer to the staging buffer so I can read it at the end.
let mut encoder = device.create_command_encoder(&CommandEncoderDescriptor
{
label: None,
});
{
let mut cpass = encoder.begin_compute_pass(&ComputePassDescriptor
{
label: None
});
cpass.set_pipeline(&pipeline);
cpass.set_bind_group(0, &bind_group, &[]);
cpass.dispatch(1, 1, 1);
}
encoder.copy_buffer_to_buffer(
&storage_buffer, 0,
&staging_buffer, 0,
size);
queue.submit(Some(encoder.finish()));
And then submit the compute pass and block for the result:
let buf_slice = staging_buffer.slice(..);
let buf_future = buf_slice.map_async(MapMode::Read);
device.poll(Maintain::Wait);
if let Ok(()) = block_on(buf_future)
{
let data = buf_slice.get_mapped_range();
let result = cast_slice::<u8, u32>(&data).to_vec();
drop(data);
staging_buffer.unmap();
println!("{:?}", result);
}
else
{
println!("error");
}
The error case isn't reached, and the program terminates with no errors, but the result is always printed [0, 0 ,0], when it should be [2, 3, 4].
What am I doing wrong?
The program works fine when I'm running it on my discrete graphics card, but wgpu is bugged on my integrated Intel HD Graphics 630, which is why the program appeared not to work.

Match only valid UTF-8 characters

I'm writing an ncurses app with Rust.
When the user inputs a valid UTF-8 char (like ć, or some Asian letters), I want to build up a search string from it and print it to screen. Currently I have this:
use ncurses::*;
fn main() {
...
let mut search_string = String::new();
...
loop {
let user_input = getch();
match user_input {
27 => break,
KEY_UP => { ... },
KEY_DOWN => { ... },
KEY_BACKSPACE => { ... },
_ => {
search_string += &std::char::from_u32(user_input as u32).expect("Invalid char.").to_string();
mvaddstr(0, 0, &search_string);
app::autosearch();
}
}
}
}
However, this catches all other keys, such as F5, KEY_LEFT, etc.
How can I match only valid UTF-8 letters?
If getch gives you a u8, you could collect subsequent key presses into a Vec<u8> and then call e.g. from_utf8 on each getch, handling the error as appropriate (see Utf8Error for more info).
In C, you could call get_wch() instead of getch() -- it returns KEY_CODE_YES for KEY_* codes, while the actual key is stored to an address passed as a parameter. But I don't know how this translates to Rust.

Idiomatic rust way to properly parse Clap ArgMatches

I'm learning rust and trying to make a find like utility (yes another one), im using clap and trying to support command line and config file for the program's parameters(this has nothing to do with the clap yml file).
Im trying to parse the commands and if no commands were passed to the app, i will try to load them from a config file.
Now I don't know how to do this in an idiomatic way.
fn main() {
let matches = App::new("findx")
.version(crate_version!())
.author(crate_authors!())
.about("find + directory operations utility")
.arg(
Arg::with_name("paths")
...
)
.arg(
Arg::with_name("patterns")
...
)
.arg(
Arg::with_name("operation")
...
)
.get_matches();
let paths;
let patterns;
let operation;
if matches.is_present("patterns") && matches.is_present("operation") {
patterns = matches.values_of("patterns").unwrap().collect();
paths = matches.values_of("paths").unwrap_or(clap::Values<&str>{"./"}).collect(); // this doesn't work
operation = match matches.value_of("operation").unwrap() { // I dont like this
"Append" => Operation::Append,
"Prepend" => Operation::Prepend,
"Rename" => Operation::Rename,
_ => {
print!("Operation unsupported");
process::exit(1);
}
};
}else if Path::new("findx.yml").is_file(){
//TODO: try load from config file
}else{
eprintln!("Command line parameters or findx.yml file must be provided");
process::exit(1);
}
if let Err(e) = findx::run(Config {
paths: paths,
patterns: patterns,
operation: operation,
}) {
eprintln!("Application error: {}", e);
process::exit(1);
}
}
There is an idiomatic way to extract Option and Result types values to the same scope, i mean all examples that i have read, uses match or if let Some(x) to consume the x value inside the scope of the pattern matching, but I need to assign the value to a variable.
Can someone help me with this, or point me to the right direction?
Best Regards
Personally I see nothing wrong with using the match statements and folding it or placing it in another function. But if you want to remove it there are many options.
There is the ability to use the .default_value_if() method which is impl for clap::Arg and have a different default value depending on which match arm is matched.
From the clap documentation
//sets value of arg "other" to "default" if value of "--opt" is "special"
let m = App::new("prog")
.arg(Arg::with_name("opt")
.takes_value(true)
.long("opt"))
.arg(Arg::with_name("other")
.long("other")
.default_value_if("opt", Some("special"), "default"))
.get_matches_from(vec![
"prog", "--opt", "special"
]);
assert_eq!(m.value_of("other"), Some("default"));
In addition you can add a validator to your operation OR convert your valid operation values into flags.
Here's an example converting your match arm values into individual flags (smaller example for clarity).
extern crate clap;
use clap::{Arg,App};
fn command_line_interface<'a>() -> clap::ArgMatches<'a> {
//Sets the command line interface of the program.
App::new("something")
.version("0.1")
.arg(Arg::with_name("rename")
.help("renames something")
.short("r")
.long("rename"))
.arg(Arg::with_name("prepend")
.help("prepends something")
.short("p")
.long("prepend"))
.arg(Arg::with_name("append")
.help("appends something")
.short("a")
.long("append"))
.get_matches()
}
#[derive(Debug)]
enum Operation {
Rename,
Append,
Prepend,
}
fn main() {
let matches = command_line_interface();
let operation = if matches.is_present("rename") {
Operation::Rename
} else if matches.is_present("prepend"){
Operation::Prepend
} else {
//DEFAULT
Operation::Append
};
println!("Value of operation is {:?}",operation);
}
I hope this helps!
EDIT:
You can also use Subcommands with your specific operations. It all depends on what you want to interface to be like.
let app_m = App::new("git")
.subcommand(SubCommand::with_name("clone"))
.subcommand(SubCommand::with_name("push"))
.subcommand(SubCommand::with_name("commit"))
.get_matches();
match app_m.subcommand() {
("clone", Some(sub_m)) => {}, // clone was used
("push", Some(sub_m)) => {}, // push was used
("commit", Some(sub_m)) => {}, // commit was used
_ => {}, // Either no subcommand or one not tested for...
}

How to implement a take_until_and_consume-like parser combinator that does not skip the tag?

I would like to write a nom parser combinator that takes as many bytes up to and including a tag sequence. I tried using take_until_and_consume!, but I found that the generated parser combinator discards the tag sequence:
#[macro_use]
extern crate nom;
named!(up_to_and_including_backslash, take_until_and_consume!("\\"));
fn main() {
let res = up_to_and_including_backslash(b" \\");
println!("{:?}", res);
}
Results in:
Done([], [32, 32, 32, 32])
What I would like is for the tag sequence (in this case, the backslash character) to be included in the result:
Done([], [32, 32, 32, 32, 92])
How can I accomplish this?
update:
You want to use recognize! on take_until_and_consume!("\\") to add everything it consumed to the Output.
You would write your parser function like this:
#[macro_use]
extern crate nom;
named!(up_to_and_including_backslash, recognize!( take_until_and_consume!("\\") ));
fn main() {
let res = up_to_and_including_backslash(b" \\");
println!("{:?}", res);
}
In case you needed to include the consumed symbols of multiple parsers to your Output you could put them all inside a do_parse! within recognize!. Like so:
recognize!( do_parse!( tag!("\\") >> take_until_and_consume!("\\") >> take!(4) >> () ) )
old:
The only way I got this to work was this ugly abomination.
named!(up_to_and_including_backslash,
do_parse!(
line: take_until_and_consume!("\\") >>
(
{
let mut complete_line:Vec<u8> = line.to_vec();
complete_line.extend_from_slice(b"\\");
&*complete_line
}
)
)
);
Right now you are using the method take_until_and_consume whose Documentation says:
generates a parser consuming bytes until the specified byte sequence
is found, and consumes it
The consume part is important, since it is what you want to avoid.
You could do something akin to this:
named!(up_to_and_including_backslash,
do_parse!(
line: take_until!("\\") >> char!('\\') >>
(line)
)
);
Which should return the line with your seperator.

Resources