tina-core 0.0.2

Tina platform
Documentation
//! 无数据
use crate::serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::task::{Context, Poll};
use std::{any::type_name, pin::Pin};

use futures::{AsyncRead, AsyncSeek, Stream};
use garde::Validate;
use utoipa::{
    openapi::{ObjectBuilder, RefOr, Schema},
    ToSchema,
};

use crate::tina::data::binary::{BinaryContent, DataContent};
use crate::tina::data::AppResult;
use crate::tina::server::session::Session;
use httpdate::HttpDate;
use std::fmt::{Display, Formatter};
use std::io::{Read, Seek, SeekFrom};
use std::time::SystemTime;
use tokio::io::ReadBuf;

/// 无数据
#[derive(Clone, Debug, Default)]
pub struct NoData;

impl Stream for NoData {
    type Item = AppResult<DataContent>;

    fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Poll::Ready(None)
    }
}

impl Read for NoData {
    fn read(&mut self, _buf: &mut [u8]) -> std::io::Result<usize> {
        Ok(0)
    }
}

impl Seek for NoData {
    fn seek(&mut self, _pos: SeekFrom) -> std::io::Result<u64> {
        Ok(0)
    }
}

impl AsyncSeek for NoData {
    fn poll_seek(self: Pin<&mut Self>, _cx: &mut Context<'_>, _pos: SeekFrom) -> Poll<std::io::Result<u64>> {
        Poll::Pending
    }
}

impl AsyncRead for NoData {
    fn poll_read(self: Pin<&mut Self>, _cx: &mut Context<'_>, _buf: &mut [u8]) -> Poll<std::io::Result<usize>> {
        Poll::Pending
    }
}

impl tokio::io::AsyncRead for NoData {
    fn poll_read(self: Pin<&mut Self>, _cx: &mut Context<'_>, _buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
        Poll::Pending
    }
}

impl BinaryContent for NoData {
    fn get_size(&self) -> Option<u64> {
        None
    }

    fn is_valid(&self) -> bool {
        false
    }

    fn last_modified(&self) -> HttpDate {
        HttpDate::from(SystemTime::now())
    }
}

impl Display for NoData {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str("null")
    }
}

impl std::error::Error for NoData {}

impl Serialize for NoData {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        serializer.serialize_unit()
    }
}

impl<'de> Deserialize<'de> for NoData {
    fn deserialize<D>(_: D) -> Result<Self, <D as Deserializer<'de>>::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(NoData {})
    }
}

#[allow(unused_variables)]
impl Validate for NoData {
    type Context = Session;

    fn validate(&self, ctx: &Self::Context) -> Result<(), garde::Errors> {
        Ok(())
    }
}

impl<'a> ToSchema<'a> for NoData {
    fn schema() -> (&'a str, RefOr<Schema>) {
        (type_name::<NoData>(), RefOr::T(Schema::from(ObjectBuilder::new().schema_type(utoipa::openapi::SchemaType::Object))))
    }
}