Skip to content

Commit

Permalink
Add validation of message length in builder
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Jun 1, 2024
1 parent 30d1885 commit 3e29e3a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/types/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use derive_builder::Builder;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Builder)]
#[builder(build_fn(validate = "Self::validate"))]
pub struct OutgoingCall {
/// Message text of the call
pub(crate) text: String,
Expand All @@ -20,6 +21,21 @@ pub struct OutgoingCall {
pub(crate) emergency: bool,
}

impl OutgoingCallBuilder {
fn validate(&self) -> Result<(), String> {
match &self.text {
Some(text) => {
if text.len() > 80 {
Err("Text must be 80 characters or less".to_string())
} else {
Ok(())
}
}
None => Err("Text must be set".to_string()),
}
}
}

#[derive(Debug, Deserialize)]
pub struct Call {
/// Message text of the call
Expand All @@ -43,3 +59,49 @@ pub struct Call {
/// Flag indicating if this call was sent with high priority
pub emergency: bool,
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn build_80_char() {
let text =
"01234567890123456789012345678901234567890123456789012345678901234567890123456789";
assert_eq!(text.len(), 80);

let call = OutgoingCallBuilder::default()
.recipients(vec!["m0nxn".to_string()])
.transmitter_groups(vec!["all".to_string()])
.text(text.to_string())
.build()
.unwrap();

assert_eq!(call.text, text);
}

#[test]
#[should_panic]
fn build_81_char() {
let text =
"012345678901234567890123456789012345678901234567890123456789012345678901234567890";
assert_eq!(text.len(), 81);

let _ = OutgoingCallBuilder::default()
.recipients(vec!["m0nxn".to_string()])
.transmitter_groups(vec!["all".to_string()])
.text(text.to_string())
.build()
.unwrap();
}

#[test]
#[should_panic]
fn build_no_text() {
let _ = OutgoingCallBuilder::default()
.recipients(vec!["m0nxn".to_string()])
.transmitter_groups(vec!["all".to_string()])
.build()
.unwrap();
}
}
59 changes: 59 additions & 0 deletions src/types/news.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use derive_builder::Builder;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Builder)]
#[builder(build_fn(validate = "Self::validate"))]
pub struct OutgoingNews {
/// Name of the rubric to send to
#[serde(rename = "rubricName")]
Expand All @@ -16,6 +17,21 @@ pub struct OutgoingNews {
pub(crate) number: i8,
}

impl OutgoingNewsBuilder {
fn validate(&self) -> Result<(), String> {
match &self.text {
Some(text) => {
if text.len() > 80 {
Err("Text must be 80 characters or less".to_string())
} else {
Ok(())
}
}
None => Err("Text must be set".to_string()),
}
}
}

#[derive(Debug, Deserialize)]
pub struct News {
/// Name of the rubric to send to
Expand All @@ -35,3 +51,46 @@ pub struct News {
#[serde(rename = "ownerName")]
pub sender: String,
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn build_80_char() {
let text =
"01234567890123456789012345678901234567890123456789012345678901234567890123456789";
assert_eq!(text.len(), 80);

let news = OutgoingNewsBuilder::default()
.rubric("test".to_string())
.text(text.to_string())
.build()
.unwrap();

assert_eq!(news.text, text);
}

#[test]
#[should_panic]
fn build_81_char() {
let text =
"012345678901234567890123456789012345678901234567890123456789012345678901234567890";
assert_eq!(text.len(), 81);

let _ = OutgoingNewsBuilder::default()
.rubric("test".to_string())
.text(text.to_string())
.build()
.unwrap();
}

#[test]
#[should_panic]
fn build_no_text() {
let _ = OutgoingNewsBuilder::default()
.rubric("test".to_string())
.build()
.unwrap();
}
}

0 comments on commit 3e29e3a

Please sign in to comment.