New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor the macro code #6
Comments
Set the |
One thing I just recalled again I would potentially like to see in this area is refactor the request / response deserialization functions to contain an inner function where |
Update doc tests to Rust 2018, remove unnecessary main declaration from them
This is about this and the few other times it's generated for the |
@DevinR528 yes, my latest comment was about that. |
What was the function signature you had in mind? Or maybe the better question is how to handle passing the request/response variable around owned in order to create the error (I'm assuming this relates to "where the outer function then adds the raw http request / response in the error case"). Is the goal is to have an inner function that does the matching but somehow hands the error creation back to the |
Without relating this closely to the code (would have to look it up again), this is what I'm thinking of: fn try_from(...) -> Result<T, OuterError> {
let try_from_impl = move || {
fallible_operation_1()?;
fallible_operation_2()?;
fallible_operation_3()
};
try_from_impl().map_err(|e| OuterError {
msg: e,
context: ...,
})
} Does that make sense or is it too abstract? 😅 |
I think the example was good enough. I was trying it out in the expanded code and this is what I came up with. Ok(Self {
room_id: {
let segment = path_segments.get(4usize).unwrap().as_bytes();
let try_from_impl =
move || -> Result<_, ruma_api::error::DeserializationError> {
let decoded =
ruma_api::exports::percent_encoding::percent_decode(segment)
.decode_utf8()
.map_err(ruma_api::error::DeserializationError::from)?;
std::convert::TryFrom::try_from(decoded.deref()).map_err(Into::into)
};
match try_from_impl() {
Ok(val) => val,
Err(err) => {
return Err(RequestDeserializationError::new(err, request).into())
}
}
},
event_id: {
let segment = path_segments.get(6usize).unwrap().as_bytes();
let try_from_impl =
move || -> Result<_, ruma_api::error::DeserializationError> {
let decoded =
ruma_api::exports::percent_encoding::percent_decode(segment)
.decode_utf8()
// Into::into needs type anotations here
.map_err(ruma_api::error::DeserializationError::from)?;
std::convert::TryFrom::try_from(decoded.deref()).map_err(Into::into)
};
// this is the only field that can use `.map_err` in this example since it's the last
try_from_impl().map_err(|err| RequestDeserializationError::new(err, request))?
},
}) All but the last field has to use |
Why one closure per request field? Is it not possible to have just one |
I don't care much whether it's |
Type inference sets the type of the closure I'm guessing as the
|
That was from this code let try_from_impl = move |segment| -> Result<_, ruma_api::error::DeserializationError> {
let decoded = ruma_api::exports::percent_encoding::percent_decode(segment)
.decode_utf8()
.map_err(ruma_api::error::DeserializationError::from)?;
std::convert::TryFrom::try_from(decoded.deref()).map_err(Into::into)
};
Ok(Self {
room_id: {
let segment = path_segments.get(4usize).unwrap().as_bytes();
match try_from_impl(segment) {
Ok(val) => val,
Err(err) => {
return Err(RequestDeserializationError::new(err, request).into())
}
}
},
event_id: {
let segment = path_segments.get(6usize).unwrap().as_bytes();
try_from_impl(segment)
.map_err(|err| RequestDeserializationError::new(err, request))?
},
}) |
@DevinR528 You're still misunderstanding something. I want the closure to return #extract_request_path
#extract_request_query
#extract_request_headers
#extract_request_body
Ok(Self {
#parse_request_path
#parse_request_query
#parse_request_headers
#parse_request_body
}) should be moved into the closure, with the remainder of the |
It works! Now I just gotta make the code-gen changes and open the PR. |
Since there are no more specific refactoring suggestions in here that haven't been implemented or found impossibe, I'll close this. |
In ruma/ruma-api#34, I once again discovered that the macro code could use some refactoring. I will try to work on this soon-ish, although if somebody else wants to look into this, I'd happily mentor that person as well.
The text was updated successfully, but these errors were encountered: