@@ -528,7 +528,7 @@ impl Config {
528528 . map_err ( |e| Error :: LoadConfigFile ( e. to_string ( ) ) ) ?;
529529
530530 let config = toml:: Deserializer :: parse ( content. as_ref ( ) )
531- . map_err ( |e| Error :: Parse ( e . to_string ( ) ) ) ?;
531+ . map_err ( |e| Error :: Parse ( ParseError :: new ( & content , & e ) ) ) ?;
532532
533533 let Configuration {
534534 theme,
@@ -559,7 +559,7 @@ impl Config {
559559 } = serde_ignored:: deserialize ( config, |ignored| {
560560 log:: warn!( "[config.toml] Ignoring unknown setting: {ignored}" ) ;
561561 } )
562- . map_err ( |e| Error :: Parse ( e . to_string ( ) ) ) ?;
562+ . map_err ( |e| Error :: Parse ( ParseError :: new ( & content , & e ) ) ) ?;
563563
564564 let servers = ServerMap :: new (
565565 servers,
@@ -787,6 +787,32 @@ pub fn random_nickname_with_seed<R: Rng>(rng: &mut R) -> String {
787787 rand_nick
788788}
789789
790+ #[ derive( Debug , Clone ) ]
791+ pub struct ParseError {
792+ /// Short description of the problem, e.g. `invalid basic string`.
793+ pub message : String ,
794+ /// Full rendered error, including a snippet of the offending TOML.
795+ pub details : String ,
796+ /// Zero-indexed line of the parse error in the config file.
797+ pub line : Option < usize > ,
798+ }
799+
800+ impl ParseError {
801+ fn new ( content : & str , error : & toml:: de:: Error ) -> Self {
802+ let line = error. span ( ) . map ( |span| {
803+ content[ ..span. start . min ( content. len ( ) ) ]
804+ . matches ( '\n' )
805+ . count ( )
806+ } ) ;
807+
808+ Self {
809+ message : error. message ( ) . to_owned ( ) ,
810+ details : error. to_string ( ) ,
811+ line,
812+ }
813+ }
814+ }
815+
790816#[ derive( Debug , Error , Clone ) ]
791817pub enum Error {
792818 #[ error( "config could not be read: {0}" ) ]
@@ -795,8 +821,8 @@ pub enum Error {
795821 ExecutePasswordCommand ( String ) ,
796822 #[ error( "{0}" ) ]
797823 Io ( String ) ,
798- #[ error( "{0}" ) ]
799- Parse ( String ) ,
824+ #[ error( "{}" , . 0 . details ) ]
825+ Parse ( ParseError ) ,
800826 #[ error( "UTF8 parsing error: {0}" ) ]
801827 StrUtf8Error ( #[ from] str:: Utf8Error ) ,
802828 #[ error( "UTF8 parsing error: {0}" ) ]
0 commit comments