Skip to content

Commit

Permalink
Allow referenced context types in JSON array and object deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Oct 22, 2024
1 parent 654fb6c commit 62df5d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions firmware/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ pub struct Config {
}

impl FromJsonObject for Config {
type Context = ();
type Context<'ctx> = ();

async fn read_next<R: BufRead>(
&mut self,
key: String,
json: &mut json::Reader<R>,
_context: &Self::Context,
_context: &Self::Context<'_>,
) -> Result<(), json::Error<R::Error>> {
match &*key {
"wifi-ssid" => self.wifi_ssid = json.read().await?,
Expand Down
28 changes: 14 additions & 14 deletions firmware/src/json/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<R: BufRead> Reader<R> {
/// `Default` implementation and its `FromJsonObject` implementation is called to read each
/// field's value. This doesn't allocate any memory while reading the object (except for the
/// current key), so the type's implementation can choose how values are stored.
pub async fn read_object<C: Default, T: FromJsonObject<Context = C>>(
pub async fn read_object<C: Default, T: for<'ctx> FromJsonObject<Context<'ctx> = C>>(
&mut self,
) -> Result<T, Error<R::Error>> {
self.read_object_with_context(&C::default()).await
Expand All @@ -82,7 +82,7 @@ impl<R: BufRead> Reader<R> {
/// `FromJsonObject` implementation.
pub async fn read_object_with_context<T: FromJsonObject>(
&mut self,
context: &T::Context,
context: &T::Context<'_>,
) -> Result<T, Error<R::Error>> {
let mut obj = T::default();
self.expect(b'{').await?;
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<R: BufRead> Reader<R> {
/// `Default` implementation and its `FromJsonArray` implementation is called to read each
/// element. This doesn't allocate any memory while reading the array, so the type's
/// implementation can choose how elements are stored.
pub async fn read_array<C: Default, T: FromJsonArray<Context = C>>(
pub async fn read_array<C: Default, T: for<'ctx> FromJsonArray<Context<'ctx> = C>>(
&mut self,
) -> Result<T, Error<R::Error>> {
self.read_array_with_context(&C::default()).await
Expand All @@ -124,7 +124,7 @@ impl<R: BufRead> Reader<R> {
/// `FromJsonArray` implementation.
pub async fn read_array_with_context<T: FromJsonArray>(
&mut self,
context: &T::Context,
context: &T::Context<'_>,
) -> Result<T, Error<R::Error>> {
let mut vec = T::default();
self.expect(b'[').await?;
Expand Down Expand Up @@ -408,7 +408,7 @@ impl<T: FromJson> FromJson for Vec<T> {
}
}

impl<C: Default, T: FromJsonObject<Context = C>> FromJson for T {
impl<C: Default, T: for<'ctx> FromJsonObject<Context<'ctx> = C>> FromJson for T {
async fn from_json<R: BufRead>(json: &mut Reader<R>) -> Result<T, Error<R::Error>> {
json.read_object().await
}
Expand All @@ -425,23 +425,23 @@ impl FromJson for Value {
/// next element.
pub trait FromJsonArray: Sized + Default {
/// Additional context information passed to deserialization
type Context: ?Sized;
type Context<'ctx>: ?Sized;

/// Read next array element from given JSON reader
async fn read_next<R: BufRead>(
&mut self,
json: &mut Reader<R>,
context: &Self::Context,
context: &Self::Context<'_>,
) -> Result<(), Error<R::Error>>;
}

impl<T: FromJson> FromJsonArray for Vec<T> {
type Context = ();
type Context<'ctx> = ();

async fn read_next<R: BufRead>(
&mut self,
json: &mut Reader<R>,
_context: &Self::Context,
_context: &Self::Context<'_>,
) -> Result<(), Error<R::Error>> {
let elem = json.read().await?;
self.push(elem);
Expand All @@ -454,25 +454,25 @@ impl<T: FromJson> FromJsonArray for Vec<T> {
/// next value.
pub trait FromJsonObject: Sized + Default {
/// Additional context information passed to deserialization
type Context: ?Sized;
type Context<'ctx>: ?Sized;

/// Read next object value from given JSON reader
async fn read_next<R: BufRead>(
&mut self,
key: String,
json: &mut Reader<R>,
context: &Self::Context,
context: &Self::Context<'_>,
) -> Result<(), Error<R::Error>>;
}

impl<T: FromJson> FromJsonObject for BTreeMap<String, T> {
type Context = ();
type Context<'ctx> = ();

async fn read_next<R: BufRead>(
&mut self,
key: String,
json: &mut Reader<R>,
_context: &Self::Context,
_context: &Self::Context<'_>,
) -> Result<(), Error<R::Error>> {
let value = json.read().await?;
self.insert(key, value);
Expand Down Expand Up @@ -511,7 +511,7 @@ mod tests {
&mut self,
key: String,
json: &mut Reader<R>,
_context: &Self::Context,
_context: &Self::Context<'_>,
) -> Result<(), Error<R::Error>> {
match &*key {
"foo" => self.foo = json.read().await?,
Expand Down
12 changes: 6 additions & 6 deletions firmware/src/vereinsflieger/proto_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub struct AccessTokenResponse {
}

impl FromJsonObject for AccessTokenResponse {
type Context = ();
type Context<'ctx> = ();

async fn read_next<R: BufRead>(
&mut self,
key: String,
json: &mut json::Reader<R>,
_context: &Self::Context,
_context: &Self::Context<'_>,
) -> Result<(), json::Error<R::Error>> {
match &*key {
"accesstoken" => self.accesstoken = json.read().await?,
Expand Down Expand Up @@ -72,13 +72,13 @@ pub struct SignInResponse {
}

impl FromJsonObject for SignInResponse {
type Context = ();
type Context<'ctx> = ();

async fn read_next<R: BufRead>(
&mut self,
_key: String,
json: &mut json::Reader<R>,
_context: &Self::Context,
_context: &Self::Context<'_>,
) -> Result<(), json::Error<R::Error>> {
_ = json.read_any().await?;
Ok(())
Expand Down Expand Up @@ -120,13 +120,13 @@ pub struct UserInformationResponse {
}

impl FromJsonObject for UserInformationResponse {
type Context = ();
type Context<'ctx> = ();

async fn read_next<R: BufRead>(
&mut self,
key: String,
json: &mut json::Reader<R>,
_context: &Self::Context,
_context: &Self::Context<'_>,
) -> Result<(), json::Error<R::Error>> {
match &*key {
"uid" => self.uid = json.read_any().await?.try_into()?,
Expand Down

0 comments on commit 62df5d0

Please sign in to comment.