Skip to content

Commit

Permalink
Push PgLTree String conversion to label.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebpuetz committed Mar 4, 2022
1 parent 131b2b2 commit bb556d7
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sqlx-core/src/postgres/types/ltree.rs
Expand Up @@ -27,13 +27,17 @@ pub enum PgLTreeParseError {
pub struct PgLTreeLabel(String);

impl PgLTreeLabel {
pub fn new(label: &str) -> Result<Self, PgLTreeParseError> {
pub fn new<S>(label: S) -> Result<Self, PgLTreeParseError>
where
S: Into<String>,
{
let label = String::from(label);
if label.len() <= 256
&& label
.bytes()
.all(|c| c.is_ascii_alphabetic() || c.is_ascii_digit() || c == b'_')
{
Ok(Self(label.to_owned()))
Ok(Self(label))
} else {
Err(PgLTreeParseError::InvalidLtreeLabel)
}
Expand Down Expand Up @@ -106,7 +110,7 @@ impl PgLTree {
{
let mut ltree = Self::default();
for label in labels {
ltree.push(String::from(label).parse()?);
ltree.push(PgLTreeLabel::new(label)?);
}
Ok(ltree)
}
Expand Down

0 comments on commit bb556d7

Please sign in to comment.