opendal/services/mini_moka/lister.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19use std::vec::IntoIter;
20
21use super::core::MiniMokaCore;
22use crate::raw::oio;
23use crate::raw::*;
24use crate::*;
25
26pub struct MiniMokaLister {
27 root: String,
28 keys: IntoIter<String>,
29}
30
31impl MiniMokaLister {
32 pub fn new(core: Arc<MiniMokaCore>, root: String, _path: String) -> Self {
33 // Get all keys from the cache
34 let keys: Vec<String> = core
35 .cache
36 .iter()
37 .map(|entry| entry.key().to_string())
38 .collect();
39
40 Self {
41 root,
42 keys: keys.into_iter(),
43 }
44 }
45}
46
47impl oio::List for MiniMokaLister {
48 async fn next(&mut self) -> Result<Option<oio::Entry>> {
49 match self.keys.next() {
50 Some(key) => {
51 // Convert absolute path to relative path
52 let rel_path = build_rel_path(&self.root, &key);
53
54 // Determine if it's a file or directory based on trailing slash
55 let mode = if key.ends_with('/') {
56 EntryMode::DIR
57 } else {
58 EntryMode::FILE
59 };
60
61 let metadata = Metadata::new(mode);
62
63 Ok(Some(oio::Entry::new(&rel_path, metadata)))
64 }
65 None => Ok(None),
66 }
67 }
68}