@@ -529,7 +529,7 @@ impl Config {
529529 . map_err ( |e| Error :: LoadConfigFile ( e. to_string ( ) ) ) ?;
530530
531531 let config = toml:: Deserializer :: parse ( content. as_ref ( ) )
532- . map_err ( |e| Error :: Parse ( e . to_string ( ) ) ) ?;
532+ . map_err ( |e| Error :: Parse ( ParseError :: new ( & content , & e ) ) ) ?;
533533
534534 let Configuration {
535535 theme,
@@ -560,7 +560,7 @@ impl Config {
560560 } = serde_ignored:: deserialize ( config, |ignored| {
561561 log:: warn!( "[config.toml] Ignoring unknown setting: {ignored}" ) ;
562562 } )
563- . map_err ( |e| Error :: Parse ( e . to_string ( ) ) ) ?;
563+ . map_err ( |e| Error :: Parse ( ParseError :: new ( & content , & e ) ) ) ?;
564564
565565 keyboard. validate ( ) ?;
566566
@@ -790,6 +790,32 @@ pub fn random_nickname_with_seed<R: Rng>(rng: &mut R) -> String {
790790 rand_nick
791791}
792792
793+ #[ derive( Debug , Clone ) ]
794+ pub struct ParseError {
795+ /// Short description of the problem, e.g. `invalid basic string`.
796+ pub message : String ,
797+ /// Full rendered error, including a snippet of the offending TOML.
798+ pub details : String ,
799+ /// Zero-indexed line of the parse error in the config file.
800+ pub line : Option < usize > ,
801+ }
802+
803+ impl ParseError {
804+ fn new ( content : & str , error : & toml:: de:: Error ) -> Self {
805+ let line = error. span ( ) . map ( |span| {
806+ content[ ..span. start . min ( content. len ( ) ) ]
807+ . matches ( '\n' )
808+ . count ( )
809+ } ) ;
810+
811+ Self {
812+ message : error. message ( ) . to_owned ( ) ,
813+ details : error. to_string ( ) ,
814+ line,
815+ }
816+ }
817+ }
818+
793819#[ derive( Debug , Error , Clone ) ]
794820pub enum Error {
795821 #[ error( "config could not be read: {0}" ) ]
@@ -798,8 +824,8 @@ pub enum Error {
798824 ExecutePasswordCommand ( String ) ,
799825 #[ error( "{0}" ) ]
800826 Io ( String ) ,
801- #[ error( "{0}" ) ]
802- Parse ( String ) ,
827+ #[ error( "{}" , . 0 . details ) ]
828+ Parse ( ParseError ) ,
803829 #[ error( "UTF8 parsing error: {0}" ) ]
804830 StrUtf8Error ( #[ from] str:: Utf8Error ) ,
805831 #[ error( "UTF8 parsing error: {0}" ) ]
0 commit comments