doc_pyramid 0.1.2

doc_pyramid
Documentation

doc_pyramid

use std::future::Future;

use aok::Result;
use doc_chunk::DocChunk;
use futures::{StreamExt, TryStreamExt, stream};

pub async fn digest<AsyncString: Future<Output = Result<String>>>(
  txt: &str,
  doc_chunk: DocChunk,
  short: &impl Fn(String) -> AsyncString,
) -> Result<String> {
  let li = stream::iter(doc_chunk.parse(txt))
    .map(|i| short(i))
    .buffered(16)
    .try_collect::<Vec<String>>()
    .await?;
  Ok(li.join("\n"))
}

pub async fn parse<AsyncString: Future<Output = Result<String>>>(
  txt: impl AsRef<str>,
  limit: usize,
  short: impl Fn(String) -> AsyncString,
) -> Result<Vec<String>> {
  let mut txt = txt.as_ref();
  let mut result = vec![];
  let doc_chunk = DocChunk::new(limit);
  while !txt.is_empty() {
    let brief = digest(txt, doc_chunk, &short).await?;
    let stop = brief.len() <= limit;
    result.push(brief);
    if stop {
      break;
    } else {
      txt = result.last().unwrap().as_str();
    }
  }

  Ok(result)
}

About

This project is an open-source component of i18n.site ⋅ Internationalization Solution.

关于

本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。