#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BranchFormat {
pub format_string: &'static [u8],
pub description: &'static str,
pub supports_tags: bool,
pub supports_stacking: bool,
pub supports_reference_locations: bool,
pub full_history: bool,
pub supported: bool,
pub deprecated: bool,
pub is_reference: bool,
}
impl BranchFormat {
pub const DEFAULT: BranchFormat = BranchFormat {
format_string: b"",
description: "",
supports_tags: false,
supports_stacking: false,
supports_reference_locations: false,
full_history: false,
supported: false,
deprecated: false,
is_reference: false,
};
pub fn format_string(&self) -> &'static [u8] {
self.format_string
}
pub fn get_format_description(&self) -> &'static str {
self.description
}
pub fn is_supported(&self) -> bool {
self.supported
}
}
pub struct BranchFormatRegistration(pub &'static BranchFormat);
inventory::collect!(BranchFormatRegistration);
#[macro_export]
macro_rules! declare_branch_format {
(
$name:ident {
format_string: $fmt:expr,
description: $desc:expr,
$( $field:ident : $value:expr, )*
}
) => {
pub static $name: $crate::branch::format::BranchFormat =
$crate::branch::format::BranchFormat {
format_string: $fmt,
description: $desc,
$( $field: $value, )*
..$crate::branch::format::BranchFormat::DEFAULT
};
inventory::submit! {
$crate::branch::format::BranchFormatRegistration(&$name)
}
};
}
pub fn find_format(format_string: &[u8]) -> Option<&'static BranchFormat> {
inventory::iter::<BranchFormatRegistration>
.into_iter()
.map(|r| r.0)
.find(|f| f.format_string == format_string)
}
pub fn all_formats() -> Vec<&'static BranchFormat> {
inventory::iter::<BranchFormatRegistration>
.into_iter()
.map(|r| r.0)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_7_is_registered_and_supported() {
let f = find_format(b"Bazaar Branch Format 7 (needs bzr 1.6)\n")
.expect("branch format 7 registered");
assert!(f.supports_tags);
assert!(f.supports_stacking);
assert!(f.supports_reference_locations);
assert!(f.is_supported());
}
#[test]
fn format_8_has_reference_locations() {
let f = find_format(b"Bazaar Branch Format 8 (needs bzr 1.15)\n")
.expect("branch format 8 registered");
assert!(f.supports_reference_locations);
}
#[test]
fn unknown_marker_is_none() {
assert!(find_format(b"Bazaar nonsense branch\n").is_none());
}
}