-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make PgLTree::push infallible and take PgLTreeLabel directly. #1734
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
String: From<S>, | ||
{ | ||
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<I, S>(labels: I) -> Result<Self, PgLTreeParseError> | ||
where | ||
S: Into<String>, | ||
String: From<S>, | ||
I: IntoIterator<Item = S>, | ||
{ | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unfortunately a breaking change, though, and will need to wait for 0.6.0. I don't have an exact timeline on that right now but I expect it to be the next release we do, probably in a month or so. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the info! The work-around is not too terrible anyways There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You could add a new |
||
self.labels.push(label); | ||
} | ||
|
||
/// pop a label from ltree | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're calling
parse()
on it, we don't need to copy it to an owned string. I would make thisS: AsRef<str>
and then dolabel.as_ref().parse()
in thefor
loop.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need owned
String
s inside thePgLTreeLabel
anyways, I changedPgLTreeLabel::new
to takeS: Into<String>
and changed the call in here fromparse()?
toPgLTreeLabel::new
.This saves copies when the caller doesn't need to own the string but leaves
&str
valid!