@@ -3,12 +3,10 @@ use std::rc::Rc;
33use tablepro_session:: runtime:: Tasks ;
44use uuid:: Uuid ;
55
6- use super :: StateFile ;
76use super :: column_layouts_document:: ColumnLayoutsDocument ;
7+ use super :: { StateFile , StateFileError , TableStateKey } ;
88use crate :: services:: grid:: ColumnLayout ;
99
10- /// Each table's column order, hidden columns and widths, remembered per
11- /// connection, schema and table.
1210#[ derive( Clone ) ]
1311pub struct ColumnLayoutStore {
1412 file : Rc < StateFile < ColumnLayoutsDocument > > ,
@@ -31,16 +29,12 @@ impl ColumnLayoutStore {
3129 self . file . path ( )
3230 }
3331
34- pub fn layout ( & self , connection_id : Uuid , schema : Option < & str > , table : & str ) -> ColumnLayout {
35- self . read ( connection_id, schema, table, |layout| {
36- layout. cloned ( ) . unwrap_or_default ( )
37- } )
32+ pub fn layout ( & self , key : impl Into < TableStateKey > , schema : Option < & str > , table : & str ) -> ColumnLayout {
33+ self . read ( & key. into ( ) , schema, table, |layout| layout. cloned ( ) . unwrap_or_default ( ) )
3834 }
3935
40- /// Keep `layout` as the table's whole layout. The grid merges what it
41- /// shows into the stored layout before it saves.
42- pub fn save ( & self , connection_id : Uuid , schema : Option < & str > , table : & str , layout : & ColumnLayout ) {
43- self . change ( connection_id, schema, table, |stored| {
36+ pub fn save ( & self , key : impl Into < TableStateKey > , schema : Option < & str > , table : & str , layout : & ColumnLayout ) {
37+ self . change ( & key. into ( ) , schema, table, |stored| {
4438 if stored == layout {
4539 return false ;
4640 }
@@ -49,20 +43,43 @@ impl ColumnLayoutStore {
4943 } ) ;
5044 }
5145
52- /// Drop everything for a connection the user deleted, so the file
53- /// does not grow with entries nothing can reach.
5446 pub fn forget_connection ( & self , connection_id : Uuid ) {
55- self . file
56- . update ( |document| document. connections . remove ( & connection_id. to_string ( ) ) . is_some ( ) ) ;
47+ self . file . update ( |document| {
48+ let before = document. connections . len ( ) ;
49+ document
50+ . connections
51+ . retain ( |key, _| !TableStateKey :: belongs_to ( key, connection_id) ) ;
52+ before != document. connections . len ( )
53+ } ) ;
54+ }
55+
56+ pub fn failing ( & self ) -> tokio:: sync:: watch:: Receiver < bool > {
57+ self . file . failing ( )
58+ }
59+
60+ pub fn revision ( & self ) -> u64 {
61+ self . file . revision ( )
62+ }
63+
64+ pub fn has_unsaved_changes ( & self ) -> bool {
65+ self . file . has_unsaved_changes ( )
66+ }
67+
68+ pub fn current_error ( & self ) -> Option < StateFileError > {
69+ self . file . current_error ( )
70+ }
71+
72+ pub fn retry ( & self ) -> bool {
73+ self . file . retry ( )
5774 }
5875
59- pub fn flush ( & self ) -> impl Future < Output = ( ) > + Send + use <> {
76+ pub fn flush ( & self ) -> impl Future < Output = Result < ( ) , StateFileError > > + Send + use <> {
6077 self . file . flush ( )
6178 }
6279
6380 fn read < R > (
6481 & self ,
65- connection_id : Uuid ,
82+ key : & TableStateKey ,
6683 schema : Option < & str > ,
6784 table : & str ,
6885 with : impl FnOnce ( Option < & ColumnLayout > ) -> R ,
@@ -71,33 +88,30 @@ impl ColumnLayoutStore {
7188 with (
7289 document
7390 . connections
74- . get ( & connection_id . to_string ( ) )
91+ . get ( key . as_str ( ) )
7592 . and_then ( |schemas| schemas. get ( schema_key ( schema) ) )
7693 . and_then ( |tables| tables. get ( table) ) ,
7794 )
7895 } )
7996 }
8097
81- /// Change one table's layout. `change` returns whether anything moved.
82- /// A layout left with nothing in it is removed with any map it empties,
83- /// so a reset shrinks the file back.
8498 fn change (
8599 & self ,
86- connection_id : Uuid ,
100+ key : & TableStateKey ,
87101 schema : Option < & str > ,
88102 table : & str ,
89103 change : impl FnOnce ( & mut ColumnLayout ) -> bool ,
90104 ) {
91- let mut layout = self . layout ( connection_id , schema, table) ;
105+ let mut layout = self . layout ( key , schema, table) ;
92106 if !change ( & mut layout) {
93107 return ;
94108 }
95109 self . file . update ( |document| match layout == ColumnLayout :: default ( ) {
96- true => prune ( document, connection_id , schema, table) ,
110+ true => prune ( document, key , schema, table) ,
97111 false => {
98112 document
99113 . connections
100- . entry ( connection_id . to_string ( ) )
114+ . entry ( key . as_str ( ) . to_owned ( ) )
101115 . or_default ( )
102116 . entry ( schema_key ( schema) . to_owned ( ) )
103117 . or_default ( )
@@ -108,14 +122,13 @@ impl ColumnLayoutStore {
108122 }
109123}
110124
111- /// `None` is stored as the empty string so every JSON key is concrete.
112125fn schema_key ( schema : Option < & str > ) -> & str {
113126 schema. unwrap_or ( "" )
114127}
115128
116- fn prune ( document : & mut ColumnLayoutsDocument , connection_id : Uuid , schema : Option < & str > , table : & str ) -> bool {
117- let connection_key = connection_id . to_string ( ) ;
118- let Some ( schemas) = document. connections . get_mut ( & connection_key) else {
129+ fn prune ( document : & mut ColumnLayoutsDocument , key : & TableStateKey , schema : Option < & str > , table : & str ) -> bool {
130+ let connection_key = key . as_str ( ) ;
131+ let Some ( schemas) = document. connections . get_mut ( connection_key) else {
119132 return false ;
120133 } ;
121134 let mut removed = false ;
@@ -126,7 +139,7 @@ fn prune(document: &mut ColumnLayoutsDocument, connection_id: Uuid, schema: Opti
126139 }
127140 }
128141 if schemas. is_empty ( ) {
129- document. connections . remove ( & connection_key) ;
142+ document. connections . remove ( connection_key) ;
130143 }
131144 removed
132145}
@@ -160,7 +173,7 @@ mod tests {
160173 let store = store_at ( & path) ;
161174
162175 store. save ( id, Some ( "public" ) , "users" , & arranged ( ) ) ;
163- store. flush ( ) . await ;
176+ store. flush ( ) . await . expect ( "saved" ) ;
164177 let reopened = store_at ( & path) ;
165178
166179 assert_eq ! ( reopened. layout( id, Some ( "public" ) , "users" ) , arranged( ) ) ;
@@ -203,7 +216,7 @@ mod tests {
203216 store. save ( id, Some ( "public" ) , "users" , & arranged ( ) ) ;
204217
205218 store. save ( id, Some ( "public" ) , "users" , & ColumnLayout :: default ( ) ) ;
206- store. flush ( ) . await ;
219+ store. flush ( ) . await . expect ( "saved" ) ;
207220
208221 assert ! (
209222 !read_text_file( & path) . contains( & id. to_string( ) ) ,
0 commit comments