tran_trait 0.1.22

tran_trait
Documentation
use std::future::Future;

use aok::{Result, Void};

pub type HashTxt = (Vec<u8>, Vec<u8>);

pub trait Cache {
  fn get(
    &self,
    from_lang: u16,
    to_lang: u16,
    hash_li: &[Vec<u8>],
  ) -> impl Future<Output = Result<Vec<Option<String>>>> + Send
  where
    Self: Sync,
  {
    async move {
      let ft = vb::e([from_lang as u64, to_lang as u64]);
      let mut r = self.user_get(&ft, hash_li).await?;
      let mut to_fetch = vec![];
      let mut pos_li = vec![];
      for (pos, (cached, hash)) in r.iter().zip(hash_li).enumerate() {
        if cached.is_none() {
          to_fetch.push(hash.clone());
          pos_li.push(pos);
        }
      }
      for (pos, i) in pos_li
        .into_iter()
        .zip(self.global_get(&ft, &to_fetch[..]).await?.into_iter())
      {
        r[pos] = i;
      }

      Ok(r)
    }
  }

  fn global_get(
    &self,
    from_to: &[u8],
    hash_li: &[Vec<u8>],
  ) -> impl Future<Output = Result<Vec<Option<String>>>> + Send;

  fn user_get(
    &self,
    from_to: &[u8],
    hash_li: &[Vec<u8>],
  ) -> impl Future<Output = Result<Vec<Option<String>>>> + Send;

  fn set(
    &self,
    from_lang: u16,
    to_lang: u16,
    hash_li: &[Vec<u8>],
    li: &[String],
  ) -> impl Future<Output = Void> + Send;

  fn set_src_li(
    &self,
    li: Vec<(
      // lang
      u16,
      // hash txt
      Vec<HashTxt>,
    )>,
  ) -> impl Future<Output = Void> + Send;

  fn set_user(
    &self,
    from_lang: u16,
    to_lang: u16,
    hash_str_li: Vec<(Vec<u8>, &str)>,
  ) -> impl Future<Output = Void> + Send;

  fn src_li(
    &self,
    hash_li: &[(u16, &Vec<u8>)],
  ) -> impl Future<Output = Result<Vec<Option<String>>>> + Send;
}