Skip to content

Commit

Permalink
Implement HelpGenerator logic in aggregator module
Browse files Browse the repository at this point in the history
Revised the aggregator module to implement HelpGenerator functionality. This includes adding properties for the help_generator and help_variants, as well as creating corresponding setter functions. Additionally, code involving help content generation has been uncommented and updated to work with the current module structure.
  • Loading branch information
Barsik-sus committed Mar 11, 2024
1 parent 867fdf2 commit 0609960
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 275 deletions.
85 changes: 42 additions & 43 deletions module/move/wca/src/ca/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ pub( crate ) mod private
use crate::*;
use ca::
{
Parser, Verifier,// ExecutorConverter,
Parser, Verifier,
Executor,
ProgramParser,
Command,
grammar::command::private::CommandFormer,
// Routine,
// help::{ HelpGeneratorFn, HelpVariants, dot_command },
help::{ HelpGeneratorFn, HelpVariants, dot_command },
};

// use std::collections::{ HashMap, HashSet };
use std::collections::HashSet;
use std::fmt;
use wtools::thiserror;
use wtools::error::
Expand Down Expand Up @@ -119,9 +118,9 @@ pub( crate ) mod private
#[ default( Executor::former().form() ) ]
executor : Executor,

// help_generator : HelpGeneratorFn,
// #[ default( HashSet::from([ HelpVariants::All ]) ) ]
// help_variants : HashSet< HelpVariants >,
help_generator : HelpGeneratorFn,
#[ default( HashSet::from([ HelpVariants::All ]) ) ]
help_variants : HashSet< HelpVariants >,
// qqq : for Bohdan : should not have fields help_generator and help_variants
// help_generator generateds VerifiedCommand(s) and stop to exist

Expand Down Expand Up @@ -191,27 +190,27 @@ pub( crate ) mod private
// self
// }

// /// Setter for help content generator
// ///
// /// ```
// /// use wca::CommandsAggregator;
// ///
// /// # fn main() -> Result< (), Box< dyn std::error::Error > > {
// /// let ca = CommandsAggregator::former()
// /// // ...
// /// .help( | grammar, command | format!( "Replaced help content" ) )
// /// .perform();
// ///
// /// ca.perform( ".help" )?;
// /// # Ok( () ) }
// /// ```
// pub fn help< HelpFunction >( mut self, func : HelpFunction ) -> Self
// where
// HelpFunction : Fn( &Verifier, Option< &Command > ) -> String + 'static
// {
// self.container.help_generator = Some( HelpGeneratorFn::new( func ) );
// self
// }
/// Setter for help content generator
///
/// ```
/// use wca::CommandsAggregator;
///
/// # fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let ca = CommandsAggregator::former()
/// // ...
/// .help( | grammar, command | format!( "Replaced help content" ) )
/// .perform();
///
/// ca.perform( ".help" )?;
/// # Ok( () ) }
/// ```
pub fn help< HelpFunction >( mut self, func : HelpFunction ) -> Self
where
HelpFunction : Fn( &Dictionary, Option< &Command > ) -> String + 'static
{
self.container.help_generator = Some( HelpGeneratorFn::new( func ) );
self
}
// qqq : it is good access method, but formed structure should not have help_generator anymore

/// Set callback function that will be executed after validation state
Expand Down Expand Up @@ -243,23 +242,23 @@ pub( crate ) mod private
/// Construct CommandsAggregator
fn build( self ) -> CommandsAggregator
{
// let mut ca = self;
let mut ca = self;

// if ca.help_variants.contains( &HelpVariants::All )
// {
// HelpVariants::All.generate( &ca.help_generator, &mut ca.dictionary );
// }
// else
// {
// for help in &ca.help_variants
// {
// help.generate( &ca.help_generator, &mut ca.dictionary );
// }
// }
//
// dot_command( &mut ca.dictionary );
if ca.help_variants.contains( &HelpVariants::All )
{
HelpVariants::All.generate( &ca.help_generator, &mut ca.dictionary );
}
else
{
for help in &ca.help_variants
{
help.generate( &ca.help_generator, &mut ca.dictionary );
}
}

self
dot_command( &mut ca.dictionary );

ca
}

/// Parse, converts and executes a program
Expand Down
74 changes: 43 additions & 31 deletions module/move/wca/src/ca/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,22 @@ pub( crate ) mod private
Another,
}

pub fn md_generator( grammar : &Verifier ) -> String
pub fn md_generator( grammar : &Dictionary ) -> String
{
let text = grammar.commands
.iter()
.sorted_by_key( |( name, _ )| *name )
.map( |( name, cmd )|
{
cmd.iter().fold( String::new(), | _, cmd |
{

let subjects = cmd.subjects.iter().fold( String::new(), | _, _ | format!( " `[argument]`" ) );
let properties = if cmd.properties.is_empty() { " " } else { " `[properties]` " };
format!
(
"[.{name}{subjects}{properties}](#{}{}{})",
name.replace( '.', "" ),
if cmd.subjects.is_empty() { "" } else { "-argument" },
if cmd.properties.is_empty() { "" } else { "-properties" },
)
})
let subjects = cmd.subjects.iter().fold( String::new(), | _, _ | format!( " `[argument]`" ) );
let properties = if cmd.properties.is_empty() { " " } else { " `[properties]` " };
format!
(
"[.{name}{subjects}{properties}](#{}{}{})",
name.replace( '.', "" ),
if cmd.subjects.is_empty() { "" } else { "-argument" },
if cmd.properties.is_empty() { "" } else { "-properties" },
)
})
.fold( String::new(), | acc, cmd |
{
Expand All @@ -45,27 +41,43 @@ pub( crate ) mod private
.sorted_by_key( |( name, _ )| *name )
.map( |( name, cmd )|
{
cmd.iter().fold( String::new(), | _, cmd |
{
let subjects = cmd.subjects.iter().fold( String::new(), | _, _ | format!( " `[Subject]`" ) );
let properties = if cmd.properties.is_empty() { " " } else { " `[properties]` " };
let hint = if cmd.hint.is_empty() { &cmd.long_hint } else { &cmd.hint };
let subjects = cmd.subjects.iter().fold( String::new(), | _, _ | format!( " `[Subject]`" ) );
let properties = if cmd.properties.is_empty() { " " } else { " `[properties]` " };
let hint = if cmd.hint.is_empty() { &cmd.long_hint } else { &cmd.hint };

let heading = format!( "## .{name}{subjects}{properties}\n__{}__\n", hint );
let heading = format!( "## .{name}{subjects}{properties}\n__{}__\n", hint );

let hint = if cmd.long_hint.is_empty() { &cmd.hint } else { &cmd.long_hint };
let full_subjects = cmd.subjects.iter().enumerate().map( |( number, subj )| format!( "\n- {}subject_{number} - {} `[{:?}]`", if subj.optional { "`<optional>` " } else { "" }, subj.hint, subj.kind ) ).join( "\n" );
let full_properties = cmd.properties.iter().sorted_by_key( |( name, _ )| *name ).map( |( name, value )| format!( "\n- {}{name} - {} `[{:?}]`", if value.optional { "`<optional>` " } else { "" }, value.hint, value.kind ) ).join( "\n" );
// qqq : for Bohdan : toooooo log lines. 130 is max
let hint = if cmd.long_hint.is_empty() { &cmd.hint } else { &cmd.long_hint };
let full_subjects = cmd
.subjects
.iter()
.enumerate()
.map
(
|( number, subj )|
format!( "\n- {}subject_{number} - {} `[{:?}]`", if subj.optional { "`<optional>` " } else { "" }, subj.hint, subj.kind )
)
.join( "\n" );
let full_properties = cmd
.properties
.iter()
.sorted_by_key( |( name, _ )| *name )
.map
(
|( name, value )|
format!( "\n- {}{name} - {} `[{:?}]`", if value.optional { "`<optional>` " } else { "" }, value.hint, value.kind )
)
.join( "\n" );
// aaa : for Bohdan : toooooo log lines. 130 is max
// aaa : done.

format!
(
"{heading}\n{}{}\n\n{hint}\n",
if cmd.subjects.is_empty() { "".to_string() } else { format!( "\n\nSubjects:{}", &full_subjects ) },
if cmd.properties.is_empty() { "".to_string() } else { format!( "\n\nProperties:{}",&full_properties ) },
)
format!
(
"{heading}\n{}{}\n\n{hint}\n",
if cmd.subjects.is_empty() { "".to_string() } else { format!( "\n\nSubjects:{}", &full_subjects ) },
if cmd.properties.is_empty() { "".to_string() } else { format!( "\n\nProperties:{}",&full_properties ) },
)

})
})
.fold( String::new(), | acc, cmd |
{
Expand Down
6 changes: 3 additions & 3 deletions module/move/wca/src/ca/grammar/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub( crate ) mod private
/// A collection of commands.
///
/// This structure holds a hashmap of commands where each command is mapped to its name.
#[ derive( Debug, Default, Former ) ]
#[ derive( Debug, Default, Former, Clone ) ]
pub struct Dictionary
{
#[ setter( false ) ]
Expand Down Expand Up @@ -45,9 +45,9 @@ pub( crate ) mod private
/// # Arguments
///
/// * `command` - The command to be registered.
pub fn register( &mut self, command : Command )
pub fn register( &mut self, command : Command ) -> Option< Command >
{
self.commands.insert( command.phrase.clone(), command );
self.commands.insert( command.phrase.clone(), command )
}

/// Retrieves the command with the specified `name` from the `commands` hashmap.
Expand Down
Loading

0 comments on commit 0609960

Please sign in to comment.