1
0
mirror of https://github.com/Sebekerga/native_api_1c.git synced 2025-06-16 23:47:43 +02:00

better error handling for parsing

This commit is contained in:
Kozlov Maxim
2023-11-02 12:19:17 +06:00
parent ba2ca1bdc7
commit dc06d182cc

View File

@ -100,16 +100,24 @@ pub struct FuncArgumentDesc {
pub struct FuncArgumentMeta {
ty: String,
default: Option<Expr>,
#[allow(dead_code)]
as_in: bool,
as_out: bool,
}
impl TryFrom<FuncArgumentMeta> for FuncArgumentDesc {
type Error = ();
type Error = ErrorConvertingMeta;
fn try_from(arg_meta: FuncArgumentMeta) -> Result<Self, Self::Error> {
if arg_meta.as_in && arg_meta.as_out {
return Err(Self::Error::ConflictingParams(
"as_in".to_string(),
"as_out".to_string(),
));
}
Ok(Self {
ty: ParamType::try_from(&arg_meta.ty)?,
ty: ParamType::try_from(&arg_meta.ty)
.map_err(|_| Self::Error::InvalidTypeForParam(arg_meta.ty))?,
default: arg_meta.default,
out_param: arg_meta.as_out,
})
@ -128,19 +136,50 @@ pub struct FuncReturnMeta {
}
impl TryFrom<FuncReturnMeta> for FuncReturnDesc {
type Error = ();
type Error = ErrorConvertingMeta;
fn try_from(arg_meta: FuncReturnMeta) -> Result<Self, Self::Error> {
Ok(Self {
ty: match arg_meta.ty.as_str() {
UNTYPED_TYPE => None,
_ => Some(ParamType::try_from(&arg_meta.ty)?),
_ => Some(
ParamType::try_from(&arg_meta.ty)
.map_err(|_| Self::Error::InvalidTypeForReturn(arg_meta.ty))?,
),
},
result: arg_meta.result,
})
}
}
pub enum ErrorConvertingMeta {
InvalidTypeForParam(String),
InvalidTypeForReturn(String),
ConflictingParams(String, String),
}
impl From<ErrorConvertingMeta> for darling::Error {
fn from(err: ErrorConvertingMeta) -> Self {
match err {
ErrorConvertingMeta::InvalidTypeForParam(ty) => {
let joined_allowed_types = crate::constants::ALL_ARG_TYPES.join(", ");
darling::Error::custom(format!(
"Invalid type: `{ty}`. Must be one of: {joined_allowed_types}"
))
}
ErrorConvertingMeta::InvalidTypeForReturn(ty) => {
let joined_allowed_types = crate::constants::ALL_RETURN_TYPES.join(", ");
darling::Error::custom(format!(
"Invalid type: `{ty}`. Must be one of: {joined_allowed_types}"
))
}
ErrorConvertingMeta::ConflictingParams(param1, param2) => {
darling::Error::custom(format!("Conflicting params: {} and {}", param1, param2))
}
}
}
}
pub fn parse_functions(struct_data: &DataStruct) -> Result<Vec<FuncDesc>, TokenStream> {
let mut functions_descriptions = vec![];