11//! Error types returned by this crate’s public APIs.
22
3- use crate :: { comments:: CommentError , docs:: TableDoc } ;
3+ use crate :: {
4+ comments:: CommentError ,
5+ docs:: { ColumnDoc , TableDoc } ,
6+ } ;
47use core:: fmt;
58use sqlparser:: parser:: ParserError ;
69use std:: { error, fmt:: Debug } ;
@@ -29,11 +32,21 @@ pub enum DocError {
2932 /// The name of the table that was not found
3033 name : String ,
3134 } ,
35+ /// Column not found when searching [`crate::docs::TableDoc`]
36+ ColumnNotFound {
37+ /// The name of the column not found
38+ name : String ,
39+ } ,
3240 /// Duplicate tables with same name were found when searching [`crate::SqlDoc`]
3341 DuplicateTablesFound {
3442 /// `Vec` of the [`TableDoc`] for each duplicate table found
3543 tables : Vec < TableDoc > ,
3644 } ,
45+ /// Duplicate columns with same name were found when searching a single [`TableDoc`]
46+ DuplicateColumnsFound {
47+ /// `Vec` of the [`crate::docs::ColumnDoc`] for each duplicate table found
48+ columns : Vec < ColumnDoc > ,
49+ } ,
3750}
3851
3952impl fmt:: Display for DocError {
@@ -48,13 +61,21 @@ impl fmt::Display for DocError {
4861 write ! ( f, "{message} at line {line}, column {column}" )
4962 }
5063 Self :: TableNotFound { name } => write ! ( f, "Table not found in SqlDoc: {name}" ) ,
64+ Self :: ColumnNotFound { name } => write ! ( f, "Column not found in TableDoc: {name}" ) ,
5165 Self :: DuplicateTablesFound { tables } => {
5266 writeln ! ( f, "Duplicate tables found:" ) ?;
5367 for t in tables {
5468 writeln ! ( f, "{t}" ) ?;
5569 }
5670 Ok ( ( ) )
5771 }
72+ Self :: DuplicateColumnsFound { columns } => {
73+ writeln ! ( f, "Duplicate columns found:" ) ?;
74+ for t in columns {
75+ writeln ! ( f, "{t}" ) ?;
76+ }
77+ Ok ( ( ) )
78+ }
5879 }
5980 }
6081}
@@ -67,7 +88,9 @@ impl error::Error for DocError {
6788 Self :: SqlParserError ( e) => Some ( e) ,
6889 Self :: InvalidObjectName { .. }
6990 | Self :: TableNotFound { .. }
70- | Self :: DuplicateTablesFound { .. } => None ,
91+ | Self :: ColumnNotFound { .. }
92+ | Self :: DuplicateTablesFound { .. }
93+ | Self :: DuplicateColumnsFound { .. } => None ,
7194 }
7295 }
7396}
@@ -222,4 +245,34 @@ mod tests {
222245 let dup = DocError :: DuplicateTablesFound { tables : vec ! [ ] } ;
223246 assert ! ( dup. source( ) . is_none( ) ) ;
224247 }
248+
249+ #[ test]
250+ fn test_doc_error_display_column_not_found ( ) {
251+ let e = DocError :: ColumnNotFound { name : "id" . to_string ( ) } ;
252+ assert_eq ! ( e. to_string( ) , "Column not found in TableDoc: id" ) ;
253+ }
254+
255+ #[ test]
256+ fn test_doc_error_display_duplicate_columns_found ( ) {
257+ use crate :: docs:: ColumnDoc ;
258+
259+ let c1 = ColumnDoc :: new ( "dup_col" . to_string ( ) , None ) ;
260+ let c2 = ColumnDoc :: new ( "dup_col" . to_string ( ) , None ) ;
261+ let e = DocError :: DuplicateColumnsFound { columns : vec ! [ c1, c2] } ;
262+
263+ let s = e. to_string ( ) ;
264+ assert ! ( s. contains( "Duplicate columns found:" ) , "output was: {s}" ) ;
265+ assert ! ( s. contains( "dup_col" ) , "output was: {s}" ) ;
266+ }
267+
268+ #[ test]
269+ fn test_doc_error_source_none_for_column_variants ( ) {
270+ use std:: error:: Error as _;
271+
272+ let not_found = DocError :: ColumnNotFound { name : "x" . to_string ( ) } ;
273+ assert ! ( not_found. source( ) . is_none( ) ) ;
274+
275+ let dup = DocError :: DuplicateColumnsFound { columns : vec ! [ ] } ;
276+ assert ! ( dup. source( ) . is_none( ) ) ;
277+ }
225278}
0 commit comments