Skip to main content

eighty/workspace/
post.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2// This file is part of Eighty.
3//
4// Copyright (c) 2021 Wei Tang.
5//
6// Eighty is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Eighty is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Eighty. If not, see <http://www.gnu.org/licenses/>.
18
19use super::FullWorkspace;
20use crate::{Error, site::SiteName};
21use std::{collections::HashMap, ops::Deref, path::PathBuf};
22
23pub struct SimplePostWorkspace(pub HashMap<SiteName, SimplePostSite>);
24
25impl SimplePostWorkspace {
26    pub fn new(full: &FullWorkspace) -> Result<Self, Error> {
27        let mut sites = HashMap::new();
28
29        for (site_name, full_site) in &full.sites {
30            let mut post_site = HashMap::new();
31
32            for (asset_path, asset) in &full_site.assets.assets {
33                post_site.insert(asset_path.clone(), asset.clone());
34            }
35
36            for (file_path, file) in full_site.files.as_ref() {
37                post_site.insert(file_path.clone(), file.content.clone());
38            }
39
40            for document in &full_site.documents {
41                post_site.insert(
42                    document.rendered.name.path(),
43                    document.content.as_bytes().to_owned(),
44                );
45            }
46
47            sites.insert(
48                site_name.clone(),
49                SimplePostSite {
50                    base_url: full_site.site.config.base_url.clone(),
51                    files: post_site,
52                },
53            );
54        }
55
56        Ok(Self(sites))
57    }
58}
59
60impl Deref for SimplePostWorkspace {
61    type Target = HashMap<SiteName, SimplePostSite>;
62
63    fn deref(&self) -> &Self::Target {
64        &self.0
65    }
66}
67
68pub struct SimplePostSite {
69    pub base_url: String,
70    pub files: HashMap<PathBuf, Vec<u8>>,
71}