Implement project tree

This commit is contained in:
Jeremy Soller 2023-10-27 11:43:30 -06:00
parent 8566c6f31f
commit 05368dea2c
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
3 changed files with 259 additions and 75 deletions

View file

@ -1,28 +1,80 @@
// SPDX-License-Identifier: GPL-3.0-only
use std::{
fs, io,
path::{Path, PathBuf},
};
pub struct Project {
path: PathBuf,
name: String,
#[derive(Clone, Debug)]
pub enum ProjectNode {
Root {
name: String,
path: PathBuf,
open: bool,
},
Folder {
name: String,
path: PathBuf,
open: bool,
},
File {
name: String,
path: PathBuf,
},
}
impl Project {
impl ProjectNode {
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
let path = fs::canonicalize(path)?;
let name = path
.file_name()
.ok_or(io::Error::new(
io::ErrorKind::Other,
format!("Path {:?} has no file name", path),
format!("path {:?} has no file name", path),
))?
.to_str()
.ok_or(io::Error::new(
io::ErrorKind::Other,
format!("Path {:?} is not valid UTF-8", path),
format!("path {:?} is not valid UTF-8", path),
))?
.to_string();
Ok(Self { path, name })
Ok(if path.is_dir() {
Self::Folder {
path,
name,
open: false,
}
} else {
Self::File { path, name }
})
}
pub fn icon_name(&self) -> &str {
match self {
//TODO: different icon for project?
ProjectNode::Root { open, .. } => {
if *open {
"go-down-symbolic"
} else {
"go-next-symbolic"
}
}
ProjectNode::Folder { open, .. } => {
if *open {
"go-down-symbolic"
} else {
"go-next-symbolic"
}
}
ProjectNode::File { .. } => "text-x-generic",
}
}
pub fn name(&self) -> &str {
match self {
ProjectNode::Root { name, .. } => name,
ProjectNode::Folder { name, .. } => name,
ProjectNode::File { name, .. } => name,
}
}
}