From babd353c2c861e2c0b7677e9c36cffd981c62d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20P=C3=BCtz?= Date: Fri, 15 Apr 2022 01:16:00 +0200 Subject: [PATCH] Make PgLTree::push infallible and take PgLTreeLabel directly. (#1734) * Make PgLTree::push infallible and take PgLTreeLabel directly. Previously the function took strings and parsed them into PgLTreeLabel internally, now it's possible to directlry push PgLTreeLabels onto a PgLTree. * Push PgLTree String conversion to label. * rebase and fix compile error Co-authored-by: Austin Bonander --- sqlx-core/src/postgres/types/ltree.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sqlx-core/src/postgres/types/ltree.rs b/sqlx-core/src/postgres/types/ltree.rs index 0e9d7768..e6ef2d16 100644 --- a/sqlx-core/src/postgres/types/ltree.rs +++ b/sqlx-core/src/postgres/types/ltree.rs @@ -27,13 +27,17 @@ pub enum PgLTreeParseError { pub struct PgLTreeLabel(String); impl PgLTreeLabel { - pub fn new(label: &str) -> Result { + pub fn new(label: S) -> Result + where + String: From, + { + 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) } @@ -101,20 +105,19 @@ impl PgLTree { /// creates ltree from an iterator with checking labels pub fn from_iter(labels: I) -> Result where - S: Into, + String: From, I: IntoIterator, { let mut ltree = Self::default(); for label in labels { - ltree.push(&label.into())?; + ltree.push(PgLTreeLabel::new(label)?); } Ok(ltree) } /// push a label to ltree - pub fn push(&mut self, label: &str) -> Result<(), PgLTreeParseError> { - self.labels.push(PgLTreeLabel::new(label)?); - Ok(()) + pub fn push(&mut self, label: PgLTreeLabel) { + self.labels.push(label); } /// pop a label from ltree