Billy Chan a766500ebf
Cont. "Enable convert from ActiveModel to Model" (#990)
* Changelog

* Enable convert from ActiveModel to Model (#725)

* feat: enable convert from ActiveModel to Model

* feat: add tests for converting from ActiveModel to Model

* cargo fmt

* Refactoring

Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>

* Fix clippy warnings

* Use error type

Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
Co-authored-by: greenhandatsjtu <40566803+greenhandatsjtu@users.noreply.github.com>
2022-10-23 23:12:22 +08:00

68 lines
2.0 KiB
Rust

use quote::format_ident;
use syn::{punctuated::Punctuated, token::Comma, Field, Ident, Meta};
pub(crate) fn field_not_ignored(field: &Field) -> bool {
for attr in field.attrs.iter() {
if let Some(ident) = attr.path.get_ident() {
if ident != "sea_orm" {
continue;
}
} else {
continue;
}
if let Ok(list) = attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated) {
for meta in list.iter() {
if let Meta::Path(path) = meta {
if let Some(name) = path.get_ident() {
if name == "ignore" {
return false;
}
}
}
}
}
}
true
}
pub(crate) fn format_field_ident(field: Field) -> Ident {
format_ident!("{}", field.ident.unwrap().to_string())
}
pub(crate) fn trim_starting_raw_identifier<T>(string: T) -> String
where
T: ToString,
{
string
.to_string()
.trim_start_matches(RAW_IDENTIFIER)
.to_string()
}
pub(crate) fn escape_rust_keyword<T>(string: T) -> String
where
T: ToString,
{
let string = string.to_string();
if RUST_KEYWORDS.iter().any(|s| s.eq(&string)) {
format!("r#{}", string)
} else if RUST_SPECIAL_KEYWORDS.iter().any(|s| s.eq(&string)) {
format!("{}_", string)
} else {
string
}
}
pub(crate) const RAW_IDENTIFIER: &str = "r#";
pub(crate) const RUST_KEYWORDS: [&str; 49] = [
"as", "async", "await", "break", "const", "continue", "dyn", "else", "enum", "extern", "false",
"fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref",
"return", "static", "struct", "super", "trait", "true", "type", "union", "unsafe", "use",
"where", "while", "abstract", "become", "box", "do", "final", "macro", "override", "priv",
"try", "typeof", "unsized", "virtual", "yield",
];
pub(crate) const RUST_SPECIAL_KEYWORDS: [&str; 3] = ["crate", "Self", "self"];