1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! # The manifest (`*.txt`) files

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)]
/// A section of the manifest
pub enum Section {
    /// The `[version]` section
    Version,
    /// The `[files]` section
    Files,
}

impl Section {
    fn as_header(&self) -> &'static str {
        match self {
            Self::Files => "[files]",
            Self::Version => "[version]",
        }
    }
}

#[derive(Debug, Error)]
/// An error from parsing a manifest
pub enum Error {
    /// Unexpected EOF, expected a header
    MissingHeader(&'static str),
    /// Unexpected EOF, expected version line
    MissingVersionLine,
    /// Expected a header but found something else
    ExpectedHeader(&'static str, String),
    /// An IO error
    IO(#[from] std::io::Error),
    /// Failed to parse a line
    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"),
        }
    }
}

/// The result type for this module
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)
}

/// A manifest file in-memory
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Manifest {
    /// The parsed version line
    pub version: VersionLine,
    /// The parsed, sorted list of files
    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)?;
            //panic!("{:?}", buffer);
            let line = version_line(buffer.trim())?;
            //panic!("{:?}", &line);
            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 {
    /// Read a manifest from a [BufRead] implementation
    ///
    /// ```
    /// use std::{io::Cursor, collections::BTreeMap};
    /// use assembly_pack::{md5::MD5Sum, txt::{Manifest, VersionLine}};
    ///
    /// let hash = MD5Sum::compute("32");
    /// let text = format!("[version]\n32,{},Name\n[files]\n", hash);
    /// let res = Manifest::from_buf_read(&mut Cursor::new(text));
    ///
    /// match res {
    ///     Err(e) => panic!("{}", e),
    ///     Ok(m) => {
    ///         assert_eq!(m, Manifest {
    ///             version: VersionLine::new(32, String::from("Name")),
    ///             files: BTreeMap::new(),
    ///         });
    ///     }
    /// }
    /// ```
    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 })
    }

    /// Read a Manifest from a file
    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)
    }
}

/// Load the manifest from a stream of lines
#[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 })
}