Add getopt(3p) bindings for Rust #91

Closed
emma wants to merge 0 commits from getopt-bindings into main
Owner
No description provided.
emma added the
enhancement
label 2024-04-02 02:42:43 +00:00
emma self-assigned this 2024-04-02 02:42:43 +00:00
emma added 2 commits 2024-04-02 02:42:44 +00:00
emma added 2 commits 2024-04-12 02:34:34 +00:00
emma added 2 commits 2024-04-12 05:52:40 +00:00
emma requested review from trinity 2024-04-12 06:08:31 +00:00
emma requested review from silt 2024-04-12 06:08:38 +00:00
emma added 1 commit 2024-04-12 06:13:28 +00:00
trinity reviewed 2024-04-12 23:08:28 +00:00
Makefile Outdated
@ -51,15 +51,14 @@ install: dist
.PHONY: test
test: build
tests/posix-compat.sh
$(RUSTC) --test src/getopt-rs/lib.rs -o build/test/getopt
Owner

Could we make it so this file is testable the same way the last was?

Could we make it so this file is testable the same way the last was?
Author
Owner

I will see what I can do

I will see what I can do
Author
Owner

See #111

See #111
emma marked this conversation as resolved
emma added 1 commit 2024-04-13 23:15:37 +00:00
emma added 1 commit 2024-04-14 05:43:18 +00:00
emma changed title from Add getopt(3p) bindings for Rust to WIP: Add getopt(3p) bindings for Rust 2024-04-24 21:00:48 +00:00
emma added 2 commits 2024-06-04 21:08:01 +00:00
emma changed title from WIP: Add getopt(3p) bindings for Rust to Add getopt(3p) bindings for Rust 2024-06-04 21:10:14 +00:00
emma added a new dependency 2024-06-04 21:14:48 +00:00
Author
Owner

@trinity could you review this and rewrite the swab.rs option parsing stuff?

@trinity could you review this and rewrite the `swab.rs` option parsing stuff?
emma added 2 commits 2024-06-05 01:17:09 +00:00
emma added 2 commits 2024-06-05 17:56:07 +00:00
Author
Owner

This can be merged once @trinity rewrites the swab(1) option parsing code to use the new API.

This can be merged once @trinity rewrites the `swab(1)` option parsing code to use the new API.
emma added 1 commit 2024-06-06 07:04:02 +00:00
emma added 1 commit 2024-06-23 04:19:27 +00:00
emma added 1 commit 2024-06-23 04:31:24 +00:00
emma added 2 commits 2024-06-23 07:01:39 +00:00
Owner

Could we name the file in the heirarchy libgetopt.rs to make it abundantly clear it's not a utility? Or (this may be not practically achievable) make this a utility that also functions as a library?

Could we name the file in the heirarchy `libgetopt.rs` to make it abundantly clear it's not a utility? Or (this may be not practically achievable) make this a utility that also functions as a library?
Author
Owner

The resulting rlib file will be called liblibgetopt.rlib in this case

The resulting rlib file will be called `liblibgetopt.rlib` in this case
Author
Owner

I was about to follow-up with the fact that -o would fix this behavior but then I remembered we already do in fact use it already. I will rename the source file, should I rename strerror.rs to libstrerror.rs too?

I was about to follow-up with the fact that `-o` would fix this behavior but then I remembered we already do in fact use it already. I will rename the source file, should I rename strerror.rs to libstrerror.rs too?
emma added 1 commit 2024-06-27 20:05:41 +00:00
Owner

I was about to follow-up with the fact that -o would fix this behavior but then I remembered we already do in fact use it already. I will rename the source file, should I rename strerror.rs to libstrerror.rs too?

That would be very welcome.

> I was about to follow-up with the fact that `-o` would fix this behavior but then I remembered we already do in fact use it already. I will rename the source file, should I rename strerror.rs to libstrerror.rs too? > That would be very welcome.
Owner

I'm getting a build error on libgetopt:

$ make build/o/libgetopt.rlib             
rustc  --crate-type=lib --crate-name=getopt  -o build/o/libgetopt.rlib src/libgetopt.rs
error[E0308]: mismatched types
  --> src/libgetopt.rs:98:41
   |
98 |             let ret = match getopt(len, argv, opts) { 
   |                             ------      ^^^^ expected `*const *mut u8`, found `*const *mut i8`
   |                             |
   |                             arguments to this function are incorrect
   |
   = note: expected raw pointer `*const *mut u8`
              found raw pointer `*const *mut i8`
note: function defined here
  --> src/libgetopt.rs:28:5
   |
28 |     fn getopt(
   |        ^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
*** Error 1 in /home/trinity/harakit (Makefile:66 'build/o/libgetopt.rlib')

Here are my versions on everything:

$ rustc --version
rustc 1.76.0 (07dca489a 2024-02-04) (built from a source tarball)
$ clang --version
OpenBSD clang version 16.0.6
Target: aarch64-unknown-openbsd7.5
Thread model: posix
InstalledDir: /usr/bin
$ uname -a
OpenBSD laika.my.domain 7.5 GENERIC#131 arm64

And here's my git status:

$ git status
On branch getopt-bindings
Your branch is up to date with 'origin/getopt-bindings'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/swab.rs

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .helix/

no changes added to commit (use "git add" and/or "git commit -a")
I'm getting a build error on `libgetopt`: ``` $ make build/o/libgetopt.rlib rustc --crate-type=lib --crate-name=getopt -o build/o/libgetopt.rlib src/libgetopt.rs error[E0308]: mismatched types --> src/libgetopt.rs:98:41 | 98 | let ret = match getopt(len, argv, opts) { | ------ ^^^^ expected `*const *mut u8`, found `*const *mut i8` | | | arguments to this function are incorrect | = note: expected raw pointer `*const *mut u8` found raw pointer `*const *mut i8` note: function defined here --> src/libgetopt.rs:28:5 | 28 | fn getopt( | ^^^^^^ error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0308`. *** Error 1 in /home/trinity/harakit (Makefile:66 'build/o/libgetopt.rlib') ``` Here are my versions on everything: ``` $ rustc --version rustc 1.76.0 (07dca489a 2024-02-04) (built from a source tarball) $ clang --version OpenBSD clang version 16.0.6 Target: aarch64-unknown-openbsd7.5 Thread model: posix InstalledDir: /usr/bin $ uname -a OpenBSD laika.my.domain 7.5 GENERIC#131 arm64 ``` And here's my git status: ``` $ git status On branch getopt-bindings Your branch is up to date with 'origin/getopt-bindings'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: src/swab.rs Untracked files: (use "git add <file>..." to include in what will be committed) .helix/ no changes added to commit (use "git add" and/or "git commit -a") ```
trinity added 1 commit 2024-06-28 14:33:48 +00:00
Owner

I committed my changes to swab(1) as I believe they will work, though I'm unable to test them at the moment.

I committed my changes to swab(1) as I believe they will work, though I'm unable to test them at the moment.
Author
Owner

I'm getting a build error on libgetopt:

$ make build/o/libgetopt.rlib             
rustc  --crate-type=lib --crate-name=getopt  -o build/o/libgetopt.rlib src/libgetopt.rs
error[E0308]: mismatched types
  --> src/libgetopt.rs:98:41
   |
98 |             let ret = match getopt(len, argv, opts) { 
   |                             ------      ^^^^ expected `*const *mut u8`, found `*const *mut i8`
   |                             |
   |                             arguments to this function are incorrect
   |
   = note: expected raw pointer `*const *mut u8`
              found raw pointer `*const *mut i8`
note: function defined here
  --> src/libgetopt.rs:28:5
   |
28 |     fn getopt(
   |        ^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
*** Error 1 in /home/trinity/harakit (Makefile:66 'build/o/libgetopt.rlib')

Here are my versions on everything:

$ rustc --version
rustc 1.76.0 (07dca489a 2024-02-04) (built from a source tarball)
$ clang --version
OpenBSD clang version 16.0.6
Target: aarch64-unknown-openbsd7.5
Thread model: posix
InstalledDir: /usr/bin
$ uname -a
OpenBSD laika.my.domain 7.5 GENERIC#131 arm64

And here's my git status:

$ git status
On branch getopt-bindings
Your branch is up to date with 'origin/getopt-bindings'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   src/swab.rs

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .helix/

no changes added to commit (use "git add" and/or "git commit -a")

This occurs because I was casting a Box to an i8 because on amd64 it is equivalent to a c_char but on aarch64 a c_char is an u8. Making changes now.

> I'm getting a build error on `libgetopt`: > ``` > $ make build/o/libgetopt.rlib > rustc --crate-type=lib --crate-name=getopt -o build/o/libgetopt.rlib src/libgetopt.rs > error[E0308]: mismatched types > --> src/libgetopt.rs:98:41 > | > 98 | let ret = match getopt(len, argv, opts) { > | ------ ^^^^ expected `*const *mut u8`, found `*const *mut i8` > | | > | arguments to this function are incorrect > | > = note: expected raw pointer `*const *mut u8` > found raw pointer `*const *mut i8` > note: function defined here > --> src/libgetopt.rs:28:5 > | > 28 | fn getopt( > | ^^^^^^ > > error: aborting due to 1 previous error > > For more information about this error, try `rustc --explain E0308`. > *** Error 1 in /home/trinity/harakit (Makefile:66 'build/o/libgetopt.rlib') > ``` > Here are my versions on everything: > > ``` > $ rustc --version > rustc 1.76.0 (07dca489a 2024-02-04) (built from a source tarball) > $ clang --version > OpenBSD clang version 16.0.6 > Target: aarch64-unknown-openbsd7.5 > Thread model: posix > InstalledDir: /usr/bin > $ uname -a > OpenBSD laika.my.domain 7.5 GENERIC#131 arm64 > ``` > > And here's my git status: > > ``` > $ git status > On branch getopt-bindings > Your branch is up to date with 'origin/getopt-bindings'. > > Changes not staged for commit: > (use "git add <file>..." to update what will be committed) > (use "git restore <file>..." to discard changes in working directory) > modified: src/swab.rs > > Untracked files: > (use "git add <file>..." to include in what will be committed) > .helix/ > > no changes added to commit (use "git add" and/or "git commit -a") > ``` This occurs because I was casting a Box to an `i8` because on amd64 it is equivalent to a `c_char` but on aarch64 a `c_char` is an `u8`. Making changes now.
emma added 1 commit 2024-06-28 16:23:23 +00:00
Author
Owner

I committed my changes to swab(1) as I believe they will work, though I'm unable to test them at the moment.

$ pdpmake swab 
rustc -Clink-arg=-fuse-ld=mold -Copt-level=z -Ccodegen-units=1 	-Cpanic=abort -Cstrip=symbols -Ctarget-cpu=native -Cembed-bitcode=yes --extern getopt=build/o/libgetopt.rlib \
	--extern sysexits=build/o/libsysexits.rlib \
	-o build/bin/swab src/swab.rs
error[E0432]: unresolved import `getopt::Parser`
  --> src/swab.rs:27:20
   |
27 | use getopt::{ Opt, Parser };
   |                    ^^^^^^ no `Parser` in the root

error[E0425]: cannot find value `arg` in this scope
   --> src/swab.rs:56:12
    |
56  |                     match arg.parse::<usize>() {
    |                           ^^^ help: a function with a similar name exists: `args`
    |
   ::: /home/emma/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/env.rs:803:1
    |
803 | pub fn args() -> Args {
    | --------------------- similarly named function `args` defined here

warning: unused import: `Opt`
  --> src/swab.rs:27:15
   |
27 | use getopt::{ Opt, Parser };
   |               ^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error[E0599]: no method named `getopt` found for struct `Vec<String>` in the current scope
  --> src/swab.rs:52:29
   |
52 |     while let Some(opt) = argv.getopt("fw:") {
   |                                ^^^^^^
   |
  ::: /home/emma/src/remote/git.tebibyte.media/bonsai/harakit/src/libgetopt.rs:78:5
   |
78 |     fn getopt(&self, optstring: &str) -> Option<Opt>;
   |        ------ the method is available for `Vec<String>` here
   |
   = help: items from traits can only be used if the trait is in scope
help: trait `GetOpt` which provides `getopt` is implemented but not in scope; perhaps you want to import it
   |
19 + use getopt::GetOpt;
   |
help: there is a method `get` with a similar name
   |
52 |     while let Some(opt) = argv.get("fw:") {
   |                                ~~~

error: aborting due to 3 previous errors; 1 warning emitted

Some errors have detailed explanations: E0425, E0432, E0599.
For more information about an error, try `rustc --explain E0425`.
pdpmake: (Makefile:139): failed to build 'build/bin/swab' exit 1
> I committed my changes to swab(1) as I believe they will work, though I'm unable to test them at the moment. ``` $ pdpmake swab rustc -Clink-arg=-fuse-ld=mold -Copt-level=z -Ccodegen-units=1 -Cpanic=abort -Cstrip=symbols -Ctarget-cpu=native -Cembed-bitcode=yes --extern getopt=build/o/libgetopt.rlib \ --extern sysexits=build/o/libsysexits.rlib \ -o build/bin/swab src/swab.rs error[E0432]: unresolved import `getopt::Parser` --> src/swab.rs:27:20 | 27 | use getopt::{ Opt, Parser }; | ^^^^^^ no `Parser` in the root error[E0425]: cannot find value `arg` in this scope --> src/swab.rs:56:12 | 56 | match arg.parse::<usize>() { | ^^^ help: a function with a similar name exists: `args` | ::: /home/emma/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/env.rs:803:1 | 803 | pub fn args() -> Args { | --------------------- similarly named function `args` defined here warning: unused import: `Opt` --> src/swab.rs:27:15 | 27 | use getopt::{ Opt, Parser }; | ^^^ | = note: `#[warn(unused_imports)]` on by default error[E0599]: no method named `getopt` found for struct `Vec<String>` in the current scope --> src/swab.rs:52:29 | 52 | while let Some(opt) = argv.getopt("fw:") { | ^^^^^^ | ::: /home/emma/src/remote/git.tebibyte.media/bonsai/harakit/src/libgetopt.rs:78:5 | 78 | fn getopt(&self, optstring: &str) -> Option<Opt>; | ------ the method is available for `Vec<String>` here | = help: items from traits can only be used if the trait is in scope help: trait `GetOpt` which provides `getopt` is implemented but not in scope; perhaps you want to import it | 19 + use getopt::GetOpt; | help: there is a method `get` with a similar name | 52 | while let Some(opt) = argv.get("fw:") { | ~~~ error: aborting due to 3 previous errors; 1 warning emitted Some errors have detailed explanations: E0425, E0432, E0599. For more information about an error, try `rustc --explain E0425`. pdpmake: (Makefile:139): failed to build 'build/bin/swab' exit 1 ```
Owner

Word, let me go fix those real quick.

Word, let me go fix those real quick.
trinity added 1 commit 2024-06-29 12:03:01 +00:00
Author
Owner
error[E0599]: no method named `parse` found for enum `Option` in the current scope
    --> src/swab.rs:56:22
     |
56   |                     match opt.arg().parse::<usize>() {
     |                                     ^^^^^ method not found in `Option<String>`
     |
note: the method `parse` exists on the type `String`
    --> /home/emma/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/mod.rs:2419:5
     |
2419 |     pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider using `Option::expect` to unwrap the `String` value, panicking if the value is an `Option::None`
     |
56   |                     match opt.arg().expect("REASON").parse::<usize>() {
     |                                    +++++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
pdpmake: (Makefile:139): failed to build 'build/bin/swab' exit 1

You can safely .unwrap() this.

``` error[E0599]: no method named `parse` found for enum `Option` in the current scope --> src/swab.rs:56:22 | 56 | match opt.arg().parse::<usize>() { | ^^^^^ method not found in `Option<String>` | note: the method `parse` exists on the type `String` --> /home/emma/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/mod.rs:2419:5 | 2419 | pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::expect` to unwrap the `String` value, panicking if the value is an `Option::None` | 56 | match opt.arg().expect("REASON").parse::<usize>() { | +++++++++++++++++ error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0599`. pdpmake: (Makefile:139): failed to build 'build/bin/swab' exit 1 ``` You can safely `.unwrap()` this.
Author
Owner

I’m going to go ahead and fix the swab(1) build so that I can merge this branch in.

I’m going to go ahead and fix the `swab(1)` build so that I can merge this branch in.
emma added 1 commit 2024-06-29 13:51:27 +00:00
emma closed this pull request 2024-06-29 13:53:37 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Reference: bonsai/harakit#91
No description provided.