use std::fmt::Debug;
use std::task::{Context, Poll};
use glaredb_error::{DbError, Result};
use super::FileType;
#[derive(Debug, Clone)]
pub struct DirEntry {
pub(crate) path: String,
pub(crate) file_type: FileType,
}
impl DirEntry {
pub fn new(path: impl Into<String>, file_type: FileType) -> Self {
let mut path = path.into();
if path.ends_with('/') {
path.pop();
}
DirEntry { path, file_type }
}
pub fn new_file(path: impl Into<String>) -> Self {
Self::new(path, FileType::File)
}
pub fn new_dir(path: impl Into<String>) -> Self {
Self::new(path, FileType::Directory)
}
}
pub trait ReadDirHandle: Debug + Sync + Send + Sized + 'static {
fn poll_list(&mut self, cx: &mut Context, ents: &mut Vec<DirEntry>) -> Poll<Result<usize>>;
fn change_dir(&mut self, relative: impl Into<String>) -> Result<Self>;
}
#[derive(Debug, Clone)]
pub struct DirHandleNotImplemented;
impl ReadDirHandle for DirHandleNotImplemented {
fn poll_list(&mut self, _cx: &mut Context, _ents: &mut Vec<DirEntry>) -> Poll<Result<usize>> {
Poll::Ready(Err(DbError::new(
"Dir handle not implemented for this file system",
)))
}
fn change_dir(&mut self, _relative: impl Into<String>) -> Result<Self> {
Err(DbError::new(
"Dir handle not implemented for this file system",
))
}
}