Skip to content

Commit 5fde5c3

Browse files
committed
Implement StringExclNull wrapper struct
This type can be used in query parameter structs to ensure that passed values do not contain null bytes.
1 parent 19a2e98 commit 5fde5c3

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ chrono = { version = "=0.4.39", default-features = false, features = ["serde"] }
7171
clap = { version = "=4.5.23", features = ["derive", "env", "unicode", "wrap_help"] }
7272
cookie = { version = "=0.18.1", features = ["secure"] }
7373
deadpool-diesel = { version = "=0.6.1", features = ["postgres", "tracing"] }
74-
derive_more = { version = "=1.0.0", features = ["deref", "deref_mut"] }
74+
derive_more = { version = "=1.0.0", features = ["deref", "deref_mut", "display"] }
7575
dialoguer = "=0.11.0"
7676
diesel = { version = "=2.2.6", features = ["postgres", "serde_json", "chrono", "numeric"] }
7777
diesel-async = { version = "=0.5.2", features = ["async-connection-wrapper", "deadpool", "postgres"] }

src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ pub mod errors;
88
mod io_util;
99
mod request_helpers;
1010
pub mod rfc3339;
11+
pub mod string_excl_null;
1112
pub mod token;
1213
pub mod tracing;

src/util/string_excl_null.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use derive_more::{Deref, Display};
2+
use serde::Deserialize;
3+
4+
/// A string that does not contain null bytes (`\0`).
5+
#[derive(Clone, Debug, Display, Deref, Deserialize, utoipa::ToSchema)]
6+
#[serde(try_from = "String")]
7+
pub struct StringExclNull(String);
8+
9+
/// Error indicating that a string contained a null byte.
10+
#[derive(Debug, thiserror::Error)]
11+
#[error("string contains null byte")]
12+
pub struct StringExclNullError;
13+
14+
impl TryFrom<String> for StringExclNull {
15+
type Error = StringExclNullError;
16+
17+
fn try_from(s: String) -> Result<Self, Self::Error> {
18+
if s.contains('\0') {
19+
Err(StringExclNullError)
20+
} else {
21+
Ok(Self(s))
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)