Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples to UPayload doc comments #33

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/proto/uprotocol/upayload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ impl UPayloadFormat {
/// # Errors
///
/// Returns an error if the given string is not a valid media type string or is unsupported by uProtocol.
///
/// # Examples
///
/// ```rust
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs tell us that you needn't put the rust there, by default anything fenced in code block ticks will be interpreted as Rust code. 👍

/// use uprotocol_sdk::uprotocol::UPayloadFormat;
///
/// let parse_attempt = UPayloadFormat::from_media_type("application/json; charset=utf-8");
/// assert!(parse_attempt.is_ok());
/// assert_eq!(parse_attempt.unwrap(), UPayloadFormat::UPAYLOAD_FORMAT_JSON);
///
/// let parse_attempt = UPayloadFormat::from_media_type("application/unsupported");
/// assert!(parse_attempt.is_err());
/// ```
pub fn from_media_type(media_type_string: &str) -> Result<Self, ParsingError> {
if let Ok(media_type) = MediaType::parse(media_type_string) {
let descriptor = UPayloadFormat::enum_descriptor();
Expand Down Expand Up @@ -56,6 +69,15 @@ impl UPayloadFormat {
/// # Returns
///
/// None if the payload format is [`UPayloadFormat::UPAYLOAD_FORMAT_UNSPECIFIED`].
///
/// # Examples
///
/// ```rust
/// use uprotocol_sdk::uprotocol::UPayloadFormat;
///
/// assert_eq!(UPayloadFormat::UPAYLOAD_FORMAT_JSON.to_media_type().unwrap(), "application/json");
/// assert!(UPayloadFormat::UPAYLOAD_FORMAT_UNSPECIFIED.to_media_type().is_none());
/// ```
pub fn to_media_type(&self) -> Option<String> {
let desc = self.descriptor();
let desc_proto = desc.proto();
Expand Down
Loading