mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-06-16 12:10:38 +00:00
33 lines
726 B
Rust
33 lines
726 B
Rust
|
#[derive(Debug)]
|
||
|
pub struct IdempotencyKey(String);
|
||
|
|
||
|
impl TryFrom<String> for IdempotencyKey {
|
||
|
type Error = anyhow::Error;
|
||
|
|
||
|
fn try_from(s: String) -> Result<Self, Self::Error> {
|
||
|
if s.is_empty() {
|
||
|
anyhow::bail!("The idempotency key cannot be empty");
|
||
|
}
|
||
|
let max_length = 50;
|
||
|
if s.len() >= max_length {
|
||
|
anyhow::bail!(
|
||
|
"The idempotency key must be shorter
|
||
|
than {max_length} characters"
|
||
|
);
|
||
|
}
|
||
|
Ok(Self(s))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<IdempotencyKey> for String {
|
||
|
fn from(k: IdempotencyKey) -> Self {
|
||
|
k.0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl AsRef<str> for IdempotencyKey {
|
||
|
fn as_ref(&self) -> &str {
|
||
|
&self.0
|
||
|
}
|
||
|
}
|