@@ -62,84 +62,132 @@ impl Termination for Error {
6262#[ derive( Serialize ) ]
6363struct ItemOutput {
6464 label : String ,
65- secret : String ,
66- created_at : String ,
67- modified_at : String ,
65+ /// Those are None if the item is locked
66+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
67+ secret : Option < String > ,
68+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
69+ created_at : Option < String > ,
70+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
71+ modified_at : Option < String > ,
6872 #[ serde( skip_serializing_if = "Option::is_none" ) ]
6973 schema : Option < String > ,
7074 #[ serde( skip_serializing_if = "Option::is_none" ) ]
7175 content_type : Option < String > ,
72- attributes : HashMap < String , String > ,
76+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
77+ attributes : Option < HashMap < String , String > > ,
78+ is_locked : bool ,
7379}
7480
7581impl ItemOutput {
7682 fn new (
77- secret : & oo7:: Secret ,
83+ secret : Option < & oo7:: Secret > ,
7884 label : & str ,
79- mut attributes : HashMap < String , String > ,
80- created : Duration ,
81- modified : Duration ,
85+ mut attributes : Option < HashMap < String , String > > ,
86+ created : Option < Duration > ,
87+ modified : Option < Duration > ,
88+ is_locked : bool ,
8289 as_hex : bool ,
8390 ) -> Self {
84- let bytes = secret. as_bytes ( ) ;
8591 let local_offset = UtcOffset :: current_local_offset ( ) . unwrap_or ( UtcOffset :: UTC ) ;
8692
87- let created = OffsetDateTime :: from_unix_timestamp ( created. as_secs ( ) as i64 )
88- . unwrap ( )
89- . to_offset ( local_offset) ;
90- let modified = OffsetDateTime :: from_unix_timestamp ( modified. as_secs ( ) as i64 )
91- . unwrap ( )
92- . to_offset ( local_offset) ;
93+ let created = created. map ( |created| {
94+ OffsetDateTime :: from_unix_timestamp ( created. as_secs ( ) as i64 )
95+ . unwrap ( )
96+ . to_offset ( local_offset)
97+ } ) ;
98+ let modified = modified. map ( |modified| {
99+ OffsetDateTime :: from_unix_timestamp ( modified. as_secs ( ) as i64 )
100+ . unwrap ( )
101+ . to_offset ( local_offset)
102+ } ) ;
93103
94104 let format = time:: format_description:: parse_borrowed :: < 2 > (
95105 "[year]-[month]-[day] [hour]:[minute]:[second]" ,
96106 )
97107 . unwrap ( ) ;
98108
99- let secret_str = if as_hex {
100- hex:: encode ( bytes)
101- } else {
102- match std:: str:: from_utf8 ( bytes) {
103- Ok ( s) => s. to_string ( ) ,
104- Err ( _) => hex:: encode ( bytes) ,
109+ let secret_str = secret. map ( |s| {
110+ let bytes = s. as_bytes ( ) ;
111+ if as_hex {
112+ hex:: encode ( bytes)
113+ } else {
114+ match std:: str:: from_utf8 ( bytes) {
115+ Ok ( s) => s. to_string ( ) ,
116+ Err ( _) => hex:: encode ( bytes) ,
117+ }
105118 }
106- } ;
119+ } ) ;
107120
108- let schema = attributes. remove ( oo7:: XDG_SCHEMA_ATTRIBUTE ) ;
109- let content_type = attributes. remove ( oo7:: CONTENT_TYPE_ATTRIBUTE ) ;
121+ let schema = attributes
122+ . as_mut ( )
123+ . map ( |attrs| attrs. remove ( oo7:: XDG_SCHEMA_ATTRIBUTE ) )
124+ . flatten ( ) ;
125+ let content_type = attributes
126+ . as_mut ( )
127+ . map ( |attrs| attrs. remove ( oo7:: CONTENT_TYPE_ATTRIBUTE ) )
128+ . flatten ( ) ;
110129
111130 Self {
112131 label : label. to_string ( ) ,
113132 secret : secret_str,
114- created_at : created. format ( & format) . unwrap ( ) ,
115- modified_at : modified. format ( & format) . unwrap ( ) ,
133+ created_at : created. map ( |created| created . format ( & format) . unwrap ( ) ) ,
134+ modified_at : modified. map ( |modified| modified . format ( & format) . unwrap ( ) ) ,
116135 schema,
117136 content_type,
118137 attributes,
138+ is_locked,
119139 }
120140 }
121141
122142 fn from_file_item ( item : & oo7:: file:: UnlockedItem , as_hex : bool ) -> Self {
123143 Self :: new (
124- & item. secret ( ) ,
144+ Some ( & item. secret ( ) ) ,
125145 item. label ( ) ,
126- item. attributes ( )
127- . iter ( )
128- . map ( |( k, v) | ( k. to_string ( ) , v. to_string ( ) ) )
129- . collect ( ) ,
130- item. created ( ) ,
131- item. modified ( ) ,
146+ Some (
147+ item. attributes ( )
148+ . iter ( )
149+ . map ( |( k, v) | ( k. to_string ( ) , v. to_string ( ) ) )
150+ . collect ( ) ,
151+ ) ,
152+ Some ( item. created ( ) ) ,
153+ Some ( item. modified ( ) ) ,
154+ false ,
132155 as_hex,
133156 )
134157 }
135158
136159 async fn from_dbus_item ( item : & oo7:: dbus:: Item , as_hex : bool ) -> Result < Self , Error > {
160+ use oo7:: dbus:: ServiceError ;
161+
162+ let is_locked = item. is_locked ( ) . await ?;
163+ let secret = match item. secret ( ) . await {
164+ Ok ( secret) => Ok ( Some ( secret) ) ,
165+ Err ( oo7:: dbus:: Error :: Service ( ServiceError :: IsLocked ( _) ) ) => Ok ( None ) ,
166+ Err ( e) => Err ( e) ,
167+ } ?;
168+ let attributes = match item. attributes ( ) . await {
169+ Ok ( attributes) => Ok ( Some ( attributes) ) ,
170+ Err ( oo7:: dbus:: Error :: Service ( ServiceError :: IsLocked ( _) ) ) => Ok ( None ) ,
171+ Err ( e) => Err ( e) ,
172+ } ?;
173+ let created = match item. created ( ) . await {
174+ Ok ( created) => Ok ( Some ( created) ) ,
175+ Err ( oo7:: dbus:: Error :: Service ( ServiceError :: IsLocked ( _) ) ) => Ok ( None ) ,
176+ Err ( e) => Err ( e) ,
177+ } ?;
178+ let modified = match item. modified ( ) . await {
179+ Ok ( modified) => Ok ( Some ( modified) ) ,
180+ Err ( oo7:: dbus:: Error :: Service ( ServiceError :: IsLocked ( _) ) ) => Ok ( None ) ,
181+ Err ( e) => Err ( e) ,
182+ } ?;
183+
137184 Ok ( Self :: new (
138- & item . secret ( ) . await ? ,
185+ secret. as_ref ( ) ,
139186 & item. label ( ) . await ?,
140- item. attributes ( ) . await ?,
141- item. created ( ) . await ?,
142- item. modified ( ) . await ?,
187+ attributes,
188+ created,
189+ modified,
190+ is_locked,
143191 as_hex,
144192 ) )
145193 }
@@ -148,16 +196,29 @@ impl ItemOutput {
148196impl fmt:: Display for ItemOutput {
149197 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
150198 writeln ! ( f, "[{}]" , self . label) ?;
151- writeln ! ( f, "secret = {}" , self . secret) ?;
152- writeln ! ( f, "created = {}" , self . created_at) ?;
153- writeln ! ( f, "modified = {}" , self . modified_at) ?;
199+ if let Some ( ref secret) = self . secret {
200+ writeln ! ( f, "secret = {}" , secret) ?;
201+ }
202+ if let Some ( ref created_at) = self . created_at {
203+ writeln ! ( f, "created = {}" , created_at) ?;
204+ }
205+ if let Some ( ref modified_at) = self . modified_at {
206+ writeln ! ( f, "modified = {}" , modified_at) ?;
207+ }
154208 if let Some ( schema) = & self . schema {
155209 writeln ! ( f, "schema = {schema}" ) ?;
156210 }
157211 if let Some ( content_type) = & self . content_type {
158212 writeln ! ( f, "content_type = {content_type}" ) ?;
159213 }
160- writeln ! ( f, "attributes = {:?}" , self . attributes) ?;
214+ if let Some ( attributes) = & self . attributes {
215+ writeln ! ( f, "attributes = {:?}" , attributes) ?;
216+ }
217+ if self . is_locked {
218+ writeln ! ( f, "locked = true" ) ?;
219+ } else {
220+ writeln ! ( f, "locked = false" ) ?;
221+ }
161222 Ok ( ( ) )
162223 }
163224}
0 commit comments