dap/utils.rs
1use std::fmt::{Display, Formatter};
2
3/// A struct representing a version of the DAP specification.
4/// This version corresponds to the [changelog](https://microsoft.github.io/debug-adapter-protocol/changelog)
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct Version {
7 /// Major version.
8 pub major: i64,
9 /// Minor version.
10 pub minor: i64,
11 /// Patch version. Historically, this is "x" in the changelog for most versions. That value
12 /// is represented as `None` here.
13 pub patch: Option<i64>,
14 /// The git commit in the DAP repo that corresponds to this version.
15 /// Please note that historically, versions (as of 1.62.x) are not tagged in the DAP repo.
16 /// Until that changes, we are using the commit that updates the JSON-schema in the gh-pages
17 /// branch.
18 pub git_commit: String,
19}
20
21impl Display for Version {
22 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23 match self.patch {
24 Some(patch) => write!(f, "{}.{}.{}", self.major, self.minor, patch),
25 None => write!(f, "{}.{}.x", self.major, self.minor),
26 }
27 }
28}
29
30/// Returns the version of the DAP specification that this crate implements.
31///
32/// Please note that historically, the DAP changelog hasn't been super accurate and the
33/// versions (as of 1.62.x) are not tagged in the DAP repo. Until that changes, we are
34/// using the commit that *adds the corresponding JSON-schema in the **gh-pages** branch*.
35pub fn get_spec_version() -> Version {
36 Version {
37 major: 1,
38 minor: 62,
39 patch: None,
40 git_commit: "7f284b169ecd19602487eb4d290ae651d4398ce7".to_string(),
41 }
42}
43
44#[cfg(test)]
45mod test {
46 use super::*;
47
48 #[test]
49 fn test_version_display() {
50 let version = Version {
51 major: 1,
52 minor: 62,
53 patch: None,
54 git_commit: "7f284b169ecd19602487eb4d290ae651d4398ce7".to_string(),
55 };
56 assert_eq!(version.to_string(), "1.62.x");
57
58 let version = Version {
59 major: 1,
60 minor: 62,
61 patch: Some(1),
62 git_commit: "7f284b169ecd19602487eb4d290ae651d4398ce7".to_string(),
63 };
64 assert_eq!(version.to_string(), "1.62.1");
65 }
66}