77//! - The semantic database format. (Internal format of this project.)
88//! - The execution event database format. (Internal format of this project.)
99
10- use super :: { json, FormatError } ;
10+ use super :: json;
1111use crate :: { intercept, semantic, semantic:: clang} ;
1212use serde_json:: de:: IoRead ;
1313use serde_json:: StreamDeserializer ;
14- use std :: io ;
14+ use thiserror :: Error ;
1515
1616/// The trait represents a file format that can be written to and read from.
1717///
1818/// The file format in this project is usually a sequence of values. This trait
1919/// provides a type-independent abstraction over the file format.
20- pub trait FileFormat < T > {
21- fn write ( _: impl io:: Write , _: impl Iterator < Item = T > ) -> Result < ( ) , FormatError > ;
20+ pub trait SerializationFormat < T > {
21+ fn write ( _: impl std :: io:: Write , _: impl Iterator < Item = T > ) -> Result < ( ) , SerializationError > ;
2222
23- fn read ( _: impl io:: Read ) -> impl Iterator < Item = Result < T , FormatError > > ;
23+ fn read ( _: impl std :: io:: Read ) -> impl Iterator < Item = Result < T , SerializationError > > ;
2424
2525 /// Reads the entries from the file and ignores any errors.
2626 /// This is not always feasible, when the file format is strict.
2727 fn read_and_ignore (
28- reader : impl io:: Read ,
28+ reader : impl std :: io:: Read ,
2929 message_writer : impl Fn ( & str ) ,
3030 ) -> impl Iterator < Item = T > {
3131 Self :: read ( reader) . filter_map ( move |result| match result {
@@ -38,6 +38,17 @@ pub trait FileFormat<T> {
3838 }
3939}
4040
41+ /// Represents errors that can occur while working with file formats.
42+ #[ derive( Debug , Error ) ]
43+ pub enum SerializationError {
44+ #[ error( "Generic IO error: {0}" ) ]
45+ Io ( #[ from] std:: io:: Error ) ,
46+ #[ error( "Format syntax error: {0}" ) ]
47+ Syntax ( #[ from] serde_json:: Error ) ,
48+ #[ error( "Format semantic error: {0}" ) ]
49+ Semantic ( #[ from] clang:: EntryError ) ,
50+ }
51+
4152/// The type represents a JSON compilation database format.
4253///
4354/// The format is a JSON array format, which is a sequence of JSON objects
@@ -49,28 +60,30 @@ pub trait FileFormat<T> {
4960/// https://clang.llvm.org/docs/JSONCompilationDatabase.html
5061pub struct JsonCompilationDatabase ;
5162
52- impl FileFormat < clang:: Entry > for JsonCompilationDatabase {
63+ impl SerializationFormat < clang:: Entry > for JsonCompilationDatabase {
5364 fn write (
54- writer : impl io:: Write ,
65+ writer : impl std :: io:: Write ,
5566 entries : impl Iterator < Item = clang:: Entry > ,
56- ) -> Result < ( ) , FormatError > {
67+ ) -> Result < ( ) , SerializationError > {
5768 json:: serialize_result_seq (
5869 writer,
5970 // Ensure only valid entries are serialized.
6071 entries. map ( |entry| match entry. validate ( ) {
6172 Ok ( _) => Ok ( entry) ,
62- Err ( err) => Err ( FormatError :: Semantic ( err) ) ,
73+ Err ( err) => Err ( SerializationError :: Semantic ( err) ) ,
6374 } ) ,
6475 )
6576 }
6677
67- fn read ( reader : impl io:: Read ) -> impl Iterator < Item = Result < clang:: Entry , FormatError > > {
78+ fn read (
79+ reader : impl std:: io:: Read ,
80+ ) -> impl Iterator < Item = Result < clang:: Entry , SerializationError > > {
6881 json:: deserialize_seq ( reader) . map ( |res| {
69- res. map_err ( FormatError :: Syntax )
82+ res. map_err ( SerializationError :: Syntax )
7083 // Ensure only valid entries are returned.
7184 . and_then ( |entry : clang:: Entry | match entry. validate ( ) {
7285 Ok ( _) => Ok ( entry) ,
73- Err ( err) => Err ( FormatError :: Semantic ( err) ) ,
86+ Err ( err) => Err ( SerializationError :: Semantic ( err) ) ,
7487 } )
7588 } )
7689 }
@@ -86,14 +99,16 @@ impl FileFormat<clang::Entry> for JsonCompilationDatabase {
8699/// The output format is not stable and may change in future versions.
87100pub struct JsonSemanticDatabase ;
88101
89- impl FileFormat < semantic:: Command > for JsonSemanticDatabase {
102+ impl SerializationFormat < semantic:: Command > for JsonSemanticDatabase {
90103 fn write (
91- writer : impl io:: Write ,
104+ writer : impl std :: io:: Write ,
92105 entries : impl Iterator < Item = semantic:: Command > ,
93- ) -> Result < ( ) , FormatError > {
94- json:: serialize_seq ( writer, entries) . map_err ( FormatError :: Syntax )
106+ ) -> Result < ( ) , SerializationError > {
107+ json:: serialize_seq ( writer, entries) . map_err ( SerializationError :: Syntax )
95108 }
96- fn read ( _: impl io:: Read ) -> impl Iterator < Item = Result < semantic:: Command , FormatError > > {
109+ fn read (
110+ _: impl std:: io:: Read ,
111+ ) -> impl Iterator < Item = Result < semantic:: Command , SerializationError > > {
97112 // Not implemented! (No reader for the semantic output in this project.)
98113 std:: iter:: empty ( )
99114 }
@@ -108,22 +123,24 @@ impl FileFormat<semantic::Command> for JsonSemanticDatabase {
108123/// The output format is not stable and may change in future versions.
109124pub struct ExecutionEventDatabase ;
110125
111- impl FileFormat < intercept:: Event > for ExecutionEventDatabase {
126+ impl SerializationFormat < intercept:: Event > for ExecutionEventDatabase {
112127 fn write (
113- writer : impl io:: Write ,
128+ writer : impl std :: io:: Write ,
114129 events : impl Iterator < Item = intercept:: Event > ,
115- ) -> Result < ( ) , FormatError > {
130+ ) -> Result < ( ) , SerializationError > {
116131 let mut writer = writer;
117132 for event in events {
118- serde_json:: to_writer ( & mut writer, & event) . map_err ( FormatError :: Syntax ) ?;
119- writer. write_all ( b"\n " ) . map_err ( FormatError :: Io ) ?;
133+ serde_json:: to_writer ( & mut writer, & event) . map_err ( SerializationError :: Syntax ) ?;
134+ writer. write_all ( b"\n " ) . map_err ( SerializationError :: Io ) ?;
120135 }
121136 Ok ( ( ) )
122137 }
123138
124- fn read ( reader : impl io:: Read ) -> impl Iterator < Item = Result < intercept:: Event , FormatError > > {
139+ fn read (
140+ reader : impl std:: io:: Read ,
141+ ) -> impl Iterator < Item = Result < intercept:: Event , SerializationError > > {
125142 let stream = StreamDeserializer :: new ( IoRead :: new ( reader) ) ;
126- stream. map ( |value| value. map_err ( FormatError :: Syntax ) )
143+ stream. map ( |value| value. map_err ( SerializationError :: Syntax ) )
127144 }
128145}
129146
@@ -132,15 +149,15 @@ mod test {
132149 mod compilation_database {
133150 use super :: super :: semantic:: clang:: { Entry , EntryError } ;
134151 use super :: super :: JsonCompilationDatabase as Sut ;
135- use super :: super :: { FileFormat , FormatError } ;
152+ use super :: super :: { SerializationError , SerializationFormat } ;
136153 use serde_json:: error:: Category ;
137154 use serde_json:: json;
138155 use std:: io:: { Cursor , Seek , SeekFrom } ;
139156
140157 macro_rules! assert_json_error {
141158 ( $x: expr) => {
142159 match $x {
143- Some ( Err ( FormatError :: Syntax ( error) ) ) => {
160+ Some ( Err ( SerializationError :: Syntax ( error) ) ) => {
144161 assert_eq!( error. classify( ) , Category :: Data )
145162 }
146163 _ => assert!( false , "shout be JSON error" ) ,
@@ -151,7 +168,7 @@ mod test {
151168 macro_rules! assert_format_error {
152169 ( $x: expr) => {
153170 assert!(
154- matches!( $x, Some ( Err ( FormatError :: Semantic ( _) ) ) ) ,
171+ matches!( $x, Some ( Err ( SerializationError :: Semantic ( _) ) ) ) ,
155172 "should be format error"
156173 ) ;
157174 } ;
@@ -226,7 +243,7 @@ mod test {
226243 let result = Sut :: write ( & mut buffer, vec ! [ entry] . into_iter ( ) ) ;
227244
228245 assert ! ( result. is_err( ) ) ;
229- assert ! ( matches!( result, Err ( FormatError :: Semantic ( _) ) ) ) ;
246+ assert ! ( matches!( result, Err ( SerializationError :: Semantic ( _) ) ) ) ;
230247 }
231248
232249 fn expected_values_with_arguments ( ) -> Vec < Entry > {
@@ -316,7 +333,7 @@ mod test {
316333 }
317334
318335 #[ test]
319- fn save_with_array_command_syntax ( ) -> Result < ( ) , FormatError > {
336+ fn save_with_array_command_syntax ( ) -> Result < ( ) , SerializationError > {
320337 let input = expected_values_with_arguments ( ) ;
321338
322339 // Create fake "file"
@@ -434,7 +451,7 @@ mod test {
434451 }
435452
436453 #[ test]
437- fn save_quoted_with_array_command_syntax ( ) -> Result < ( ) , FormatError > {
454+ fn save_quoted_with_array_command_syntax ( ) -> Result < ( ) , SerializationError > {
438455 let input = expected_quoted_values_with_argument ( ) ;
439456
440457 // Create fake "file"
@@ -454,7 +471,7 @@ mod test {
454471
455472 mod execution_events {
456473 use super :: super :: ExecutionEventDatabase as Sut ;
457- use super :: super :: FileFormat ;
474+ use super :: super :: SerializationFormat ;
458475 use crate :: intercept:: Event ;
459476 use serde_json:: json;
460477 use std:: collections:: HashMap ;
0 commit comments