Skip to content

Commit

Permalink
rust-extensions:0.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Nov 20, 2024
1 parent 7f8a840 commit cce1abf
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 69 deletions.
2 changes: 1 addition & 1 deletion external-dependencies/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "external-dependencies"
version = "0.6.3"
version = "0.6.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 2 additions & 2 deletions my-grpc-client-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "my-grpc-client-macros"
version = "0.6.3"
version = "0.6.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -14,7 +14,7 @@ with-ssh = []
proc-macro = true

[dependencies]
types-reader = { tag = "0.3.0", git = "https://github.com/MyJetTools/types-reader.git" }
types-reader = { tag = "0.5.1", git = "https://github.com/MyJetTools/types-reader.git" }

syn = { version = "*", features = ["extra-traits"] }
quote = "*"
Expand Down
29 changes: 11 additions & 18 deletions my-grpc-client-macros/src/grpc_client/fn_override.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
use std::collections::HashMap;

use proc_macro2::TokenStream;
use types_reader::ParamsList;
use types_reader::TokensObject;

pub struct FnOverride<'s> {
pub retries: usize,
pub token_stream: &'s TokenStream,
pub token_stream: &'s TokensObject,
}

impl<'s> FnOverride<'s> {
pub fn new(attributes: &'s ParamsList) -> Result<HashMap<String, Self>, syn::Error> {
let overrides = attributes.try_get_named_param("overrides");
pub fn new(params: &'s TokensObject) -> Result<HashMap<String, Self>, syn::Error> {
let overrides = params.try_get_named_param("overrides");

if overrides.is_none() {
return Ok(HashMap::new());
}

let tokens_list = overrides.unwrap();

let overrides = tokens_list.unwrap_as_object_list()?;
let overrides = overrides.unwrap().unwrap_as_vec()?;

let mut result = HashMap::new();

for item in overrides.iter() {
let name = item
.get_named_param("fn_name")?
.unwrap_as_string_value()?
.to_string();
let name: String = item.get_named_param("fn_name")?.try_into()?;

let retries: usize = item.get_named_param("retries")?.try_into()?;
result.insert(
name,
FnOverride {
retries: item
.get_named_param("retries")?
.unwrap_as_number_value()?
.as_usize(),
token_stream: item.get_token_stream(),
retries,
token_stream: item,
},
);
}

if result.len() == 0 {
return Err(tokens_list.throw_error("Overrides list can not be empty. Just remove field if you do not want to override any function"));
return Err(params.throw_error_at_param_token("Overrides list can not be empty. Just remove field if you do not want to override any function"));
}

Ok(result)
Expand Down
41 changes: 16 additions & 25 deletions my-grpc-client-macros/src/grpc_client/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::str::FromStr;

use proc_macro::TokenStream;
use types_reader::ParamsList;
use types_reader::TokensObject;


use crate::grpc_client::{fn_override::FnOverride, proto_file_reader::into_snake_case};
Expand All @@ -23,59 +23,50 @@ pub fn generate(



let attr_input: proc_macro2::TokenStream = attr.into();
let attr: proc_macro2::TokenStream = attr.into();


let attributes = ParamsList::new(attr_input, ||None)?;
let params_list = TokensObject::new(attr.into())?;

let timeout_sec = attributes.get_named_param("request_timeout_sec")?;
let timeout_sec = timeout_sec.unwrap_as_number_value()?.as_literal();


let ping_timeout_sec = attributes.get_named_param("ping_timeout_sec")?;
let ping_timeout_sec = ping_timeout_sec.unwrap_as_number_value()?.as_literal();
let proto_file:String = params_list.get_named_param("proto_file")?.try_into()?;


let ping_interval_sec = attributes.get_named_param("ping_interval_sec")?;
let ping_interval_sec = ping_interval_sec.unwrap_as_number_value()?.as_literal();

let proto_file = attributes.get_named_param("proto_file")?;
let proto_file = proto_file.unwrap_as_string_value()?.as_str();
let proto_file = ProtoServiceDescription::read_proto_file(proto_file);
let proto_file = ProtoServiceDescription::read_proto_file(&proto_file);

let grpc_service_name = &proto_file.service_name;
let grpc_service_name_token = proto_file.get_service_name_as_token();

let interfaces = super::generate_interfaces_implementations(struct_name, &proto_file);

let retries = attributes.get_named_param("retries")?;
let retries = retries.unwrap_as_number_value()?.as_usize();


let overrides = FnOverride::new(&attributes)?;
let overrides = FnOverride::new(&params_list)?;

let ping_timeout_sec:usize = params_list.get_named_param("ping_timeout_sec")?.try_into()?;
let ping_interval_sec:usize = params_list.get_named_param("ping_interval_sec")?.try_into()?;
let timeout_sec:usize = params_list.get_named_param("request_timeout_sec")?.try_into()?;
let retries:usize = params_list.get_named_param("retries")?.try_into()?;


let crate_ns = attributes.get_named_param("crate_ns")?.unwrap_as_string_value()?.as_str();
let crate_ns:String = params_list.get_named_param("crate_ns")?.try_into()?;
let mut use_name_spaces = Vec::new();
use_name_spaces.push(proc_macro2::TokenStream::from_str(format!("use {}::*", crate_ns).as_str()).unwrap());

let ns_of_client = format!("use {}::{}::{}", crate_ns,into_snake_case(&grpc_service_name), grpc_service_name);
use_name_spaces.push(proc_macro2::TokenStream::from_str(ns_of_client.as_str()).unwrap());


let settings_service_name = if let Some(service_name) = attributes.try_get_named_param("service_name"){
service_name.unwrap_as_string_value()?.as_str().to_string()

let settings_service_name = if let Some(service_name) = params_list.try_get_named_param("service_name"){
service_name.unwrap_as_value()?.as_string()?.to_string()
}else{
struct_name.to_string()
};

for (override_fn_name, fn_override) in &overrides{
if !proto_file.has_method(override_fn_name){
return Err(syn::Error::new_spanned(
fn_override.token_stream.clone(),
format!("Method {} is not found in proto file for service {}", override_fn_name, grpc_service_name),
));
let message = format!("Method {} is not found in proto file for service {}", override_fn_name, grpc_service_name);
return Err(fn_override.token_stream.throw_error_at_value_token(message.as_str()));
}
}

Expand Down
2 changes: 2 additions & 0 deletions my-grpc-client-macros/src/grpc_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub use generate_interfaces_implementations::*;
pub use param_type::*;
mod generate;
pub use generate::*;
//mod attr_params;
//pub use attr_params::*;
12 changes: 6 additions & 6 deletions my-grpc-extensions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "my-grpc-extensions"
version = "0.6.3"
version = "0.6.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -17,19 +17,19 @@ with-ssh = ["my-ssh", "my-grpc-client-macros?/with-ssh"]


[dependencies]
my-telemetry = { tag = "1.2.0", git = "https://github.com/MyJetTools/my-telemetry.git", features = [
my-telemetry = { tag = "1.2.1", git = "https://github.com/MyJetTools/my-telemetry.git", features = [
"my-telemetry-writer",
], optional = true }

my-logger = { tag = "1.1.0", git = "https://github.com/MyJetTools/my-logger.git" }
rust-extensions = { tag = "0.1.4", git = "https://github.com/MyJetTools/rust-extensions.git", features = [
my-logger = { tag = "1.2.1", git = "https://github.com/MyJetTools/my-logger.git" }
rust-extensions = { tag = "0.1.5", git = "https://github.com/MyJetTools/rust-extensions.git", features = [
"with-tokio",
] }

lazy_static = "*"


my-tls = { tag = "0.1.1", git = "https://github.com/MyJetTools/my-tls.git", optional = true }
my-tls = { tag = "0.1.5", git = "https://github.com/MyJetTools/my-tls.git", optional = true }
tokio = { version = "*", features = ["full"] }
tonic = { version = "*", features = ["tls", "tls-roots", "prost"] }
hyper = { version = "*" }
Expand All @@ -39,7 +39,7 @@ tokio-stream = "*"
futures = "*"
async-trait = "*"
tower = { version = "*" }
my-ssh = { tag = "0.1.1", git = "https://github.com/MyJetTools/my-ssh.git", optional = true }
my-ssh = { tag = "0.1.2", git = "https://github.com/MyJetTools/my-ssh.git", optional = true }
my-grpc-client-macros = { optional = true, path = "../my-grpc-client-macros" }
my-grpc-server-macros = { optional = true, path = "../my-grpc-server-macros" }
external-dependencies = { path = "../external-dependencies" }
Expand Down
4 changes: 2 additions & 2 deletions my-grpc-server-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "my-grpc-server-macros"
version = "0.6.3"
version = "0.6.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -9,7 +9,7 @@ edition = "2021"
proc-macro = true

[dependencies]
types-reader = { tag = "0.3.0", git = "https://github.com/MyJetTools/types-reader.git" }
types-reader = { tag = "0.5.1", git = "https://github.com/MyJetTools/types-reader.git" }

syn = { version = "*", features = ["extra-traits"] }
quote = "*"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use proc_macro::TokenStream;
use types_reader::TokensObject;

pub fn generate_server_stream(tokens: TokenStream) -> Result<TokenStream, syn::Error> {
let params_list = types_reader::ParamsList::new(tokens.into(), || None)?;
let tokens: proc_macro2::TokenStream = tokens.into();
let params_list = TokensObject::new(tokens.into())?;

let stream_name = params_list.get_named_param("stream_name")?;
let stream_name: &str = params_list.get_named_param("stream_name")?.try_into()?;

let item_name = params_list.get_named_param("item_name")?;
let item_name: &str = params_list.get_named_param("item_name")?.try_into()?;

Ok(crate::generate_stream::generate_stream(
stream_name.unwrap_as_string_value()?.as_str(),
item_name.unwrap_as_string_value()?.as_str(),
)
.into())
Ok(crate::generate_stream::generate_stream(stream_name, item_name).into())
}
15 changes: 8 additions & 7 deletions my-grpc-server-macros/src/with_result_stream/generate.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use types_reader::TokensObject;

pub fn generate(
attr: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> Result<proc_macro::TokenStream, syn::Error> {
let params_list = types_reader::ParamsList::new(attr.into(), || None)?;
let tokens: proc_macro2::TokenStream = attr.into();
let params_list = TokensObject::new(tokens.into())?;

let item_name = params_list.get_from_single_or_named("item_name")?;
let item_name: &str = params_list
.get_value_from_single_or_named("item_name")?
.try_into()?;

let content = input.to_string();

let stream_name = find_stream_name(content.as_str());

let stream_implementation = crate::generate_stream::generate_stream(
stream_name,
item_name.unwrap_as_string_value()?.as_str(),
);
let stream_implementation = crate::generate_stream::generate_stream(stream_name, item_name);

let input: proc_macro2::TokenStream = input.into();

Expand Down Expand Up @@ -64,7 +66,6 @@ fn find_stream_name(content: &str) -> &str {
stream_name.unwrap()
}


#[cfg(test)]
mod tests {

Expand Down

0 comments on commit cce1abf

Please sign in to comment.