Skip to content

Commit b48db5e

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 b48db5e

File tree

4 files changed

+30
-1
lines changed

4 files changed

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

0 commit comments

Comments
 (0)