提交 6bde025e authored 作者: Serhij S's avatar Serhij S

auto worker name

上级 7b76763a
[package] [package]
name = "roboplc-derive" name = "roboplc-derive"
version = "0.1.4" version = "0.1.5"
edition = "2021" edition = "2021"
authors = ["Serhij S. <div@altertech.com>"] authors = ["Serhij S. <div@altertech.com>"]
license = "Apache-2.0" license = "Apache-2.0"
......
...@@ -211,6 +211,19 @@ fn parse_delivery_policy(s: Option<&str>) -> proc_macro2::TokenStream { ...@@ -211,6 +211,19 @@ fn parse_delivery_policy(s: Option<&str>) -> proc_macro2::TokenStream {
} }
} }
fn lowercase_first_letter(s: &str) -> String {
s.chars()
.enumerate()
.map(|(i, c)| {
if i == 0 {
c.to_lowercase().to_string()
} else {
c.to_string()
}
})
.collect()
}
/// Automatically implements the `WorkerOptions` trait for a worker struct /// Automatically implements the `WorkerOptions` trait for a worker struct
/// ///
/// Provides an attribute `worker_opts` to specify the worker options. The attribute can be /// Provides an attribute `worker_opts` to specify the worker options. The attribute can be
...@@ -218,8 +231,9 @@ fn parse_delivery_policy(s: Option<&str>) -> proc_macro2::TokenStream { ...@@ -218,8 +231,9 @@ fn parse_delivery_policy(s: Option<&str>) -> proc_macro2::TokenStream {
/// ///
/// Atrribute arguments: /// Atrribute arguments:
/// ///
/// * `name` - Specifies the name of the worker. The value is mandatory and must be a quoted /// * `name` - Specifies the name of the worker. The value must be a quoted string. The name must
/// string. The name must be unique and must be 15 characters or less. /// be unique and must be 15 characters or less. If not specified, the default is the the structure
/// name with the first letter in lowercase
/// ///
/// * `stack_size` - Specifies the stack size for the worker /// * `stack_size` - Specifies the stack size for the worker
/// ///
...@@ -333,7 +347,7 @@ pub fn worker_opts_derive(input: TokenStream) -> TokenStream { ...@@ -333,7 +347,7 @@ pub fn worker_opts_derive(input: TokenStream) -> TokenStream {
} }
} }
let worker_name = worker_name.expect("worker name is not specified"); let worker_name = worker_name.unwrap_or_else(|| lowercase_first_letter(&name.to_string()));
assert!( assert!(
worker_name.len() <= 15, worker_name.len() <= 15,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论