Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit 2fadd8b

Browse files
committed
validation: add channel topic validation
1 parent d09774d commit 2fadd8b

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

http/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ rust-version = "1.57.0"
1111
version = "0.1.0-dev.6"
1212

1313
[dependencies]
14-
guilded_model = "0.1.0-dev.13"
14+
guilded_model = "0.1.0-dev.14"
15+
guilded_validation = "0.1.0-dev.1"
1516

1617
hyper = { default-features = false, features = [ "client", "http1", "http2", "runtime" ], version = "0.14.19" }
1718
hyper-rustls = { default-features = false, optional = true, features = [ "http1", "http2" ], version = "0.23.0" }
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::client::Client;
2-
use guilded_model::channel::ServerChannelType;
32

3+
use guilded_model::channel::ServerChannelType;
4+
use guilded_validation::channel::{self, ChannelValidationError};
45
use serde::Serialize;
56

67
#[must_use = "requests must be configured and executed"]
@@ -9,8 +10,29 @@ pub struct ServerChannelCreate<'a> {
910
fields: ServerChannelCreateFields<'a>,
1011
}
1112

13+
impl<'a> ServerChannelCreate<'a> {
14+
pub(crate) fn new(
15+
client: &'a Client,
16+
name: &'a str,
17+
r#type: ServerChannelType,
18+
) -> Result<Self, ChannelValidationError> {
19+
channel::validate_name_length(name)?;
20+
21+
Ok(Self {
22+
client,
23+
fields: ServerChannelCreateFields {
24+
name,
25+
topic: None,
26+
r#type,
27+
}
28+
})
29+
}
30+
}
31+
1232
#[derive(Serialize)]
1333
struct ServerChannelCreateFields<'a> {
1434
name: &'a str,
35+
#[serde(skip_serializing_if = "Option::is_none")]
36+
topic: Option<&'a str>,
1537
r#type: ServerChannelType,
1638
}

validation/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ edition = "2021"
88
license = "ISC"
99
repository = "https://github.com/HarTexTeam/guilded-rs"
1010
rust-version = "1.57.0"
11-
version = "0.1.0-dev.1"
11+
version = "0.1.0-dev.2"
1212

1313
[dependencies]

validation/src/channel.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ impl Display for ChannelValidationError {
1212
ChannelValidationErrorType::InvalidNameLength => {
1313
f.write_str("invalid length of channel name")
1414
}
15+
ChannelValidationErrorType::InvalidTopicLength => {
16+
f.write_str("invalid length of channel topic")
17+
}
1518
}
1619
}
1720
}
@@ -22,11 +25,15 @@ impl Error for ChannelValidationError {}
2225
#[non_exhaustive]
2326
pub enum ChannelValidationErrorType {
2427
InvalidNameLength,
28+
InvalidTopicLength,
2529
}
2630

2731
pub const CHANNEL_NAME_MIN_LENGTH: usize = 1;
2832
pub const CHANNEL_NAME_MAX_LENGTH: usize = 100;
2933

34+
pub const CHANNEL_TOPIC_MIN_LENGTH: usize = 1;
35+
pub const CHANNEL_TOPIC_MAX_LENGTH: usize = 512;
36+
3037
pub fn validate_name_length(name: impl AsRef<str>) -> Result<(), ChannelValidationError> {
3138
let length = name.as_ref().chars().count();
3239

@@ -38,3 +45,15 @@ pub fn validate_name_length(name: impl AsRef<str>) -> Result<(), ChannelValidati
3845
r#type: ChannelValidationErrorType::InvalidNameLength,
3946
})
4047
}
48+
49+
pub fn validate_topic_length(topic: impl AsRef<str>) -> Result<(), ChannelValidationError> {
50+
let length = topic.as_ref().chars().count();
51+
52+
if (CHANNEL_TOPIC_MIN_LENGTH..=CHANNEL_TOPIC_MAX_LENGTH).contains(&length) {
53+
return Ok(());
54+
}
55+
56+
Err(ChannelValidationError {
57+
r#type: ChannelValidationErrorType::InvalidTopicLength,
58+
})
59+
}

0 commit comments

Comments
 (0)