Skip to content

Commit

Permalink
feat: get, set methods for OpenEnum
Browse files Browse the repository at this point in the history
These are meant to be the primary ways to get and set
open enumeration fields, instead of the getters and setters generated
by Message derive.
  • Loading branch information
mzabaluev committed Nov 12, 2024
1 parent a8a6d05 commit c0b885c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion prost/src/open_enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::encoding::{DecodeContext, WireType};
use crate::{DecodeError, Message};
use crate::{DecodeError, Message, UnknownEnumValue};

use bytes::{Buf, BufMut};

Expand Down Expand Up @@ -128,6 +128,23 @@ impl<T> OpenEnum<T> {
}
}

/// If the value of the open enum is known, returns it in `Ok`, otherwise
/// returns an `Err` with the unknown value.
pub fn get(&self) -> Result<T, UnknownEnumValue>
where
T: Clone,
{
match self {
Self::Known(v) => Ok(v.clone()),
Self::Unknown(r) => Err(UnknownEnumValue(*r)),
}
}

/// Sets the value of receiver to the provided known value.
pub fn set(&mut self, value: T) {
*self = Self::Known(value);
}

/// If the value of the open enum is known, returns it in `Some`, otherwise
/// returns `None`.
pub fn known(self) -> Option<T> {
Expand Down

0 comments on commit c0b885c

Please sign in to comment.