Skip to content

Commit b6594fd

Browse files
committed
Implemented binary search for column
1 parent 552e733 commit b6594fd

3 files changed

Lines changed: 5 additions & 16 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
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
@@ -1,6 +1,6 @@
11
[package]
22
name = "sql_docs"
3-
version = "1.0.6"
3+
version = "1.0.7"
44
edition = "2024"
55
description = "A crate for parsing comments from sql files and using them for documentation generation"
66
documentation = "https://docs.rs/sql_docs"

src/docs.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl ColumnDoc {
2121
/// - name: `String` - the name of the column
2222
/// - doc: `Option<String>` the comment for the column
2323
#[must_use]
24-
pub const fn new(name: String, doc: Option<String>) -> Self {
24+
pub fn new(name: String, doc: Option<String>) -> Self {
2525
Self { name, doc }
2626
}
2727

@@ -133,19 +133,8 @@ impl TableDoc {
133133
///
134134
/// # Errors
135135
/// - Will return [`DocError::ColumnNotFound`] if the expected table is not found
136-
pub fn column(&self, column: &str) -> Result<&ColumnDoc, DocError> {
137-
let matches = self
138-
.columns
139-
.iter()
140-
.filter(|col_doc| col_doc.name() == column)
141-
.collect::<Vec<&ColumnDoc>>();
142-
match matches.as_slice() {
143-
[] => Err(DocError::ColumnNotFound { name: column.to_owned() }),
144-
[only] => Ok(*only),
145-
_ => Err(DocError::DuplicateColumnsFound {
146-
columns: matches.into_iter().cloned().collect(),
147-
}),
148-
}
136+
pub fn column(&self, name: &str) -> Result<&ColumnDoc, DocError> {
137+
self.columns().binary_search_by(|c| c.name().cmp(name)).map_or_else(|_| Err(DocError::ColumnNotFound { name: name.to_owned() }), |id| Ok(&self.columns()[id]))
149138
}
150139

151140
/// Getter method for retrieving the table's [`Path`]

0 commit comments

Comments
 (0)