lib.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #[macro_use] extern crate failure;
  2. pub mod contlines;
  3. use contlines::ContinuationLines;
  4. struct ParsingContext {
  5. current_record_type: Option<String>,
  6. }
  7. #[derive(Eq, PartialEq, Debug)]
  8. pub struct Record {
  9. pub rec_type: Option<String>,
  10. pub fields: Vec<(String, String)>,
  11. }
  12. impl Record {
  13. pub fn write<W>(&self, w: &mut W) -> std::io::Result<()>
  14. where W: std::io::Write
  15. {
  16. for &(ref name, ref value) in self.fields.iter() {
  17. write!(w, "{}: {}\n", name, value)?;
  18. }
  19. write!(w, "\n")
  20. }
  21. pub fn size(&self) -> usize {
  22. self.fields.len()
  23. }
  24. }
  25. #[derive(Eq, PartialEq, Debug)]
  26. pub struct Recfile {
  27. pub records: Vec<Record>,
  28. }
  29. impl Recfile {
  30. pub fn write<W>(&self, w: &mut W) -> std::io::Result<()>
  31. where W: std::io::Write
  32. {
  33. for r in self.records.iter() {
  34. r.write(w)?;
  35. }
  36. Ok(())
  37. }
  38. pub fn filter_by_type(&mut self, type_name: &str) {
  39. self.records.retain(|r| match r.rec_type {
  40. Some(ref t) => t == type_name,
  41. None => false,
  42. });
  43. }
  44. }
  45. #[derive(Debug, Fail)]
  46. pub enum RecError {
  47. #[fail(display = "Error parsing records: {}", message)]
  48. GenericError {
  49. message: String,
  50. },
  51. #[fail(display = "Found cont line in nonsensical place: {}", ln)]
  52. BadContLine {
  53. ln: String,
  54. },
  55. #[fail(display = "Invalid line: {}", ln)]
  56. InvalidLine {
  57. ln: String,
  58. },
  59. }
  60. impl Recfile {
  61. pub fn parse<I>(i: I) -> Result<Recfile, RecError>
  62. where I: std::io::BufRead
  63. {
  64. let mut iter = ContinuationLines::new(i.lines());
  65. let mut current = Record {
  66. fields: vec![],
  67. rec_type: None,
  68. };
  69. let mut buf = vec![];
  70. let mut ctx = ParsingContext {
  71. current_record_type: None,
  72. };
  73. while let Some(Ok(ln)) = iter.next() {
  74. let ln = ln.trim_left_matches(' ');
  75. if ln.starts_with('#') {
  76. // skip comment lines
  77. } else if ln.is_empty() {
  78. if !current.fields.is_empty() {
  79. buf.push(current);
  80. current = Record {
  81. rec_type: ctx.current_record_type.clone(),
  82. fields: vec![],
  83. };
  84. }
  85. } else if ln.starts_with('+') {
  86. if let Some(val) = current.fields.last_mut() {
  87. val.1.push_str("\n");
  88. val.1.push_str(
  89. if ln[1..].starts_with(' ') {
  90. &ln[2..]
  91. } else {
  92. &ln[1..]
  93. });
  94. } else {
  95. return Err(RecError::BadContLine{ ln: ln.to_owned() });
  96. }
  97. } else if let Some(pos) = ln.find(':') {
  98. let (key, val) = ln.split_at(pos);
  99. current.fields.push((
  100. key.to_owned(),
  101. val[1..].trim_left().to_owned()));
  102. if key == "%rec" {
  103. ctx.current_record_type = Some(val[1..].trim_left().to_owned());
  104. }
  105. } else {
  106. return Err(RecError::InvalidLine { ln: ln.to_owned() });
  107. }
  108. }
  109. if !current.fields.is_empty() {
  110. buf.push(current);
  111. }
  112. Ok(Recfile { records: buf })
  113. }
  114. }
  115. #[cfg(test)]
  116. mod tests {
  117. use ::{Recfile,Record};
  118. fn test_parse(input: &[u8], expected: Vec<Vec<(&str, &str)>>) {
  119. let file = Recfile {
  120. records: expected.iter().map( |v| {
  121. Record {
  122. rec_type: None,
  123. fields: v.iter().map( |&(k, v)| {
  124. (k.to_owned(), v.to_owned())
  125. }).collect(),
  126. }
  127. }).collect(),
  128. };
  129. assert_eq!(Recfile::parse(input), Ok(file));
  130. }
  131. #[test]
  132. fn empty_file() {
  133. test_parse(b"\n", vec![]);
  134. }
  135. #[test]
  136. fn only_comments() {
  137. test_parse(b"# an empty file\n", vec![]);
  138. }
  139. #[test]
  140. fn one_section() {
  141. test_parse(b"hello: yes\n", vec![ vec![ ("hello", "yes") ] ]);
  142. }
  143. #[test]
  144. fn two_sections() {
  145. test_parse(
  146. b"hello: yes\n\ngoodbye: no\n",
  147. vec![
  148. vec![ ("hello", "yes") ],
  149. vec![ ("goodbye", "no") ],
  150. ],
  151. );
  152. }
  153. #[test]
  154. fn continuation_with_space() {
  155. test_parse(
  156. b"hello: yes\n+ but also no\n",
  157. vec![
  158. vec![ ("hello", "yes\nbut also no") ],
  159. ],
  160. );
  161. }
  162. #[test]
  163. fn continuation_without_space() {
  164. test_parse(
  165. b"hello: yes\n+but also no\n",
  166. vec![
  167. vec![ ("hello", "yes\nbut also no") ],
  168. ],
  169. );
  170. }
  171. #[test]
  172. fn continuation_with_two_spaces() {
  173. test_parse(
  174. b"hello: yes\n+ but also no\n",
  175. vec![
  176. vec![ ("hello", "yes\n but also no") ],
  177. ],
  178. );
  179. }
  180. }