Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- use std::borrow::Cow;
- use std::collections::{BTreeMap, VecDeque};
- use std::path::Path;
- enum FileWalker<'p> {
- Parent(ParentFileWalker<'p>),
- Child(ChildFileWalker<'p>),
- }
- struct ParentFileWalker<'p>(Option<&'p Path>);
- impl<'p> ParentFileWalker<'p> {
- pub fn new(origin_item_path: &'p Path) -> Self {
- Self(Some(origin_item_path))
- }
- }
- struct ChildFileWalker<'p> {
- frontier: VecDeque<Result<Cow<'p, Path>, Error>>,
- last_processed_path: Option<Cow<'p, Path>>,
- }
- struct Selection;
- type MetaBlock<'k> = BTreeMap<MetaKey<'k>, MetaVal<'k>>;
- enum MetaBlockStream<'mb> {
- Fixed(FixedMetaBlockStream<'mb>),
- File(FileMetaBlockStream<'mb>),
- }
- struct FixedMetaBlockStream<'mb>(VecDeque<(Cow<'mb, Path>, MetaBlock<'mb>)>);
- struct FileMetaBlockStream<'mb> {
- file_walker: FileWalker<'mb>,
- selection: &'mb Selection,
- }
- impl<'mb> FileMetaBlockStream<'mb> {
- fn new<FW>(
- file_walker: FW,
- selection: &'mb Selection,
- ) -> Self
- where
- FW: Into<FileWalker<'mb>>,
- {
- FileMetaBlockStream {
- file_walker: file_walker.into(),
- selection,
- }
- }
- }
- #[derive(Debug, PartialEq)]
- enum MetaVal<'k> {
- Nil,
- Str(String),
- Seq(Vec<MetaVal<'k>>),
- Map(BTreeMap<MetaKey<'k>, MetaVal<'k>>),
- Int(i64),
- Bul(bool),
- }
- #[derive(Clone, Debug, PartialEq)]
- enum MetaKey<'k> {
- Str(Cow<'k, str>),
- }
- impl<'k> From<&'k str> for MetaKey<'k> {
- fn from(s: &'k str) -> Self {
- MetaKey::Str(Cow::Borrowed(s))
- }
- }
- impl<'k> From<&str> for MetaVal<'k> {
- fn from(s: &str) -> Self {
- MetaVal::Str(s.to_string())
- }
- }
- struct MetaValueStream<'vs> {
- target_key_path: MetaKeyPath<'vs>,
- meta_block_stream: MetaBlockStream<'vs>,
- }
- impl<'vs> MetaValueStream<'vs> {
- pub fn new<MBS>(target_key_path: MetaKeyPath<'vs>, meta_block_stream: MBS) -> Self
- where
- MBS: Into<MetaBlockStream<'vs>>,
- {
- Self {
- target_key_path,
- meta_block_stream: meta_block_stream.into(),
- }
- }
- }
- #[derive(Debug)]
- struct Error;
- impl<'vs> Iterator for MetaValueStream<'vs> {
- type Item = Result<(Cow<'vs, Path>, MetaVal<'vs>), Error>;
- fn next(&mut self) -> Option<Self::Item> { None }
- }
- #[derive(Clone)]
- struct MetaKeyPath<'k>(Cow<'k, [MetaKey<'k>]>);
- impl<'k> From<&'k str> for MetaKeyPath<'k> {
- fn from(s: &'k str) -> Self {
- let mk: MetaKey<'k> = s.into();
- mk.into()
- }
- }
- impl<'k> From<MetaKey<'k>> for MetaKeyPath<'k> {
- fn from(mk: MetaKey<'k>) -> Self {
- Self(Cow::Owned(vec![mk]))
- }
- }
- fn foo() {
- let root_dir = Path::new("/root");
- let selection = Selection;
- let target_key_path = MetaKeyPath::from("flag_key");
- let origin_path = root_dir.join("blah");
- let file_walker = FileWalker::Parent(ParentFileWalker::new(&origin_path));
- let block_stream = MetaBlockStream::File(FileMetaBlockStream::new(
- file_walker,
- &selection,
- ));
- let expected = vec![
- (Cow::Owned(root_dir.join("0").join("0_1").join("0_1_2")), MetaVal::from("0_1_2")),
- (Cow::Owned(root_dir.join("0").join("0_1")), MetaVal::from("0_1")),
- (Cow::Owned(root_dir.join("0")), MetaVal::from("0")),
- ];
- let produced = {
- MetaValueStream::new(target_key_path.clone(), block_stream)
- .into_iter()
- .map(|res| res.unwrap())
- .collect::<Vec<_>>()
- };
- assert_eq!(expected, produced);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement