30 lines
706 B
Rust
30 lines
706 B
Rust
#[derive(Debug)]
|
|
pub struct IdempotencyKey(String);
|
|
|
|
impl TryFrom<String> for IdempotencyKey {
|
|
type Error = String;
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
if value.is_empty() {
|
|
return Err("The idempotency key cannot be empty.".into());
|
|
}
|
|
let max_length = 50;
|
|
if value.len() >= max_length {
|
|
return Err("The idempotency key must be shorter than {max_length} characters.".into());
|
|
}
|
|
Ok(Self(value))
|
|
}
|
|
}
|
|
|
|
impl From<IdempotencyKey> for String {
|
|
fn from(value: IdempotencyKey) -> Self {
|
|
value.0
|
|
}
|
|
}
|
|
|
|
impl AsRef<str> for IdempotencyKey {
|
|
fn as_ref(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|