1- use crate :: Token ;
2- use anyhow:: { Context , Result } ;
1+ use crate :: { LangsError , Token } ;
32use serde:: { Deserialize , Serialize } ;
4- use std:: fs;
53use std:: ops:: Deref ;
64use std:: path:: PathBuf ;
7- use tmc_langs_util:: file_util :: { self , create_file_lock } ;
5+ use tmc_langs_util:: { file_util , FileError } ;
86
97#[ derive( Debug , Serialize , Deserialize ) ]
108pub struct Credentials {
@@ -14,7 +12,7 @@ pub struct Credentials {
1412
1513impl Credentials {
1614 // path to the credentials file
17- fn get_credentials_path ( client_name : & str ) -> Result < PathBuf > {
15+ fn get_credentials_path ( client_name : & str ) -> Result < PathBuf , LangsError > {
1816 super :: get_tmc_dir ( client_name) . map ( |dir| dir. join ( "credentials.json" ) )
1917 }
2018
@@ -24,19 +22,16 @@ impl Credentials {
2422 /// - Err if a credentials file exists but cannot be deserialized.
2523 ///
2624 /// On Err, the file is deleted.
27- pub fn load ( client_name : & str ) -> Result < Option < Self > > {
25+ pub fn load ( client_name : & str ) -> Result < Option < Self > , LangsError > {
2826 let credentials_path = Self :: get_credentials_path ( client_name) ?;
2927 if !credentials_path. exists ( ) {
3028 return Ok ( None ) ;
3129 }
3230
3331 let mut credentials_file = file_util:: open_file_lock ( & credentials_path) ?;
34- let guard = credentials_file. lock ( ) . with_context ( || {
35- format ! (
36- "Failed to lock credentials file at {}" ,
37- credentials_path. display( )
38- )
39- } ) ?;
32+ let guard = credentials_file
33+ . lock ( )
34+ . map_err ( |e| FileError :: FdLock ( credentials_path. clone ( ) , e) ) ?;
4035
4136 match serde_json:: from_reader ( guard. deref ( ) ) {
4237 Ok ( token) => Ok ( Some ( Credentials {
@@ -48,55 +43,36 @@ impl Credentials {
4843 "Failed to deserialize credentials.json due to \" {}\" , deleting" ,
4944 e
5045 ) ;
51- fs:: remove_file ( & credentials_path) . with_context ( || {
52- format ! (
53- "Failed to remove malformed credentials.json file {}" ,
54- credentials_path. display( )
55- )
56- } ) ?;
57- anyhow:: bail!(
58- "Failed to deserialize credentials file at {}; removed the file, please try again." ,
59- credentials_path. display( )
60- )
46+ file_util:: remove_file ( & credentials_path) ?;
47+ Err ( LangsError :: DeserializeCredentials ( credentials_path, e) )
6148 }
6249 }
6350 }
6451
65- pub fn save ( client_name : & str , token : Token ) -> Result < ( ) > {
52+ pub fn save ( client_name : & str , token : Token ) -> Result < ( ) , LangsError > {
6653 let credentials_path = Self :: get_credentials_path ( client_name) ?;
6754
6855 if let Some ( p) = credentials_path. parent ( ) {
69- fs:: create_dir_all ( p)
70- . with_context ( || format ! ( "Failed to create directory {}" , p. display( ) ) ) ?;
56+ file_util:: create_dir_all ( p) ?;
7157 }
72- let mut credentials_file = create_file_lock ( & credentials_path)
73- . with_context ( || format ! ( "Failed to create file at {}" , credentials_path. display( ) ) ) ?;
74- let guard = credentials_file. lock ( ) ?;
58+ let mut credentials_file = file_util:: create_file_lock ( & credentials_path) ?;
59+ let guard = credentials_file
60+ . lock ( )
61+ . map_err ( |e| FileError :: FdLock ( credentials_path. clone ( ) , e) ) ?;
7562
7663 // write token
7764 if let Err ( e) = serde_json:: to_writer ( guard. deref ( ) , & token) {
7865 // failed to write token, removing credentials file
79- fs:: remove_file ( & credentials_path) . with_context ( || {
80- format ! (
81- "Failed to remove empty credentials file after failing to write {}" ,
82- credentials_path. display( )
83- )
84- } ) ?;
85- Err ( e) . with_context ( || {
86- format ! (
87- "Failed to write credentials to {}" ,
88- credentials_path. display( )
89- )
90- } ) ?;
66+ file_util:: remove_file ( & credentials_path) ?;
67+ return Err ( LangsError :: Json ( e) ) ;
9168 }
9269 Ok ( ( ) )
9370 }
9471
95- pub fn remove ( self ) -> Result < ( ) > {
72+ pub fn remove ( self ) -> Result < ( ) , LangsError > {
9673 file_util:: lock!( & self . path) ;
9774
98- fs:: remove_file ( & self . path )
99- . with_context ( || format ! ( "Failed to remove credentials at {}" , self . path. display( ) ) ) ?;
75+ file_util:: remove_file ( & self . path ) ?;
10076 Ok ( ( ) )
10177 }
10278
0 commit comments