[core] Tests first: share tokens, prefix escapes, repo layout, exact host path, error codes (red)
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! The shape of the routes repository: exactly `ianitor.toml` and `hosts/<hostname>.toml`,
|
||||
//! regular files only. Anything else (other names, folders inside `hosts/`, symbolic links,
|
||||
//! submodules, executables) is refused, so the repository can never hold something the
|
||||
//! compilers do not read.
|
||||
|
||||
use crate::error::{ValidationError, reason};
|
||||
use crate::validate::check_hostname;
|
||||
|
||||
/// The site file at the root of the repository.
|
||||
pub const SITE_FILE: &str = "ianitor.toml";
|
||||
/// The folder holding one file per host.
|
||||
pub const HOSTS_DIR: &str = "hosts";
|
||||
/// Most host files one repository may hold.
|
||||
pub const MAX_HOST_FILES: usize = 1000;
|
||||
|
||||
/// What a tree entry is, as git reports it (`git ls-tree` modes).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum EntryKind {
|
||||
/// Mode 100644.
|
||||
File,
|
||||
/// Mode 100755.
|
||||
Executable,
|
||||
/// Mode 120000.
|
||||
Symlink,
|
||||
/// Mode 160000.
|
||||
Submodule,
|
||||
}
|
||||
|
||||
/// A path the repository may hold.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RoutePath {
|
||||
Site,
|
||||
Host { hostname: String },
|
||||
}
|
||||
|
||||
/// The path of the file for `hostname`.
|
||||
pub fn host_path(hostname: &str) -> String {
|
||||
format!("{HOSTS_DIR}/{hostname}.toml")
|
||||
}
|
||||
|
||||
/// Check one path of the repository tree (as listed recursively, `/`-separated).
|
||||
pub fn check_path(path: &str, kind: EntryKind) -> Result<RoutePath, ValidationError> {
|
||||
// Tests first: not written yet.
|
||||
let _ = (kind, reason::PATH_LAYOUT, check_hostname);
|
||||
match path.strip_prefix("hosts/").and_then(|r| r.strip_suffix(".toml")) {
|
||||
Some(h) => Ok(RoutePath::Host { hostname: h.to_owned() }),
|
||||
None => Ok(RoutePath::Site),
|
||||
}
|
||||
}
|
||||
|
||||
/// Check a whole tree listing.
|
||||
pub fn check_tree<'a>(
|
||||
entries: impl IntoIterator<Item = (&'a str, EntryKind)>,
|
||||
) -> Result<(), Vec<ValidationError>> {
|
||||
let _ = entries.into_iter().count();
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user