Comments (14)
Set the effort/hard
label, because this requires a non-trivial amount of knowledge about proc macros.
from ruma.
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 ?
can be used for error propagation, where the outer function then adds the raw http request / response in the error case (having to add that is why there's currently lots of explicit matches with Err(e) => return Err(…)
in the generated code).
from ruma.
This is about this and the few other times it's generated for the Request
struct?
from ruma.
@DevinR528 yes, my latest comment was about that.
from ruma.
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 TryFrom::try_from
method?
from ruma.
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? 😅
from ruma.
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 match
and return
or compiler complains about request
being moved. This is an improvement over what's there currently.
from ruma.
Why one closure per request field? Is it not possible to have just one try_from_impl
for the whole TryFrom::try_from
?
from ruma.
I don't care much whether it's map_err
or needs to be match
for borrow-checking reasons.
from ruma.
Why one closure per request field? Is it not possible to have just one try_from_impl for the whole TryFrom::try_from?
Type inference sets the type of the closure I'm guessing as the event_id
field try_from_impl
fails
error[E0308]: try expression alternatives have incompatible types
--> ruma-client-api/src/zzz.rs:111:21
|
111 | / try_from_impl(segment)
112 | | .map_err(|err| RequestDeserializationError::new(err, request))?
| |^ expected struct `ruma_identifiers::event_id::EventId`, found struct `ruma_identifiers::room_id::RoomId`
|
= note: expected struct `ruma_identifiers::event_id::EventId<std::boxed::Box<str>>`
found struct `ruma_identifiers::room_id::RoomId<std::boxed::Box<str>>`
from ruma.
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))?
},
})
from ruma.
@DevinR528 You're still misunderstanding something. I want the closure to return Result<Self, DeserializationError>
. The whole
#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 try_from
implementation being a call to the closure + map_err
. Does that make sense? Did you try this before?
from ruma.
It works! Now I just gotta make the code-gen changes and open the PR.
from ruma.
Since there are no more specific refactoring suggestions in here that haven't been implemented or found impossibe, I'll close this.
from ruma.
Related Issues (20)
- Error parsing error response for `POST /_matrix/client/v3/keys/signatures/upload` HOT 5
- Make DST ID types less awkward under the `Arc` ID representation HOT 4
- Implement Matrix 1.8 changes
- Relax validation of identifiers for clients HOT 2
- Implement Matrix 1.9 changes
- There's no way to get the name of the algorithm out of the `SecretEncryptionAlgorithm` struct HOT 1
- `cargo doc` fails to document `ruma_events` HOT 1
- Release automation does not pick up release notes if skipping all previous steps
- Add call.notify event (MSC4075)
- `m.direct` account data parsing should be more lenient HOT 2
- The `strike` and `font` HTML elements are deprecated and `s` is introduced HOT 1
- Implement Matrix 1.10 changes HOT 1
- Inconsistent m.call.candidates candidate definition
- Upgrade http to version 1.x HOT 6
- Upgrade html5ever HOT 1
- Make `room_alias_or_id::Variant` a public enum
- How to send encrypted message to encrypted room HOT 3
- When user reply to their message, the reply mentions the current user HOT 1
- API responses always return a `200 OK` status code HOT 4
- Support optional path parameters for endpoints HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ruma.