Skip to content

Commit e90a240

Browse files
committed
Cleaned up use of to_string
1 parent a00197e commit e90a240

6 files changed

Lines changed: 121 additions & 136 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.1"
3+
version = "1.0.2"
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/comments.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Comment {
143143
}
144144

145145
/// Enum for returning errors withe Comment parsing
146-
#[derive(Debug)]
146+
#[derive(Clone, Debug, Eq, PartialEq)]
147147
pub enum CommentError {
148148
/// Found a multiline comment terminator `*/` without a matching opener `/*`
149149
UnmatchedMultilineCommentStart {
@@ -294,8 +294,8 @@ impl Comments {
294294
.lines()
295295
.enumerate()
296296
.map(|(i, line)| match i {
297-
0 => line.trim().to_string(),
298-
_ => "\n".to_string() + line.trim(),
297+
0 => line.trim().to_owned(),
298+
_ => "\n".to_owned() + line.trim(),
299299
})
300300
.collect();
301301
comments.push(Comment::new(
@@ -326,7 +326,7 @@ impl Comments {
326326
in_single = false;
327327
let end_loc = Location::new(line_num, col);
328328
comments.push(Comment::new(
329-
CommentKind::SingleLine(buf.trim().to_string()),
329+
CommentKind::SingleLine(buf.trim().to_owned()),
330330
Span::new(Location { line: start_line, column: start_col }, end_loc),
331331
));
332332
buf.clear();
@@ -398,7 +398,7 @@ mod tests {
398398
let raw_comment = "-- a comment";
399399
let len = raw_comment.len() as u64;
400400

401-
let singleline = CommentKind::SingleLine(raw_comment.to_string());
401+
let singleline = CommentKind::SingleLine(raw_comment.to_owned());
402402
let mut span = Span::default();
403403
span.end.column = len - 1;
404404

@@ -414,7 +414,7 @@ mod tests {
414414

415415
#[test]
416416
fn multiline_comment_span() {
417-
let kind = CommentKind::MultiLine("/* hello world */".to_string());
417+
let kind = CommentKind::MultiLine("/* hello world */".to_owned());
418418
let span = Span::new(Location { line: 1, column: 1 }, Location { line: 2, column: 9 });
419419

420420
let comment = Comment::new(kind.clone(), span);
@@ -760,11 +760,11 @@ CREATE TABLE posts (
760760
fn test_comments() {
761761
let comment_vec = vec![
762762
Comment::new(
763-
CommentKind::SingleLine("a comment".to_string()),
763+
CommentKind::SingleLine("a comment".to_owned()),
764764
Span { start: Location::new(1, 1), end: Location::new(1, 12) },
765765
),
766766
Comment::new(
767-
CommentKind::SingleLine("a second comment".to_string()),
767+
CommentKind::SingleLine("a second comment".to_owned()),
768768
Span { start: Location::new(1, 1), end: Location::new(2, 19) },
769769
),
770770
];

src/docs.rs

Lines changed: 72 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl SqlFileDoc {
200200
let column_name = column.name.value.clone();
201201
let column_doc = match column_leading {
202202
Some(col_comment) => {
203-
ColumnDoc::new(column_name, Some(col_comment.text().to_string()))
203+
ColumnDoc::new(column_name, Some(col_comment.text().to_owned()))
204204
}
205205
None => ColumnDoc::new(column_name, None),
206206
};
@@ -211,7 +211,7 @@ impl SqlFileDoc {
211211
let table_doc = TableDoc::new(
212212
schema,
213213
name,
214-
table_leading.as_ref().map(|c| c.text().to_string()),
214+
table_leading.as_ref().map(|c| c.text().to_owned()),
215215
column_docs,
216216
file.path_into_path_buf(),
217217
);
@@ -280,7 +280,7 @@ fn schema_and_table(name: &ObjectName) -> Result<(Option<String>, String), DocEr
280280
[] => {
281281
let span = name.span();
282282
Err(DocError::InvalidObjectName {
283-
message: "ObjectName had no identifier parts".to_string(),
283+
message: "ObjectName had no identifier parts".to_owned(),
284284
line: span.start.line,
285285
column: span.start.column,
286286
})
@@ -307,12 +307,12 @@ mod tests {
307307

308308
#[test]
309309
fn test_sql_docs_struct() {
310-
let column_doc = ColumnDoc::new("id".to_string(), Some("The ID for the table".to_string()));
310+
let column_doc = ColumnDoc::new("id".to_owned(), Some("The ID for the table".to_owned()));
311311
let columns = vec![column_doc];
312312
let table_doc = TableDoc::new(
313313
None,
314-
"user".to_string(),
315-
Some("The table for users".to_string()),
314+
"user".to_owned(),
315+
Some("The table for users".to_owned()),
316316
columns,
317317
None,
318318
);
@@ -511,26 +511,26 @@ CREATE TABLE posts (
511511
SqlFileDoc::new(vec![
512512
TableDoc::new(
513513
None,
514-
"users".to_string(),
514+
"users".to_owned(),
515515
None,
516516
vec![
517-
ColumnDoc::new("id".to_string(), None),
518-
ColumnDoc::new("username".to_string(), None),
519-
ColumnDoc::new("email".to_string(), None),
520-
ColumnDoc::new("created_at".to_string(), None),
517+
ColumnDoc::new("id".to_owned(), None),
518+
ColumnDoc::new("username".to_owned(), None),
519+
ColumnDoc::new("email".to_owned(), None),
520+
ColumnDoc::new("created_at".to_owned(), None),
521521
],
522522
None,
523523
),
524524
TableDoc::new(
525525
None,
526-
"posts".to_string(),
526+
"posts".to_owned(),
527527
None,
528528
vec![
529-
ColumnDoc::new("id".to_string(), None),
530-
ColumnDoc::new("title".to_string(), None),
531-
ColumnDoc::new("user_id".to_string(), None),
532-
ColumnDoc::new("body".to_string(), None),
533-
ColumnDoc::new("published_at".to_string(), None),
529+
ColumnDoc::new("id".to_owned(), None),
530+
ColumnDoc::new("title".to_owned(), None),
531+
ColumnDoc::new("user_id".to_owned(), None),
532+
ColumnDoc::new("body".to_owned(), None),
533+
ColumnDoc::new("published_at".to_owned(), None),
534534
],
535535
None,
536536
),
@@ -543,34 +543,34 @@ CREATE TABLE posts (
543543
let first_docs = SqlFileDoc::new(vec![
544544
TableDoc::new(
545545
None,
546-
"users".to_string(),
547-
Some("Users table stores user account information".to_string()),
546+
"users".to_owned(),
547+
Some("Users table stores user account information".to_owned()),
548548
vec![
549-
ColumnDoc::new("id".to_string(), Some("Primary key".to_string())),
550-
ColumnDoc::new("username".to_string(), Some("Username for login".to_string())),
551-
ColumnDoc::new("email".to_string(), Some("Email address".to_string())),
549+
ColumnDoc::new("id".to_owned(), Some("Primary key".to_owned())),
550+
ColumnDoc::new("username".to_owned(), Some("Username for login".to_owned())),
551+
ColumnDoc::new("email".to_owned(), Some("Email address".to_owned())),
552552
ColumnDoc::new(
553-
"created_at".to_string(),
554-
Some("When the user registered".to_string()),
553+
"created_at".to_owned(),
554+
Some("When the user registered".to_owned()),
555555
),
556556
],
557557
None,
558558
),
559559
TableDoc::new(
560560
None,
561-
"posts".to_string(),
562-
Some("Posts table stores blog posts".to_string()),
561+
"posts".to_owned(),
562+
Some("Posts table stores blog posts".to_owned()),
563563
vec![
564-
ColumnDoc::new("id".to_string(), Some("Primary key".to_string())),
565-
ColumnDoc::new("title".to_string(), Some("Post title".to_string())),
564+
ColumnDoc::new("id".to_owned(), Some("Primary key".to_owned())),
565+
ColumnDoc::new("title".to_owned(), Some("Post title".to_owned())),
566566
ColumnDoc::new(
567-
"user_id".to_string(),
568-
Some("Foreign key linking to users".to_string()),
567+
"user_id".to_owned(),
568+
Some("Foreign key linking to users".to_owned()),
569569
),
570-
ColumnDoc::new("body".to_string(), Some("Main body text".to_string())),
570+
ColumnDoc::new("body".to_owned(), Some("Main body text".to_owned())),
571571
ColumnDoc::new(
572-
"published_at".to_string(),
573-
Some("When the post was created".to_string()),
572+
"published_at".to_owned(),
573+
Some("When the post was created".to_owned()),
574574
),
575575
],
576576
None,
@@ -581,43 +581,37 @@ CREATE TABLE posts (
581581
let second_docs = SqlFileDoc::new(vec![
582582
TableDoc::new(
583583
None,
584-
"users".to_string(),
585-
Some("Users table stores user account information\nmultiline".to_string()),
584+
"users".to_owned(),
585+
Some("Users table stores user account information\nmultiline".to_owned()),
586586
vec![
587-
ColumnDoc::new("id".to_string(), Some("Primary key\nmultiline".to_string())),
587+
ColumnDoc::new("id".to_owned(), Some("Primary key\nmultiline".to_owned())),
588588
ColumnDoc::new(
589-
"username".to_string(),
590-
Some("Username for login\nmultiline".to_string()),
589+
"username".to_owned(),
590+
Some("Username for login\nmultiline".to_owned()),
591591
),
592+
ColumnDoc::new("email".to_owned(), Some("Email address\nmultiline".to_owned())),
592593
ColumnDoc::new(
593-
"email".to_string(),
594-
Some("Email address\nmultiline".to_string()),
595-
),
596-
ColumnDoc::new(
597-
"created_at".to_string(),
598-
Some("When the user registered\nmultiline".to_string()),
594+
"created_at".to_owned(),
595+
Some("When the user registered\nmultiline".to_owned()),
599596
),
600597
],
601598
None,
602599
),
603600
TableDoc::new(
604601
None,
605-
"posts".to_string(),
606-
Some("Posts table stores blog posts\nmultiline".to_string()),
602+
"posts".to_owned(),
603+
Some("Posts table stores blog posts\nmultiline".to_owned()),
607604
vec![
608-
ColumnDoc::new("id".to_string(), Some("Primary key\nmultiline".to_string())),
609-
ColumnDoc::new("title".to_string(), Some("Post title\nmultiline".to_string())),
610-
ColumnDoc::new(
611-
"user_id".to_string(),
612-
Some("Foreign key linking to users\nmultiline".to_string()),
613-
),
605+
ColumnDoc::new("id".to_owned(), Some("Primary key\nmultiline".to_owned())),
606+
ColumnDoc::new("title".to_owned(), Some("Post title\nmultiline".to_owned())),
614607
ColumnDoc::new(
615-
"body".to_string(),
616-
Some("Main body text\nmultiline".to_string()),
608+
"user_id".to_owned(),
609+
Some("Foreign key linking to users\nmultiline".to_owned()),
617610
),
611+
ColumnDoc::new("body".to_owned(), Some("Main body text\nmultiline".to_owned())),
618612
ColumnDoc::new(
619-
"published_at".to_string(),
620-
Some("When the post was created\nmultiline".to_string()),
613+
"published_at".to_owned(),
614+
Some("When the post was created\nmultiline".to_owned()),
621615
),
622616
],
623617
None,
@@ -630,26 +624,26 @@ CREATE TABLE posts (
630624

631625
#[test]
632626
fn test_doc() {
633-
let col_doc = ColumnDoc::new("test".to_string(), Some("comment".to_string()));
634-
assert_eq!(&col_doc.to_string(), &"Column Name: test\nColumn Doc: comment\n".to_string());
635-
let col_doc_no_doc = ColumnDoc::new("test".to_string(), None);
627+
let col_doc = ColumnDoc::new("test".to_owned(), Some("comment".to_owned()));
628+
assert_eq!(&col_doc.to_string(), &"Column Name: test\nColumn Doc: comment\n".to_owned());
629+
let col_doc_no_doc = ColumnDoc::new("test".to_owned(), None);
636630
assert_eq!(
637631
&col_doc_no_doc.to_string(),
638-
&"Column Name: test\nNo Column Doc Found\n".to_string()
632+
&"Column Name: test\nNo Column Doc Found\n".to_owned()
639633
);
640634
assert_eq!(col_doc.doc(), Some("comment"));
641635
assert_eq!(col_doc.name(), "test");
642636
assert_eq!(col_doc_no_doc.doc(), None);
643637
assert_eq!(col_doc_no_doc.name(), "test");
644638
let table_doc = TableDoc::new(
645-
Some("schema".to_string()),
646-
"table".to_string(),
647-
Some("table doc".to_string()),
639+
Some("schema".to_owned()),
640+
"table".to_owned(),
641+
Some("table doc".to_owned()),
648642
vec![col_doc],
649643
None,
650644
);
651645
let table_doc_no_doc =
652-
TableDoc::new(None, "table".to_string(), None, vec![col_doc_no_doc], None);
646+
TableDoc::new(None, "table".to_owned(), None, vec![col_doc_no_doc], None);
653647
assert_eq!(table_doc.name(), "table");
654648
assert_eq!(table_doc.schema(), Some("schema"));
655649
assert_eq!(
@@ -665,7 +659,7 @@ CREATE TABLE posts (
665659
}
666660

667661
fn ident(v: &str) -> Ident {
668-
Ident { value: v.to_string(), quote_style: None, span: Span::empty() }
662+
Ident { value: v.to_owned(), quote_style: None, span: Span::empty() }
669663
}
670664

671665
fn func_part(name: &str) -> ObjectNamePart {
@@ -713,7 +707,7 @@ CREATE TABLE posts (
713707
]);
714708

715709
let (schema, table) = schema_and_table(&name)?;
716-
assert_eq!(schema, Some("public".to_string()));
710+
assert_eq!(schema, Some("public".to_owned()));
717711
assert_eq!(table, "orders");
718712
Ok(())
719713
}
@@ -785,7 +779,7 @@ CREATE TABLE posts (
785779

786780
#[test]
787781
fn column_doc_set_doc_updates_doc() {
788-
let mut col = ColumnDoc::new("id".to_string(), None);
782+
let mut col = ColumnDoc::new("id".to_owned(), None);
789783
assert_eq!(col.name(), "id");
790784
assert_eq!(col.doc(), None);
791785
col.set_doc("primary key");
@@ -797,7 +791,7 @@ CREATE TABLE posts (
797791

798792
#[test]
799793
fn table_doc_set_doc_updates_doc() {
800-
let mut table = TableDoc::new(None, "users".to_string(), None, Vec::new(), None);
794+
let mut table = TableDoc::new(None, "users".to_owned(), None, Vec::new(), None);
801795
assert_eq!(table.name(), "users");
802796
assert_eq!(table.schema(), None);
803797
assert_eq!(table.doc(), None);
@@ -811,11 +805,11 @@ CREATE TABLE posts (
811805
fn columns_mut_allows_mutating_column_docs() {
812806
let mut table = TableDoc::new(
813807
None,
814-
"users".to_string(),
808+
"users".to_owned(),
815809
None,
816810
vec![
817-
ColumnDoc::new("id".to_string(), None),
818-
ColumnDoc::new("username".to_string(), None),
811+
ColumnDoc::new("id".to_owned(), None),
812+
ColumnDoc::new("username".to_owned(), None),
819813
],
820814
None,
821815
);
@@ -837,16 +831,16 @@ CREATE TABLE posts (
837831
fn test_from_sql_file_doc_into_vec_table_doc_preserves_contents_and_order() {
838832
let t1 = TableDoc::new(
839833
None,
840-
"users".to_string(),
841-
Some("users doc".to_string()),
842-
vec![ColumnDoc::new("id".to_string(), Some("pk".to_string()))],
834+
"users".to_owned(),
835+
Some("users doc".to_owned()),
836+
vec![ColumnDoc::new("id".to_owned(), Some("pk".to_owned()))],
843837
None,
844838
);
845839
let t2 = TableDoc::new(
846-
Some("analytics".to_string()),
847-
"events".to_string(),
840+
Some("analytics".to_owned()),
841+
"events".to_owned(),
848842
None,
849-
vec![ColumnDoc::new("payload".to_string(), None)],
843+
vec![ColumnDoc::new("payload".to_owned(), None)],
850844
None,
851845
);
852846

@@ -858,7 +852,7 @@ CREATE TABLE posts (
858852
}
859853
#[test]
860854
fn table_doc_path_getter_returns_expected_value() {
861-
let mut table = TableDoc::new(None, "users".to_string(), None, Vec::new(), None);
855+
let mut table = TableDoc::new(None, "users".to_owned(), None, Vec::new(), None);
862856
assert_eq!(table.path(), None);
863857
let pb = PathBuf::from("some/dir/file.sql");
864858
table.set_path(Some(pb.clone()));

0 commit comments

Comments
 (0)