Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #![allow(dead_code)]
  2.  
  3. use std::borrow::Cow;
  4. use std::collections::{BTreeMap, VecDeque};
  5. use std::path::Path;
  6.  
  7. enum FileWalker<'p> {
  8. Parent(ParentFileWalker<'p>),
  9. Child(ChildFileWalker<'p>),
  10. }
  11.  
  12. struct ParentFileWalker<'p>(Option<&'p Path>);
  13.  
  14. impl<'p> ParentFileWalker<'p> {
  15. pub fn new(origin_item_path: &'p Path) -> Self {
  16. Self(Some(origin_item_path))
  17. }
  18. }
  19.  
  20. struct ChildFileWalker<'p> {
  21. frontier: VecDeque<Result<Cow<'p, Path>, Error>>,
  22. last_processed_path: Option<Cow<'p, Path>>,
  23. }
  24.  
  25. struct Selection;
  26.  
  27. type MetaBlock<'k> = BTreeMap<MetaKey<'k>, MetaVal<'k>>;
  28.  
  29. enum MetaBlockStream<'mb> {
  30. Fixed(FixedMetaBlockStream<'mb>),
  31. File(FileMetaBlockStream<'mb>),
  32. }
  33.  
  34. struct FixedMetaBlockStream<'mb>(VecDeque<(Cow<'mb, Path>, MetaBlock<'mb>)>);
  35.  
  36. struct FileMetaBlockStream<'mb> {
  37. file_walker: FileWalker<'mb>,
  38. selection: &'mb Selection,
  39. }
  40.  
  41. impl<'mb> FileMetaBlockStream<'mb> {
  42. fn new<FW>(
  43. file_walker: FW,
  44. selection: &'mb Selection,
  45. ) -> Self
  46. where
  47. FW: Into<FileWalker<'mb>>,
  48. {
  49. FileMetaBlockStream {
  50. file_walker: file_walker.into(),
  51. selection,
  52. }
  53. }
  54. }
  55.  
  56. #[derive(Debug, PartialEq)]
  57. enum MetaVal<'k> {
  58. Nil,
  59. Str(String),
  60. Seq(Vec<MetaVal<'k>>),
  61. Map(BTreeMap<MetaKey<'k>, MetaVal<'k>>),
  62. Int(i64),
  63. Bul(bool),
  64. }
  65.  
  66. #[derive(Clone, Debug, PartialEq)]
  67. enum MetaKey<'k> {
  68. Str(Cow<'k, str>),
  69. }
  70.  
  71. impl<'k> From<&'k str> for MetaKey<'k> {
  72. fn from(s: &'k str) -> Self {
  73. MetaKey::Str(Cow::Borrowed(s))
  74. }
  75. }
  76.  
  77. impl<'k> From<&str> for MetaVal<'k> {
  78. fn from(s: &str) -> Self {
  79. MetaVal::Str(s.to_string())
  80. }
  81. }
  82.  
  83. struct MetaValueStream<'vs> {
  84. target_key_path: MetaKeyPath<'vs>,
  85. meta_block_stream: MetaBlockStream<'vs>,
  86. }
  87.  
  88. impl<'vs> MetaValueStream<'vs> {
  89. pub fn new<MBS>(target_key_path: MetaKeyPath<'vs>, meta_block_stream: MBS) -> Self
  90. where
  91. MBS: Into<MetaBlockStream<'vs>>,
  92. {
  93. Self {
  94. target_key_path,
  95. meta_block_stream: meta_block_stream.into(),
  96. }
  97. }
  98. }
  99.  
  100. #[derive(Debug)]
  101. struct Error;
  102.  
  103. impl<'vs> Iterator for MetaValueStream<'vs> {
  104. type Item = Result<(Cow<'vs, Path>, MetaVal<'vs>), Error>;
  105.  
  106. fn next(&mut self) -> Option<Self::Item> { None }
  107. }
  108.  
  109. #[derive(Clone)]
  110. struct MetaKeyPath<'k>(Cow<'k, [MetaKey<'k>]>);
  111.  
  112. impl<'k> From<&'k str> for MetaKeyPath<'k> {
  113. fn from(s: &'k str) -> Self {
  114. let mk: MetaKey<'k> = s.into();
  115. mk.into()
  116. }
  117. }
  118.  
  119. impl<'k> From<MetaKey<'k>> for MetaKeyPath<'k> {
  120. fn from(mk: MetaKey<'k>) -> Self {
  121. Self(Cow::Owned(vec![mk]))
  122. }
  123. }
  124.  
  125. fn foo() {
  126. let root_dir = Path::new("/root");
  127. let selection = Selection;
  128. let target_key_path = MetaKeyPath::from("flag_key");
  129. let origin_path = root_dir.join("blah");
  130. let file_walker = FileWalker::Parent(ParentFileWalker::new(&origin_path));
  131. let block_stream = MetaBlockStream::File(FileMetaBlockStream::new(
  132. file_walker,
  133. &selection,
  134. ));
  135. let expected = vec![
  136. (Cow::Owned(root_dir.join("0").join("0_1").join("0_1_2")), MetaVal::from("0_1_2")),
  137. (Cow::Owned(root_dir.join("0").join("0_1")), MetaVal::from("0_1")),
  138. (Cow::Owned(root_dir.join("0")), MetaVal::from("0")),
  139. ];
  140. let produced = {
  141. MetaValueStream::new(target_key_path.clone(), block_stream)
  142. .into_iter()
  143. .map(|res| res.unwrap())
  144. .collect::<Vec<_>>()
  145. };
  146. assert_eq!(expected, produced);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement