11use crate :: { AsDoc , CommentTag , Comments , Deployment , Markdown , writer:: traits:: ParamLike } ;
22use itertools:: Itertools ;
3- use solang_parser:: pt:: { ErrorParameter , EventParameter , Parameter , VariableDeclaration } ;
3+ use solang_parser:: pt:: {
4+ EnumDefinition , ErrorParameter , EventParameter , Parameter , VariableDeclaration ,
5+ } ;
46use std:: {
57 fmt:: { self , Display , Write } ,
68 sync:: LazyLock ,
@@ -19,6 +21,11 @@ const DEPLOYMENTS_TABLE_HEADERS: &[&str] = &["Network", "Address"];
1921static DEPLOYMENTS_TABLE_SEPARATOR : LazyLock < String > =
2022 LazyLock :: new ( || DEPLOYMENTS_TABLE_HEADERS . iter ( ) . map ( |h| "-" . repeat ( h. len ( ) ) ) . join ( "|" ) ) ;
2123
24+ /// Headers and separator for rendering the variants table.
25+ const VARIANTS_TABLE_HEADERS : & [ & str ] = & [ "Name" , "Description" ] ;
26+ static VARIANTS_TABLE_SEPARATOR : LazyLock < String > =
27+ LazyLock :: new ( || VARIANTS_TABLE_HEADERS . iter ( ) . map ( |h| "-" . repeat ( h. len ( ) ) ) . join ( "|" ) ) ;
28+
2229/// The buffered writer.
2330/// Writes various display items into the internal buffer.
2431#[ derive( Debug , Default ) ]
@@ -177,6 +184,45 @@ impl BufWriter {
177184 self . try_write_table ( CommentTag :: Param , params, comments, "Properties" )
178185 }
179186
187+ /// Tries to write the variant table to the buffer.
188+ /// Doesn't write anything if either params or comments are empty.
189+ pub fn try_write_variant_table (
190+ & mut self ,
191+ params : & EnumDefinition ,
192+ comments : & Comments ,
193+ ) -> fmt:: Result {
194+ let comments = comments. include_tags ( & [ CommentTag :: Param ] ) ;
195+
196+ // There is nothing to write.
197+ if comments. is_empty ( ) {
198+ return Ok ( ( ) ) ;
199+ }
200+
201+ self . write_bold ( "Variants" ) ?;
202+ self . writeln ( ) ?;
203+
204+ self . write_piped ( & VARIANTS_TABLE_HEADERS . join ( "|" ) ) ?;
205+ self . write_piped ( & VARIANTS_TABLE_SEPARATOR ) ?;
206+
207+ for value in & params. values {
208+ let param_name = value. as_ref ( ) . map ( |v| v. name . clone ( ) ) ;
209+
210+ let comment = param_name. as_ref ( ) . and_then ( |name| {
211+ comments. iter ( ) . find_map ( |comment| comment. match_first_word ( name) )
212+ } ) ;
213+
214+ let row = [
215+ Markdown :: Code ( & param_name. unwrap_or ( "<none>" . to_string ( ) ) ) . as_doc ( ) ?,
216+ comment. unwrap_or_default ( ) . replace ( '\n' , " " ) ,
217+ ] ;
218+ self . write_piped ( & row. join ( "|" ) ) ?;
219+ }
220+
221+ self . writeln ( ) ?;
222+
223+ Ok ( ( ) )
224+ }
225+
180226 /// Tries to write the parameters table to the buffer.
181227 /// Doesn't write anything if either params or comments are empty.
182228 pub fn try_write_events_table (
0 commit comments