#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WorkingTreeFormat {
pub format_string: &'static [u8],
pub description: &'static str,
pub uses_dirstate: bool,
pub supports_content_filtering: bool,
pub supports_views: bool,
pub supported: bool,
pub deprecated: bool,
}
impl WorkingTreeFormat {
pub const DEFAULT: WorkingTreeFormat = WorkingTreeFormat {
format_string: b"",
description: "",
uses_dirstate: false,
supports_content_filtering: false,
supports_views: false,
supported: false,
deprecated: 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 WorkingTreeFormatRegistration(pub &'static WorkingTreeFormat);
inventory::collect!(WorkingTreeFormatRegistration);
#[macro_export]
macro_rules! declare_workingtree_format {
(
$name:ident {
format_string: $fmt:expr,
description: $desc:expr,
$( $field:ident : $value:expr, )*
}
) => {
pub static $name: $crate::workingtree::format::WorkingTreeFormat =
$crate::workingtree::format::WorkingTreeFormat {
format_string: $fmt,
description: $desc,
$( $field: $value, )*
..$crate::workingtree::format::WorkingTreeFormat::DEFAULT
};
inventory::submit! {
$crate::workingtree::format::WorkingTreeFormatRegistration(&$name)
}
};
}
pub fn find_format(format_string: &[u8]) -> Option<&'static WorkingTreeFormat> {
inventory::iter::<WorkingTreeFormatRegistration>
.into_iter()
.map(|r| r.0)
.find(|f| f.format_string == format_string)
}
pub fn all_formats() -> Vec<&'static WorkingTreeFormat> {
inventory::iter::<WorkingTreeFormatRegistration>
.into_iter()
.map(|r| r.0)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn format_6_is_registered_and_supported() {
let f = find_format(b"Bazaar Working Tree Format 6 (bzr 1.14)\n")
.expect("working tree format 6 registered");
assert!(f.uses_dirstate);
assert!(f.supports_views);
assert!(f.supports_content_filtering);
assert!(f.is_supported());
}
#[cfg(any(feature = "weave", feature = "knit"))]
#[test]
fn format_3_is_not_dirstate() {
let f = find_format(b"Bazaar-NG Working Tree format 3")
.expect("working tree format 3 registered");
assert!(!f.uses_dirstate);
}
#[test]
fn unknown_marker_is_none() {
assert!(find_format(b"nonsense\n").is_none());
}
}