Skip to content

upd: using bincode and leveraging use of slices#71

Closed
muse254 wants to merge 3 commits into
mainfrom
chore/clippy-and-fmt
Closed

upd: using bincode and leveraging use of slices#71
muse254 wants to merge 3 commits into
mainfrom
chore/clippy-and-fmt

Conversation

@muse254
Copy link
Copy Markdown
Contributor

@muse254 muse254 commented Nov 16, 2025

Changes:

  • cargo-fmt and cargo-clippy
  • using bincode and leveraging use of slices

@muse254 muse254 marked this pull request as ready for review November 17, 2025 02:34
@muse254 muse254 requested a review from dtpthao November 17, 2025 02:34
* rebase to clippy-fmt pr
* rm unrelated
@muse254 muse254 changed the title chore: cargo-clippy&cargo-fmt upd: using bincode and leveraging use of slices Nov 18, 2025
Comment thread forward-proxy/src/handler/mod.rs Outdated
pub x509_certificate: String,
pub client_id: String,
body: Some(
serde_json::to_vec(&response_body).expect("this struct is json serializable"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would you remove usage of .to_bytes() if you can add expect statement to the method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't hate me.

But the argument here is that this .to_bytes indirection is not necessary imo since we are only calling the single function serde_json::bytes. We are also introducing bincode so it removes any assumptions for whoever is reading the code.

It also gives more clarity to inline what we have and keep things moving instead of the reader navigating to the function that really doesn't do any other operations by itself. It's acting as an alias.

Indirection makes sense when there's reason for it e.g. testing(plugin custom logic), public APIs (traits come in handy) and where we need to isolate a chunk of repeatable code and such

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't hate me.

But the argument here is that this .to_bytes indirection is not necessary imo since we are only calling the single function serde_json::bytes. We are also introducing bincode so it removes any assumptions for whoever is reading the code.

It also gives more clarity to inline what we have and keep things moving instead of the reader navigating to the function that really doesn't do any other operations by itself. It's acting as an alias.

Indirection makes sense when there's reason for it e.g. testing(plugin custom logic), public APIs (traits come in handy) and where we need to isolate a chunk of repeatable code and such

you can change the name of the method if its current isn't clear enough. I agree it's just a simple operation, but if you need to update that operation, you'll need to update it repeatedly in every place you use it, so do I also need to review it repeatedly?

We only have a few usages of it at the moment, but we might need to update the codebase and will use more in the future. Also, when reading the main functions, I prefer to focus on the core logic rather than low-level implementation details.

Comment thread forward-proxy/src/handler/mod.rs Outdated
.unwrap_or_default();

let res_from_rp =
match serde_json::from_slice::<InitTunnelResponseFromRP>(ctx.get_response_body()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a reason for removing the usage of utils here? I prefer not to import dependencies directly in the main functions. Instead, the main functions should call utils, which helps us control dependency usage and makes it easier to change dependencies later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument makes sense, but we can have a more holistic conversation on the pros and cons of the api.
If we go this route for smaller/one-off calls the number of indirections will make the code spaggetti

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument makes sense, but we can have a more holistic conversation on the pros and cons of the api. If we go this route for smaller/one-off calls the number of indirections will make the code spaggetti

I see your point, but in this context the pros still outweigh the cons for me. Including the details of a simple conversion operation inside a large function makes the code harder to read and review.

}

fn get_handlers(&self, method: &Method, path: &str) -> Option<&Box<[APIHandler<T>]>> {
fn get_handlers(&self, method: &Method, path: &str) -> Option<&[APIHandler<T>]> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice refactor!


match encrypted_message {
Ok(encrypted_message) => {
let data = bincode::encode_to_vec(&encrypted_message, bincode::config::standard())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update to_bytes() methods, so you don't need to update here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EncryptedMessage is from ntor crate

ctx: &mut Layer8Context,
header_key: &str,
jwt_secret: &Vec<u8>
jwt_secret: &[u8],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how [u8] is better than Vec<u8>?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&[u8] can accept both &Vec and &[u8] as arguments when calling a function
&Vec only accepts &Vec

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

Ok(res) => Ok(res),
ctx: &mut Layer8Context,
) -> Result<EncryptedMessage, APIHandlerResponse> {
match bincode::decode_from_slice(&ctx.get_request_body(), bincode::config::standard()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, update ProxyHandler::parse_request_body, it's reusable - so you don't need to update repeatedly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most implementations still use the json implementation, the args are tightly-coupled with Generic args and would take some refactoring and unrelated code changes in diff files. Does creating a new method for ProxyHandler make sense instead?

Err(APIHandlerResponse {
status: StatusCode::BAD_REQUEST,
body,
body: Some(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parse_request_body and ErrorResponse were created to be reusable, so we don’t have to duplicate the same logic throughout the codebase or update it in multiple places later. Do you have a reason for not using them here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diff errors here since it's from bincode impl

let wrapped_request: L8RequestObject = bytes_to_json(decrypted_data)
.map_err(|err| {
let wrapped_request: L8RequestObject =
serde_json::from_slice(&decrypted_data).map_err(|err| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, update the utils function

ntor_server.set_shared_secret(shared_secret);

let data = response_body.to_bytes();
let data = serde_json::to_vec(&response_body).expect("the struct is json serializable");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the work would be much easier if you just update the utils funciton/method?

@muse254
Copy link
Copy Markdown
Contributor Author

muse254 commented Dec 1, 2025

Will setup a diff clean PR

@muse254 muse254 closed this Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants