async_dashscope/operation/
validate.rs

1use crate::error::{DashScopeError, Result};
2use crate::operation::request::RequestTrait;
3
4/// Defines the validation strategy for a given model.
5///
6/// This enum replaces the previous trait-based approach to resolve `dyn` compatibility issues.
7/// It allows for static dispatch, which is more performant and avoids the complexities of
8/// object safety.
9pub enum ModelValidator {
10    /// The default validation, which performs no special checks.
11    Default,
12    /// Validation specific to the `deepseek-r1` model.
13    DeepSeekV1,
14}
15
16impl ModelValidator {
17    /// Validates the request parameters based on the selected model strategy.
18    ///
19    /// # Arguments
20    ///
21    /// * `params` - A type that implements `RequestTrait`, providing access to the model name and parameters.
22    ///
23    /// # Returns
24    ///
25    /// * `Ok(())` if the parameters are valid.
26    /// * `Err(DashScopeError)` if the parameters are invalid for the given model.
27    pub fn validate<R: RequestTrait + ?Sized>(&self, params: &R) -> Result<()> {
28        match self {
29            ModelValidator::Default => {
30                // No specific validation rules for the default case.
31                Ok(())
32            }
33            ModelValidator::DeepSeekV1 => {
34                // The deepseek-r1 model does not support `result_format: "text"`.
35                if let Some(p) = params.parameters() {
36                    if let Some(format) = &p.result_format {
37                        if format == "text" {
38                            return Err(DashScopeError::InvalidArgument(
39                                "deepseek-r1 does not support result_format = text".into(),
40                            ));
41                        }
42                    }
43                }
44                Ok(())
45            }
46        }
47    }
48}
49
50/// Selects the appropriate validator for the given model name.
51///
52/// # Arguments
53///
54/// * `model` - The name of the model as a string slice.
55///
56/// # Returns
57///
58/// A `ModelValidator` enum variant corresponding to the required validation strategy.
59pub(crate) fn check_model_parameters(model: &str) -> ModelValidator {
60    match model {
61        "deepseek-r1" => ModelValidator::DeepSeekV1,
62        _ => ModelValidator::Default,
63    }
64}