refactor(src/macros/generate/host): Merge host generation in one module
This commit is contained in:
parent
4d76282b99
commit
0f131c957f
|
@ -1,75 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2023 - 2024:
|
|
||||||
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
|
||||||
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
||||||
*
|
|
||||||
* This file is part of the Trixy crate for Trinitrix.
|
|
||||||
*
|
|
||||||
* Trixy is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the Lesser GNU General Public License as
|
|
||||||
* published by the Free Software Foundation, either version 3 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* and the Lesser GNU General Public License along with this program.
|
|
||||||
* If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
|
||||||
use quote::quote;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
macros::config::trixy::TrixyConfig,
|
|
||||||
parser::command_spec::{CommandSpec, Enumeration, Structure},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// This function generates the main c API provided by Trixy.
|
|
||||||
/// This works for example like this:
|
|
||||||
/// Turning this:
|
|
||||||
/// ```text
|
|
||||||
/// nasp trinitrix {
|
|
||||||
/// struct Callback {
|
|
||||||
/// func: String,
|
|
||||||
/// timeout: String,
|
|
||||||
/// };
|
|
||||||
///
|
|
||||||
/// enum CallbackPriority {
|
|
||||||
/// High,
|
|
||||||
/// Medium,
|
|
||||||
/// Low,
|
|
||||||
/// };
|
|
||||||
///
|
|
||||||
/// fn execute_callback(callback: Callback, priority: CallbackPriority);
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
/// to this:
|
|
||||||
/// ```no_run
|
|
||||||
/// pub extern "C" fn exectute_callback(callback: Callback, priority: CallbackPriority) {
|
|
||||||
/// /* Here we simply call your handler function, with the command of the function */
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> TokenStream2 {
|
|
||||||
let functions: TokenStream2 = trixy
|
|
||||||
.functions
|
|
||||||
.iter()
|
|
||||||
.map(|r#fn| r#fn.to_c(&config, &vec![]))
|
|
||||||
.collect();
|
|
||||||
let namespaced_functions: TokenStream2 = trixy
|
|
||||||
.namespaces
|
|
||||||
.iter()
|
|
||||||
.map(|nasp| nasp.to_c(&config, &vec![]))
|
|
||||||
.collect();
|
|
||||||
let structures: TokenStream2 = trixy.structures.iter().map(Structure::to_c).collect();
|
|
||||||
let enumerations: TokenStream2 = trixy.enumerations.iter().map(Enumeration::to_c).collect();
|
|
||||||
quote! {
|
|
||||||
#enumerations
|
|
||||||
#structures
|
|
||||||
#functions
|
|
||||||
#namespaced_functions
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -22,13 +22,14 @@
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
macros::{config::trixy::TrixyConfig, generate::host::format_rust},
|
macros::{config::trixy::TrixyConfig, generate::host::format_rust},
|
||||||
parser::command_spec::CommandSpec,
|
parser::command_spec::{CommandSpec, Enumeration, Structure},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod host;
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
|
pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
|
||||||
let host_rust_code = host::generate(&trixy, &config);
|
let host_rust_code = generate_code(&trixy, &config);
|
||||||
|
|
||||||
let rust_code = format_rust(host_rust_code);
|
let rust_code = format_rust(host_rust_code);
|
||||||
|
|
||||||
|
@ -39,3 +40,48 @@ pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
|
||||||
rust_code
|
rust_code
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
/// This function generates the main c API provided by Trixy.
|
||||||
|
/// This works for example like this:
|
||||||
|
/// Turning this:
|
||||||
|
/// ```text
|
||||||
|
/// nasp trinitrix {
|
||||||
|
/// struct Callback {
|
||||||
|
/// func: String,
|
||||||
|
/// timeout: String,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// enum CallbackPriority {
|
||||||
|
/// High,
|
||||||
|
/// Medium,
|
||||||
|
/// Low,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn execute_callback(callback: Callback, priority: CallbackPriority);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
/// to this:
|
||||||
|
/// ```no_run
|
||||||
|
/// pub extern "C" fn exectute_callback(callback: Callback, priority: CallbackPriority) {
|
||||||
|
/// /* Here we simply call your handler function, with the command of the function */
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn generate_code(trixy: &CommandSpec, config: &TrixyConfig) -> TokenStream2 {
|
||||||
|
let functions: TokenStream2 = trixy
|
||||||
|
.functions
|
||||||
|
.iter()
|
||||||
|
.map(|r#fn| r#fn.to_c(&config, &vec![]))
|
||||||
|
.collect();
|
||||||
|
let namespaced_functions: TokenStream2 = trixy
|
||||||
|
.namespaces
|
||||||
|
.iter()
|
||||||
|
.map(|nasp| nasp.to_c(&config, &vec![]))
|
||||||
|
.collect();
|
||||||
|
let structures: TokenStream2 = trixy.structures.iter().map(Structure::to_c).collect();
|
||||||
|
let enumerations: TokenStream2 = trixy.enumerations.iter().map(Enumeration::to_c).collect();
|
||||||
|
quote! {
|
||||||
|
#enumerations
|
||||||
|
#structures
|
||||||
|
#functions
|
||||||
|
#namespaced_functions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,115 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2023 - 2024:
|
|
||||||
* The Trinitrix Project <soispha@vhack.eu, antifallobst@systemausfall.org>
|
|
||||||
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
||||||
*
|
|
||||||
* This file is part of the Trixy crate for Trinitrix.
|
|
||||||
*
|
|
||||||
* Trixy is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the Lesser GNU General Public License as
|
|
||||||
* published by the Free Software Foundation, either version 3 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* and the Lesser GNU General Public License along with this program.
|
|
||||||
* If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//! This module is responsible for generating the rust code used to interface with the api.
|
|
||||||
//! That includes the structs and enums declared in the trixy file and the enum used to describe the
|
|
||||||
//! command being executed.
|
|
||||||
|
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
|
||||||
use quote::quote;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
macros::config::trixy::TrixyConfig,
|
|
||||||
parser::command_spec::{CommandSpec, Enumeration, Namespace, Structure},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// This function turns, for example, the following trixy input into this rust code:
|
|
||||||
/// ```text
|
|
||||||
/// nasp trinitrix {
|
|
||||||
/// struct Callback {
|
|
||||||
/// func: String,
|
|
||||||
/// timeout: String,
|
|
||||||
/// };
|
|
||||||
///
|
|
||||||
/// enum CallbackPriority {
|
|
||||||
/// High,
|
|
||||||
/// Medium,
|
|
||||||
/// Low,
|
|
||||||
/// };
|
|
||||||
///
|
|
||||||
/// fn execute_callback(callback: Callback, priority: CallbackPriority) -> String;
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
/// ```no_run
|
|
||||||
/// #[derive(Debug)]
|
|
||||||
/// pub enum Commands {
|
|
||||||
/// Trinitrix(trinitrix::Trinitrix),
|
|
||||||
/// }
|
|
||||||
/// pub mod trinitrix {
|
|
||||||
/// #[allow(non_camel_case_types)]
|
|
||||||
/// #[derive(Debug)]
|
|
||||||
/// struct Callback {
|
|
||||||
/// func: String,
|
|
||||||
/// timeout: String,
|
|
||||||
/// }
|
|
||||||
/// #[allow(non_camel_case_types)]
|
|
||||||
/// #[derive(Debug)]
|
|
||||||
/// enum CallbackPriority {
|
|
||||||
/// High,
|
|
||||||
/// Medium,
|
|
||||||
/// Low,
|
|
||||||
/// }
|
|
||||||
/// #[derive(Debug)]
|
|
||||||
/// pub enum Trinitrix {
|
|
||||||
/// #[allow(non_camel_case_types)]
|
|
||||||
/// execute_callback {
|
|
||||||
/// callback: Callback,
|
|
||||||
/// priority: CallbackPriority,
|
|
||||||
/// trixy_output: trixy::oneshot::channel<String>
|
|
||||||
/// },
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub fn generate(trixy: &CommandSpec, _config: &TrixyConfig) -> TokenStream2 {
|
|
||||||
let modules: TokenStream2 = trixy
|
|
||||||
.namespaces
|
|
||||||
.iter()
|
|
||||||
.map(|nasp| nasp.to_rust_module(&vec![]))
|
|
||||||
.collect();
|
|
||||||
let structures: TokenStream2 = trixy.structures.iter().map(Structure::to_rust).collect();
|
|
||||||
let enumerations: TokenStream2 = trixy
|
|
||||||
.enumerations
|
|
||||||
.iter()
|
|
||||||
.map(Enumeration::to_rust)
|
|
||||||
.collect();
|
|
||||||
let functions: Vec<TokenStream2> = trixy
|
|
||||||
.functions
|
|
||||||
.iter()
|
|
||||||
.map(|r#fn| r#fn.to_rust(&[]))
|
|
||||||
.collect();
|
|
||||||
let namespace_modules: Vec<TokenStream2> = trixy
|
|
||||||
.namespaces
|
|
||||||
.iter()
|
|
||||||
.map(Namespace::to_rust_module_enum)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
#structures
|
|
||||||
#enumerations
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Commands {
|
|
||||||
#(#functions,)*
|
|
||||||
#(#namespace_modules),*
|
|
||||||
}
|
|
||||||
#modules
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -20,14 +20,20 @@
|
||||||
* If not, see <https://www.gnu.org/licenses/>.
|
* If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::parser::command_spec::CommandSpec;
|
//! This module is responsible for generating the rust code used to interface with the api.
|
||||||
|
//! That includes the structs and enums declared in the trixy file and the enum used to describe the
|
||||||
|
//! command being executed.
|
||||||
|
|
||||||
use crate::macros::{config::trixy::TrixyConfig, generate::host::format_rust};
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
pub mod host;
|
use crate::{
|
||||||
|
macros::{config::trixy::TrixyConfig, generate::host::format_rust},
|
||||||
|
parser::command_spec::{CommandSpec, Enumeration, Namespace, Structure},
|
||||||
|
};
|
||||||
|
|
||||||
pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
|
pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
|
||||||
let host_rust_code = host::generate(&trixy, &config);
|
let host_rust_code = generate_code(&trixy, &config);
|
||||||
|
|
||||||
let rust_code = format_rust(host_rust_code);
|
let rust_code = format_rust(host_rust_code);
|
||||||
|
|
||||||
|
@ -38,3 +44,85 @@ pub fn generate(trixy: &CommandSpec, config: &TrixyConfig) -> String {
|
||||||
rust_code
|
rust_code
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This function turns, for example, the following trixy input into this rust code:
|
||||||
|
/// ```text
|
||||||
|
/// mod trinitrix {
|
||||||
|
/// struct Callback {
|
||||||
|
/// func: String,
|
||||||
|
/// timeout: String,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// enum CallbackPriority {
|
||||||
|
/// High,
|
||||||
|
/// Medium,
|
||||||
|
/// Low,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// fn execute_callback(callback: Callback, priority: CallbackPriority) -> String;
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
/// ```no_run
|
||||||
|
/// #[derive(Debug)]
|
||||||
|
/// pub enum Commands {
|
||||||
|
/// Trinitrix(trinitrix::Trinitrix),
|
||||||
|
/// }
|
||||||
|
/// pub mod trinitrix {
|
||||||
|
/// #[allow(non_camel_case_types)]
|
||||||
|
/// #[derive(Debug)]
|
||||||
|
/// struct Callback {
|
||||||
|
/// func: String,
|
||||||
|
/// timeout: String,
|
||||||
|
/// }
|
||||||
|
/// #[allow(non_camel_case_types)]
|
||||||
|
/// #[derive(Debug)]
|
||||||
|
/// enum CallbackPriority {
|
||||||
|
/// High,
|
||||||
|
/// Medium,
|
||||||
|
/// Low,
|
||||||
|
/// }
|
||||||
|
/// #[derive(Debug)]
|
||||||
|
/// pub enum Trinitrix {
|
||||||
|
/// #[allow(non_camel_case_types)]
|
||||||
|
/// execute_callback {
|
||||||
|
/// callback: Callback,
|
||||||
|
/// priority: CallbackPriority,
|
||||||
|
/// trixy_output: trixy::oneshot::channel<String>
|
||||||
|
/// },
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn generate_code(trixy: &CommandSpec, _config: &TrixyConfig) -> TokenStream2 {
|
||||||
|
let modules: TokenStream2 = trixy
|
||||||
|
.namespaces
|
||||||
|
.iter()
|
||||||
|
.map(|nasp| nasp.to_rust_module(&vec![]))
|
||||||
|
.collect();
|
||||||
|
let structures: TokenStream2 = trixy.structures.iter().map(Structure::to_rust).collect();
|
||||||
|
let enumerations: TokenStream2 = trixy
|
||||||
|
.enumerations
|
||||||
|
.iter()
|
||||||
|
.map(Enumeration::to_rust)
|
||||||
|
.collect();
|
||||||
|
let functions: Vec<TokenStream2> = trixy
|
||||||
|
.functions
|
||||||
|
.iter()
|
||||||
|
.map(|r#fn| r#fn.to_rust(&[]))
|
||||||
|
.collect();
|
||||||
|
let namespace_modules: Vec<TokenStream2> = trixy
|
||||||
|
.namespaces
|
||||||
|
.iter()
|
||||||
|
.map(Namespace::to_rust_module_enum)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#structures
|
||||||
|
#enumerations
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Commands {
|
||||||
|
#(#functions,)*
|
||||||
|
#(#namespace_modules),*
|
||||||
|
}
|
||||||
|
#modules
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue