Skip to content

Commit

Permalink
[Rust-Axum] Fix compilation error when validate is used on Nullable v…
Browse files Browse the repository at this point in the history
…alues (#20100)

* Fix compilation error when validate is used on Nullable values

* Update samples

* Switch Nullable Into Option trait implement to From

* Update samples from rebase
  • Loading branch information
Victoria-Casasampere-BeeTheData authored Nov 15, 2024
1 parent 2935247 commit d7a23a9
Show file tree
Hide file tree
Showing 11 changed files with 1,375 additions and 0 deletions.
125 changes: 125 additions & 0 deletions modules/openapi-generator/src/main/resources/rust-axum/types.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}

impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}

impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}

impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}

impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}

impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}

impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}

#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {
Expand Down
125 changes: 125 additions & 0 deletions samples/server/petstore/rust-axum/output/apikey-auths/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,131 @@ where
}
}

impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}

impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}

impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}

impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}

impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}

impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}

impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}

impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}

#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {
Expand Down
125 changes: 125 additions & 0 deletions samples/server/petstore/rust-axum/output/multipart-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,131 @@ where
}
}

impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}

impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}

impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}

impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}

impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}

impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}

impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}

impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}

impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}

#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {
Expand Down
Loading

0 comments on commit d7a23a9

Please sign in to comment.