async-git 0.0.0-squat-name

Pure-rust async implementation of git built on tokio
Documentation
mod hex;

use std::io::{self, Error as IOError};
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};

pub use async_compression::futures::bufread::{ZlibDecoder, ZlibEncoder};
use tokio::{
    fs::File,
    io::{AsyncReadExt, AsyncWriteExt},
};

pub use self::hex::*;

/// Marker trait to ensure compression/decompression is already happening transparently.
pub trait ZlibIO {}

impl<R> ZlibIO for ZlibDecoder<R> {}

impl<W> ZlibIO for ZlibEncoder<W> {}

pub async fn init_file(path: impl AsRef<Path>, contents: impl AsRef<str>) -> Result<(), io::Error> {
    let mut file = File::create(path.as_ref()).await?;
    file.write_all(contents.as_ref().as_bytes()).await?;
    Ok(())
}

pub async fn file_to_string(path: impl AsRef<Path>) -> Result<String, io::Error> {
    let mut file = File::open(path).await?;
    let mut contents = String::new();
    file.read_to_string(&mut contents).await?;
    Ok(contents)
}

pub struct FuckYouTokio<R>(pub R);

impl<R: tokio::io::AsyncRead + Unpin> futures::io::AsyncRead for FuckYouTokio<R> {
    fn poll_read(
        self: Pin<&mut Self>,
        ctx: &mut Context,
        buf: &mut [u8],
    ) -> Poll<Result<usize, IOError>> {
        let pin = self.get_mut();
        R::poll_read(Pin::new(&mut pin.0), ctx, buf)
    }
}

impl<R: futures::io::AsyncRead + Unpin> tokio::io::AsyncRead for FuckYouTokio<R> {
    fn poll_read(
        self: Pin<&mut Self>,
        ctx: &mut Context,
        buf: &mut [u8],
    ) -> Poll<Result<usize, IOError>> {
        let pin = self.get_mut();
        R::poll_read(Pin::new(&mut pin.0), ctx, buf)
    }
}