1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
pub( crate ) mod private
{
use crate::*;
use ca::
{
Parser, Verifier,
Executor,
ProgramParser,
Command,
grammar::command::private::CommandFormer,
help::{ HelpGeneratorFn, HelpVariants, dot_command },
};
use std::collections::HashSet;
use std::fmt;
use wtools::thiserror;
use wtools::error::
{
Result,
for_app::Error as wError,
for_lib::*,
};
/// Validation errors that can occur in application.
#[ derive( Error, Debug ) ]
pub enum ValidationError
{
/// This variant is used to represent parser errors.
/// It carries a `String` payload that provides additional information about the error.
#[ error( "The following input is not recognized: `{input}`.\nDetails: {error}" ) ]
Parser
{
/// source of the program
input : String,
/// original error
error : wError,
},
/// This variant represents errors that occur during grammar conversion.
#[ error( "Can not identify a command.\nDetails: {0}" ) ]
Verifier( wError ),
/// This variant is used to represent errors that occur during executor conversion.
#[ error( "Can not find a routine for a command.\nDetails: {0}" ) ]
ExecutorConverter( wError ),
}
/// Errors that can occur in application.
#[ derive( Error, Debug ) ]
pub enum Error
{
/// This variant is used to represent validation errors.
/// It carries a `ValidationError` payload that provides additional information about the error.
#[ error( "Validation error. {0}" ) ]
Validation( ValidationError ),
/// This variant represents execution errors.
#[ error( "Execution failed. {0:?}" ) ]
Execution( wError ),
}
// xxx : qqq : qqq2 : for Bohdan : one level is obviously redundant
// Program< Namespace< ExecutableCommand_ > > -> Program< ExecutableCommand_ >
// aaa : done. The concept of `Namespace` has been removed
struct CommandsAggregatorCallback( Box< dyn Fn( &str, &Program< VerifiedCommand > ) > );
impl fmt::Debug for CommandsAggregatorCallback
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
f.debug_struct( "CommandsAggregatorCallback" ).finish_non_exhaustive()
}
}
/// The `CommandsAggregator` struct is responsible for aggregating all commands that the user defines,
/// and for parsing and executing them. It is the main entry point of the library.
///
/// CommandsAggregator component brings everything together. This component is responsible for configuring the `Parser`, `Grammar`, and `Executor` components based on the user’s needs. It also manages the entire pipeline of processing, from parsing the raw text input to executing the final command(parse -> validate -> execute).
///
/// # Example:
///
/// ```
/// use wca::{ CommandsAggregator, Args, Props, Type };
///
/// # fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let ca = CommandsAggregator::former()
/// .command( "echo" )
/// .hint( "prints all subjects and properties" )
/// .subject().hint( "argument" ).kind( Type::String ).optional( false ).end()
/// .property( "property" ).hint( "simple property" ).kind( Type::String ).optional( false ).end()
/// .routine( | args : Args, props : Props | println!( "= Args\n{args:?}\n\n= Properties\n{props:?}\n" ) )
/// .end()
/// .perform();
///
/// ca.perform( ".echo something" )?;
/// # Ok( () ) }
/// ```
#[ derive( Debug ) ]
#[ derive( former::Former ) ]
#[ perform( fn build() -> CommandsAggregator ) ]
pub struct CommandsAggregator
{
#[ default( Dictionary::default() ) ]
dictionary : Dictionary,
#[ default( Parser::former().form() ) ]
parser : Parser,
#[ setter( false ) ]
#[ default( Executor::former().form() ) ]
executor : Executor,
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
// #[ default( Verifier::former().form() ) ]
#[ default( Verifier ) ]
verifier : Verifier,
// #[ default( ExecutorConverter::former().form() ) ]
// executor_converter : ExecutorConverter,
callback_fn : Option< CommandsAggregatorCallback >,
}
impl< Context, End > CommandsAggregatorFormer< Context, End >
where
End : former::ToSuperFormer< CommandsAggregator, Context >,
{
/// Creates a command in the command chain.
///
/// # Arguments
///
/// * `name` - The name of the command.
pub fn command< IntoName >( self, name : IntoName ) -> CommandFormer< Self, impl former::ToSuperFormer< Command, Self > >
where
IntoName : Into< String >,
{
let on_end = | command : Command, super_former : Option< Self > | -> Self
{
let mut super_former = super_former.unwrap();
let mut dictionary = super_former.container.dictionary.unwrap_or_default();
dictionary.register( command );
super_former.container.dictionary = Some( dictionary );
super_former
};
let former = CommandFormer::begin( Some( self ), on_end );
former.phrase( name )
}
}
impl CommandsAggregatorFormer
{
// qqq : delete on completion
// /// Setter for grammar
// ///
// /// Gets list of available commands
// pub fn grammar< V >( mut self, commands : V ) -> Self
// where
// V : Into< Vec< Command > >
// {
// let verifier = Verifier::former()
// .commands( commands )
// .form();
// self.container.verifier = Some( verifier );
// self
// }
// /// Setter for executor
// ///
// /// Gets dictionary of routines( command name -> callback )
// pub fn executor< H >( mut self, routines : H ) -> Self
// where
// H : Into< HashMap< String, Routine > >
// {
// let executor = ExecutorConverter::former()
// .routines( routines )
// .form();
//
// self.container.executor_converter = Some( executor );
// 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
///
/// ```
/// use wca::CommandsAggregator;
///
/// # fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let ca = CommandsAggregator::former()
/// // ...
/// .callback( | _input, _program | println!( "Program is valid" ) )
/// .perform();
///
/// // prints the "Program is valid" and after executes the program
/// ca.perform( ".help" )?;
/// # Ok( () ) }
/// ```
pub fn callback< Callback >( mut self, callback : Callback ) -> Self
where
Callback : Fn( &str, &Program< VerifiedCommand > ) + 'static,
{
self.container.callback_fn = Some( CommandsAggregatorCallback( Box::new( callback ) ) );
self
}
}
impl CommandsAggregator
{
/// Construct CommandsAggregator
fn build( self ) -> CommandsAggregator
{
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 );
ca
}
/// Parse, converts and executes a program
///
/// Takes a string with program and executes it
pub fn perform< S >( &self, program : S ) -> Result< (), Error >
where
S : IntoInput
{
let Input( ref program ) = program.into_input();
let raw_program = self.parser.program( program ).map_err( | e | Error::Validation( ValidationError::Parser { input : program.to_string(), error : e } ) )?;
let grammar_program = self.verifier.to_program( &self.dictionary, raw_program ).map_err( | e | Error::Validation( ValidationError::Verifier( e ) ) )?;
// let exec_program = self.executor_converter.to_program( grammar_program ).map_err( | e | Error::Validation( ValidationError::ExecutorConverter( e ) ) )?;
if let Some( callback ) = &self.callback_fn
{
callback.0( program, &grammar_program )
}
self.executor.program( &self.dictionary, grammar_program ).map_err( | e | Error::Execution( e ) )
}
}
}
//
crate::mod_interface!
{
exposed use CommandsAggregator;
exposed use CommandsAggregatorFormer;
exposed use Error;
exposed use ValidationError;
}