Skip to content

Remove .ok() calls on Results #152

@rnbguy

Description

@rnbguy

There are a few Result::ok() calls in the source code - a few of them are redundant, and a few of them are used wrongly.

  1. We convert Option to Result and then to Option. This is redundant as we can simply use the first Option.

fn lookup_module(&self, port_id: &PortId) -> Option<ModuleId> {
self.port_to_module_map
.get(port_id)
.ok_or(RouterError::UnknownPort {
port_id: port_id.clone(),
})
.cloned()
.ok()
}

  1. We ignore the fact path serialization has failed.

let path: Option<Path> = request.path.try_into().ok();
let modules = self.modules.read_access();
let height = Height::from(request.height as u64);
for IdentifiedModule { id, module } in modules.iter() {
match module.query(&request.data, path.as_ref(), height, request.prove) {

  1. We ignore a codec encode/decode that has failed in concrete Codec implementations. For example, with JsonCodec,

fn encode(d: &Self::Value) -> Option<Self::Encoded> {
serde_json::to_string(d).ok()
}
fn decode(bytes: &[u8]) -> Option<Self::Value> {
let json_string = String::from_utf8(bytes.to_vec()).ok()?;
serde_json::from_str(&json_string).ok()
}

The Codec trait should have an associated type Error which lets us return a Result with the failure type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions