mod lines;
use std::collections::BTreeMap;
use std::fmt;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
#[cfg(feature = "async")]
use std::io;
#[cfg(feature = "async")]
use futures_util::{TryStream, TryStreamExt};
use nom_supreme::final_parser::Location;
use thiserror::Error;
use self::lines::{file_line, version_line};
pub use self::lines::{FileLine, VersionLine};
pub use crate::common::FileMeta;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Section {
Version,
Files,
}
impl Section {
fn as_header(&self) -> &'static str {
match self {
Self::Files => "[files]",
Self::Version => "[version]",
}
}
}
#[derive(Debug, Error)]
pub enum Error {
MissingHeader(&'static str),
MissingVersionLine,
ExpectedHeader(&'static str, String),
IO(#[from] std::io::Error),
Nom(#[from] nom::error::Error<Location>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingHeader(h) => write!(f, "Missing '{}' header", h),
Self::ExpectedHeader(h, line) => write!(f, "Expected '{}' header, got {:?}", h, line),
Self::MissingVersionLine => write!(f, "Missing version line"),
Self::IO(_e) => write!(f, "I/O error"),
Self::Nom(_e) => write!(f, "Parser error"),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(feature = "async")]
async fn expect_header<L>(lines: &mut L, section: Section) -> Result<()>
where
L: TryStream<Ok = String, Error = io::Error> + Unpin,
{
let header = section.as_header();
let res = lines.try_next().await?;
let line = res.ok_or(Error::MissingHeader(header))?;
if line != header {
return Err(Error::ExpectedHeader(header, line));
}
Ok(())
}
#[cfg(feature = "async")]
async fn read_index_version<L>(lines: &mut L) -> Result<VersionLine>
where
L: TryStream<Ok = String, Error = io::Error> + Unpin,
{
expect_header(lines, Section::Version).await?;
let line = lines.try_next().await?.ok_or(Error::MissingVersionLine)?;
let version = version_line(&line)?;
Ok(version)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Manifest {
pub version: VersionLine,
pub files: BTreeMap<String, FileLine>,
}
fn find_version_line<B: BufRead>(buffer: &mut String, reader: &mut B) -> Result<VersionLine> {
buffer.clear();
loop {
let len = reader.read_line(buffer)?;
assert_ne!(len, 0);
if buffer.trim() == Section::Version.as_header() {
buffer.clear();
reader.read_line(buffer)?;
let line = version_line(buffer.trim())?;
break Ok(line);
}
buffer.clear();
}
}
fn find_files_lines<B: BufRead>(buffer: &mut String, reader: &mut B) -> Result<()> {
buffer.clear();
loop {
let len = reader.read_line(buffer)?;
assert_ne!(len, 0);
if buffer.trim() == Section::Files.as_header() {
buffer.clear();
break Ok(());
}
buffer.clear();
}
}
impl Manifest {
pub fn from_buf_read<B: BufRead>(reader: &mut B) -> Result<Self> {
let mut buffer = String::new();
let mut files = BTreeMap::new();
let version = find_version_line(&mut buffer, reader)?;
find_files_lines(&mut buffer, reader)?;
while reader.read_line(&mut buffer)? > 0 {
let (name, meta, hash) = file_line(buffer.trim())?;
files.insert(name.to_string(), (meta, hash));
buffer.clear();
}
Ok(Self { version, files })
}
pub fn from_file(path: &Path) -> Result<Self> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
Self::from_buf_read(&mut reader)
}
}
#[cfg(feature = "async")]
pub async fn load_manifest<L>(lines: &mut L) -> Result<Manifest>
where
L: TryStream<Ok = String, Error = io::Error> + Unpin,
{
let mut files = BTreeMap::new();
let version = read_index_version(lines).await?;
expect_header(lines, Section::Files).await?;
while let Some(item) = lines.try_next().await? {
let line = item;
let (filename, meta, hash) = file_line(&line)?;
files.insert(filename.to_owned(), (meta, hash));
}
Ok(Manifest { version, files })
}