Accept ip_port and net_id as Option<String> or Option<&str> for sbot::init() #52

Open
glyph wants to merge 1 commits from string_str_ip_port into main
Owner

Fairly simple change that gives us more flexibility when calling sbot::init() by allowing the ip_port and net_id parameters to be passed as Option<String> or Option<&str>.

This makes it possible to define const values for the parameters required by sbot::init(), for example:

const KEYSTORE: Keystore = Keystore::GoSbot;
const IP_PORT: Option<&str> = Some("127.0.0.1:8021");
const NET_ID: Option<&str> = None;

Then any sbot initialisation can reuse those values:

let mut sbot = Sbot::init(KEYSTORE, IP_PORT, NET_ID)
    .await
    .map_err(|e| e.to_string())?;

The downside is that None parameters need to be annotated when the function is called. For example:

Sbot::init(Keystore::CustomGoSbot(key_path), None::<&str>, None::<&str>).await?

Notice the None::<&str> annotation.

@notplants What do you think? I'm happy to make the required changes to peach-web and peach-config, along with the various documentation, but wanted to check-in with you because this represents a breaking change.


This was prompted by my work on lykin, specifically wanting to be able to quickly define a custom port for go-sbot.

Fairly simple change that gives us more flexibility when calling `sbot::init()` by allowing the `ip_port` and `net_id` parameters to be passed as `Option<String>` or `Option<&str>`. This makes it possible to define `const` values for the parameters required by `sbot::init()`, for example: ```rust const KEYSTORE: Keystore = Keystore::GoSbot; const IP_PORT: Option<&str> = Some("127.0.0.1:8021"); const NET_ID: Option<&str> = None; ``` Then any `sbot` initialisation can reuse those values: ```rust let mut sbot = Sbot::init(KEYSTORE, IP_PORT, NET_ID) .await .map_err(|e| e.to_string())?; ``` The downside is that `None` parameters need to be annotated when the function is called. For example: ```rust Sbot::init(Keystore::CustomGoSbot(key_path), None::<&str>, None::<&str>).await? ``` Notice the `None::<&str>` annotation. @notplants What do you think? I'm happy to make the required changes to `peach-web` and `peach-config`, along with the various documentation, but wanted to check-in with you because this represents a breaking change. ----- This was prompted by my work on `lykin`, specifically wanting to be able to quickly define a custom port for go-sbot.
glyph added 1 commit 2022-07-13 10:22:18 +00:00
glyph requested review from notplants 2022-07-19 07:42:27 +00:00
Owner

Interesting, it seems like a natural thing to want to be able to define, but that None::<&str> syntax is pretty bizarre. But it seems like thats the way to do "optional" arguments in rust so looks good.

Then any sbot initialisation can reuse those values:

fwiw, the pattern I would probably use in the application code is to define a separate utility function with no arguments init_sbot(), which calls Sbot::init(KEYSTORE, IP_PORT, NET_ID) inside of it, rather than calling Sbot::init in multiple places passing the same constant arguments (and thus avoiding the code duplication of needing the same arguments to be passed)

Interesting, it seems like a natural thing to want to be able to define, but that None::<&str> syntax is pretty bizarre. But it seems like thats the way to do "optional" arguments in rust so looks good. > Then any sbot initialisation can reuse those values: fwiw, the pattern I would probably use in the application code is to define a separate utility function with no arguments init_sbot(), which calls Sbot::init(KEYSTORE, IP_PORT, NET_ID) inside of it, rather than calling Sbot::init in multiple places passing the same constant arguments (and thus avoiding the code duplication of needing the same arguments to be passed)
Author
Owner

Haha yeah the turbofish is definitely one of the weirder pieces of Rust syntax.

the pattern I would probably use in the application code is to define a separate utility function with no arguments init_sbot(), which calls Sbot::init(KEYSTORE, IP_PORT, NET_ID) inside of it

Perfect, thanks for the tip. I'll do that.

I'll get some other PR's ready before merging this.

Haha yeah the turbofish is definitely one of the weirder pieces of Rust syntax. > the pattern I would probably use in the application code is to define a separate utility function with no arguments init_sbot(), which calls Sbot::init(KEYSTORE, IP_PORT, NET_ID) inside of it Perfect, thanks for the tip. I'll do that. I'll get some other PR's ready before merging this.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
You can also view command line instructions.

Step 1:

From your project repository, check out a new branch and test the changes.
git checkout -b string_str_ip_port main
git pull origin string_str_ip_port

Step 2:

Merge the changes and update on Gitea.
git checkout main
git merge --no-ff string_str_ip_port
git push origin main
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.

Dependencies

No dependencies set.

Reference: golgi-ssb/golgi#52
No description provided.