##// END OF EJS Templates
rust-changelog: introducing an intermediate `ChangelogEntry`...
Georges Racinet -
r51268:841b13e6 default
parent child Browse files
Show More
@@ -1,6 +1,6 b''
1 1 use crate::errors::HgError;
2 use crate::revlog::Revision;
3 2 use crate::revlog::{Node, NodePrefix};
3 use crate::revlog::{Revision, NULL_REVISION};
4 4 use crate::revlog::{Revlog, RevlogEntry, RevlogError};
5 5 use crate::utils::hg_path::HgPath;
6 6 use crate::vfs::Vfs;
@@ -32,30 +32,29 b' impl Changelog {'
32 32 self.data_for_rev(rev)
33 33 }
34 34
35 /// Return the `RevlogEntry` for the given revision number.
35 /// Return the [`ChangelogEntry`] for the given revision number.
36 36 pub fn entry_for_rev(
37 37 &self,
38 38 rev: Revision,
39 ) -> Result<RevlogEntry, RevlogError> {
40 self.revlog.get_entry(rev)
39 ) -> Result<ChangelogEntry, RevlogError> {
40 let revlog_entry = self.revlog.get_entry(rev)?;
41 Ok(ChangelogEntry { revlog_entry })
41 42 }
42 43
43 44 /// Return the [`ChangelogRevisionData`] for the given revision number.
45 ///
46 /// This is a useful shortcut in case the caller does not need the
47 /// generic revlog information (parents, hashes etc). Otherwise
48 /// consider taking a [`ChangelogEntry`] with
49 /// [entry_for_rev](`Self::entry_for_rev`) and doing everything from there.
44 50 pub fn data_for_rev(
45 51 &self,
46 52 rev: Revision,
47 53 ) -> Result<ChangelogRevisionData, RevlogError> {
48 let bytes = self.revlog.get_rev_data(rev)?;
49 if bytes.is_empty() {
50 Ok(ChangelogRevisionData::null())
51 } else {
52 Ok(ChangelogRevisionData::new(bytes).map_err(|err| {
53 RevlogError::Other(HgError::CorruptedRepository(format!(
54 "Invalid changelog data for revision {}: {:?}",
55 rev, err
56 )))
57 })?)
54 if rev == NULL_REVISION {
55 return Ok(ChangelogRevisionData::null());
58 56 }
57 self.entry_for_rev(rev)?.data()
59 58 }
60 59
61 60 pub fn node_from_rev(&self, rev: Revision) -> Option<&Node> {
@@ -70,6 +69,45 b' impl Changelog {'
70 69 }
71 70 }
72 71
72 /// A specialized `RevlogEntry` for `changelog` data format
73 ///
74 /// This is a `RevlogEntry` with the added semantics that the associated
75 /// data should meet the requirements for `changelog`, materialized by
76 /// the fact that `data()` constructs a `ChangelogRevisionData`.
77 /// In case that promise would be broken, the `data` method returns an error.
78 #[derive(Clone)]
79 pub struct ChangelogEntry<'changelog> {
80 /// Same data, as a generic `RevlogEntry`.
81 pub(crate) revlog_entry: RevlogEntry<'changelog>,
82 }
83
84 impl<'changelog> ChangelogEntry<'changelog> {
85 pub fn data<'a>(
86 &'a self,
87 ) -> Result<ChangelogRevisionData<'changelog>, RevlogError> {
88 let bytes = self.revlog_entry.data()?;
89 if bytes.is_empty() {
90 Ok(ChangelogRevisionData::null())
91 } else {
92 Ok(ChangelogRevisionData::new(bytes).map_err(|err| {
93 RevlogError::Other(HgError::CorruptedRepository(format!(
94 "Invalid changelog data for revision {}: {:?}",
95 self.revlog_entry.revision(),
96 err
97 )))
98 })?)
99 }
100 }
101
102 /// Obtain a reference to the underlying `RevlogEntry`.
103 ///
104 /// This allows the caller to access the information that is common
105 /// to all revlog entries: revision number, node id, parent revisions etc.
106 pub fn as_revlog_entry(&self) -> &RevlogEntry {
107 &self.revlog_entry
108 }
109 }
110
73 111 /// `Changelog` entry which knows how to interpret the `changelog` data bytes.
74 112 #[derive(PartialEq)]
75 113 pub struct ChangelogRevisionData<'changelog> {
General Comments 0
You need to be logged in to leave comments. Login now