Florian
Florian

Reputation: 13

Turning an <unknown> type in the known type in rust

How can I tell the compiler what type "<unknown>" has, if i do know but statically it cannot be determined?

I am using a library function of the jwt crate (version 0.11) to parse a string representation of a jwt token into a Token "Token<H,C,Unverified>" type. The library offers high level of abstraction to use own types for Headers and Claims, so statically it cannot determine the type of the parsed headers and claims. This results in a problem when trying to use the result of the function.

(https://docs.rs/jwt/0.11.0/jwt/struct.Token.html#method.claims):

let token : Result<Header, Claims> = jwt::Token::parse_unverified(token_as_string.as_str());

I know the type of H is Header and the type of C is Claims (Unverified is already the correctly identified type). If I write the code like this, I get the following error

mismatched types [E0308] expected Result<Header, Claims>, found Result<Token<<unknown>, <unknown>, Unverified>, Error>

This makes sense, that the function does not know the type of the header and the claims as the Base64 encoded token needs parsing. However, how can I tell the compiler the types of ? I tried with the type annotation above for token, but this did obviously not work.

Upvotes: 1

Views: 1359

Answers (1)

Aplet123
Aplet123

Reputation: 35482

Your type is erroneously constructed, it should not be:

Result<Header, Claims>

but

Result<jwt::Token<Header, Claims, Unverified>, jwt::error::Error>

Upvotes: 1

Related Questions