#[derive(Debug, Clone, Copy)]
pub struct ControlDirFormat {
pub name: &'static str,
pub repo_marker: &'static [u8],
pub branch_marker: &'static [u8],
pub wt_marker: &'static [u8],
pub wt_has_views: bool,
}
impl ControlDirFormat {
pub const DEFAULT: ControlDirFormat = ControlDirFormat {
name: "",
repo_marker: b"",
branch_marker: b"",
wt_marker: b"",
wt_has_views: false,
};
}
pub struct ControlDirFormatRegistration(pub &'static ControlDirFormat);
inventory::collect!(ControlDirFormatRegistration);
#[macro_export]
macro_rules! declare_bzrdir_format {
(
$static_name:ident {
$( $field:ident : $value:expr, )*
}
) => {
pub static $static_name: $crate::bzrdir::format::ControlDirFormat =
$crate::bzrdir::format::ControlDirFormat {
$( $field: $value, )*
..$crate::bzrdir::format::ControlDirFormat::DEFAULT
};
inventory::submit! {
$crate::bzrdir::format::ControlDirFormatRegistration(&$static_name)
}
};
}
pub fn find_control_dir_format(name: &str) -> Option<&'static ControlDirFormat> {
inventory::iter::<ControlDirFormatRegistration>
.into_iter()
.map(|r| r.0)
.find(|f| f.name == name)
}
pub fn control_dir_formats() -> Vec<&'static ControlDirFormat> {
inventory::iter::<ControlDirFormatRegistration>
.into_iter()
.map(|r| r.0)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_2a_combo_is_registered() {
let f = find_control_dir_format("2a").expect("2a combo registered");
assert_eq!(f.repo_marker, super::super::REPOSITORY_FORMAT_2A);
assert_eq!(f.branch_marker, super::super::BRANCH_FORMAT_7);
assert_eq!(f.wt_marker, super::super::WORKINGTREE_FORMAT_6);
assert!(f.wt_has_views);
}
#[cfg(feature = "knit")]
#[test]
fn the_knit_combo_pairs_branch_5_and_wt_3() {
let f = find_control_dir_format("knit").expect("knit combo registered");
assert_eq!(f.repo_marker, b"Bazaar-NG Knit Repository Format 1");
assert_eq!(f.branch_marker, b"Bazaar-NG branch format 5\n");
assert_eq!(f.wt_marker, b"Bazaar-NG Working Tree format 3");
assert!(!f.wt_has_views);
}
#[test]
fn every_combo_names_a_registered_repository_format() {
for f in control_dir_formats() {
assert!(
crate::repository::find_format(f.repo_marker).is_some(),
"combo {} names an unregistered repository marker",
f.name
);
}
}
#[test]
fn unknown_name_is_none() {
assert!(find_control_dir_format("no-such-format").is_none());
}
}