debian_watch/lib.rs
1#![deny(missing_docs)]
2//! Formatting-preserving parser and editor for Debian watch files
3//!
4//! # Example
5//!
6//! ```rust
7//! let wf = debian_watch::WatchFile::new(None);
8//! assert_eq!(wf.version(), debian_watch::DEFAULT_VERSION);
9//! assert_eq!("", wf.to_string());
10//!
11//! let wf = debian_watch::WatchFile::new(Some(4));
12//! assert_eq!(wf.version(), 4);
13//! assert_eq!("version=4\n", wf.to_string());
14//!
15//! let wf: debian_watch::WatchFile = r#"version=4
16//! opts=foo=blah https://foo.com/bar .*/v?(\d\S+)\.tar\.gz
17//! "#.parse().unwrap();
18//! assert_eq!(wf.version(), 4);
19//! assert_eq!(wf.entries().collect::<Vec<_>>().len(), 1);
20//! let entry = wf.entries().next().unwrap();
21//! assert_eq!(entry.opts(), maplit::hashmap! {
22//! "foo".to_string() => "blah".to_string(),
23//! });
24//! assert_eq!(&entry.url(), "https://foo.com/bar");
25//! assert_eq!(entry.matching_pattern().as_deref(), Some(".*/v?(\\d\\S+)\\.tar\\.gz"));
26//! ```
27
28mod lex;
29mod parse;
30
31/// Any watch files without a version are assumed to be
32/// version 1.
33pub const DEFAULT_VERSION: u32 = 1;
34
35mod types;
36
37pub use types::*;
38
39/// Let's start with defining all kinds of tokens and
40/// composite nodes.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
42#[allow(non_camel_case_types, missing_docs, clippy::upper_case_acronyms)]
43#[repr(u16)]
44pub(crate) enum SyntaxKind {
45 KEY = 0,
46 VALUE,
47 EQUALS,
48 QUOTE,
49 COMMA,
50 CONTINUATION,
51 NEWLINE,
52 WHITESPACE, // whitespaces is explicit
53 COMMENT, // comments
54 ERROR, // as well as errors
55
56 // composite nodes
57 ROOT, // The entire file
58 VERSION, // "version=x\n"
59 ENTRY, // "opts=foo=blah https://foo.com/bar .*/v?(\d\S+)\.tar\.gz\n"
60 OPTS_LIST, // "opts=foo=blah"
61 OPTION, // "foo=blah"
62}
63
64/// Convert our `SyntaxKind` into the rowan `SyntaxKind`.
65impl From<SyntaxKind> for rowan::SyntaxKind {
66 fn from(kind: SyntaxKind) -> Self {
67 Self(kind as u16)
68 }
69}
70
71pub use crate::parse::Entry;
72pub use crate::parse::WatchFile;
73
74#[cfg(test)]
75mod tests {
76 #[test]
77 fn test_create_watchfile() {
78 let wf = super::WatchFile::new(None);
79 assert_eq!(wf.version(), super::DEFAULT_VERSION);
80
81 assert_eq!("", wf.to_string());
82
83 let wf = super::WatchFile::new(Some(4));
84 assert_eq!(wf.version(), 4);
85
86 assert_eq!("version=4\n", wf.to_string());
87 }
88
89 #[test]
90 fn test_set_version() {
91 let mut wf = super::WatchFile::new(Some(4));
92 assert_eq!(wf.version(), 4);
93
94 wf.set_version(5);
95 assert_eq!(wf.version(), 5);
96 assert_eq!("version=5\n", wf.to_string());
97
98 // Test setting version on a file without version
99 let mut wf = super::WatchFile::new(None);
100 assert_eq!(wf.version(), super::DEFAULT_VERSION);
101
102 wf.set_version(4);
103 assert_eq!(wf.version(), 4);
104 assert_eq!("version=4\n", wf.to_string());
105 }
106}