Skip to content

Commit

Permalink
Rework workspace paths, reorganize modules, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Jan 17, 2025
1 parent 99a91d4 commit bf1a876
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 117 deletions.
13 changes: 10 additions & 3 deletions workspace/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@ pub enum GTWError {
#[diagnostic(code(GTW001))]
FilesLock,

#[error("`{0}` not found")]
#[error("Can't resolve `{0}`")]
#[diagnostic(
code(GTW101),
help("Use an absolute path or provide a working directory and make sure it exists")
)]
ResolvePath(String),

#[error("`{0}` not found")]
#[diagnostic(
code(GTW102),
help("Make sure the path is absolute or can be resolved from the current directory")
)]
CanonicalizePath(String),

#[error("Can't detect the kind of `{0}`")]
#[diagnostic(
code(GTW102),
code(GTW103),
help("The file must be named `genotype(.*)?.toml` or `*.type`")
)]
DetectKind(String),

#[error("Can't read `{0}`")]
#[diagnostic(
code(GTW103),
code(GTW104),
help("Does the file exist? Do you have the right permissions?")
)]
ReadSource(String),
Expand Down
73 changes: 0 additions & 73 deletions workspace/src/file.rs

This file was deleted.

28 changes: 28 additions & 0 deletions workspace/src/file/kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use miette::Result;

use crate::{error::GTWError, path::GTWPath};

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum GTWFileKind {
Config,
Module,
}

impl GTWFileKind {
pub fn detect(path: &GTWPath) -> Result<Self> {
let path = path.as_path();
let ext = path.extension().and_then(|ext| ext.to_str());
match ext {
Some("toml") => {
if path.starts_with("genotype") {
return Ok(GTWFileKind::Config);
}
}
Some("type") => {
return Ok(GTWFileKind::Module);
}
_ => {}
}
Err(GTWError::DetectKind(path.display().to_string()).into())
}
}
41 changes: 41 additions & 0 deletions workspace/src/file/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use genotype_config::GTConfig;
use indexmap::IndexMap;
use miette::Result;
use std::sync::{Arc, Mutex};

use crate::path::GTWPath;

pub mod source;
pub use source::*;

pub mod payload;
pub use payload::*;

pub mod kind;
pub use kind::*;

pub type GTWFiles = Arc<Mutex<IndexMap<GTWPath, GTWFile>>>;

pub struct GTWFile {
source: GTWFileSource,
payload: GTWFilePayload,
}

impl GTWFile {
pub fn load(path: &GTWPath, source: GTWFileSource) -> Result<Self> {
match GTWFileKind::detect(path)? {
GTWFileKind::Config => {}

GTWFileKind::Module => {}
}

Ok(GTWFile {
source,
payload: GTWFilePayload::Config(GTConfig::default()),
})
}

pub fn same_hash(&self, source: &GTWFileSource) -> bool {
self.source.same_hash(source)
}
}
7 changes: 7 additions & 0 deletions workspace/src/file/payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use genotype_config::GTConfig;
use genotype_parser::GTModuleParse;

pub enum GTWFilePayload {
Config(GTConfig),
Module(GTModuleParse),
}
34 changes: 34 additions & 0 deletions workspace/src/file/source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::fs::read_to_string;

use miette::Result;
use sha2::{Digest, Sha256};

use crate::{error::GTWError, path::GTWPath};

pub struct GTWFileSource {
hash: String,
content: String,
}

impl GTWFileSource {
pub fn read(path: &GTWPath) -> Result<GTWFileSource> {
let content =
read_to_string(path.as_path()).map_err(|_| GTWError::ReadSource(path.into()))?;

Ok(GTWFileSource {
hash: Self::hash(&content),
content,
})
}

pub fn hash(content: &String) -> String {
let mut hasher = Sha256::new();
hasher.update(content.as_bytes());
let result = hasher.finalize();
format!("{:x}", result)
}

pub fn same_hash(&self, source: &GTWFileSource) -> bool {
self.hash == source.hash
}
}
62 changes: 27 additions & 35 deletions workspace/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
use std::path::PathBuf;
use std::{env, path::PathBuf};

use miette::Result;

use crate::error::GTWError;

/// Workspace path. It holds the absolute canonical path of a file or directory.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct GTWPath {
/// Absolute canonical path.
path: PathBuf,
kind: GTWPathKind,
}

impl GTWPath {
pub fn new(path: &String) -> Result<GTWPath> {
let absolute_path = PathBuf::from(path)
/// Creates a new `GTWPath` from a string. A relative path will resolve to
/// the passed working directory or the current directory. The path will
/// be canonicalized.
pub fn try_new(path_str: &String, cwd: Option<&GTWPath>) -> Result<GTWPath> {
let path = PathBuf::from(path_str);

let path = match path.is_absolute() {
true => path,

false => match cwd {
Some(root) => root.path.join(path),
None => match cwd {
Some(root) => root.path.join(path),
None => match env::current_dir() {
Ok(cwd) => cwd.join(path),
Err(_) => return Err(GTWError::ResolvePath(path_str.clone()).into()),
},
},
},
};

let path = path
.canonicalize()
.map_err(|_| GTWError::CanonicalizePath(path.clone()))?;
let kind = GTWPath::detect_kind(&absolute_path)?;
Ok(GTWPath {
path: absolute_path,
kind,
})
}
.map_err(|_| GTWError::CanonicalizePath(path_str.clone()))?;

pub fn detect_kind(path: &PathBuf) -> Result<GTWPathKind> {
let ext = path.extension().and_then(|ext| ext.to_str());
match ext {
Some("toml") => {
if path.starts_with("genotype") {
return Ok(GTWPathKind::Config);
}
}
Some("type") => {
return Ok(GTWPathKind::Module);
}
_ => {}
}
Err(GTWError::DetectKind(path.display().to_string()).into())
Ok(GTWPath { path })
}

pub fn as_path(&self) -> &PathBuf {
&self.path
}

pub fn kind(&self) -> &GTWPathKind {
&self.kind
}
}

impl From<GTWPath> for String {
Expand All @@ -58,9 +56,3 @@ impl From<&GTWPath> for String {
path.path.display().to_string()
}
}

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum GTWPathKind {
Config,
Module,
}
25 changes: 19 additions & 6 deletions workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,31 @@ use crate::{
};

pub struct GTWorkspace {
/// Workspace root path. It helps to format relative paths, i.e. to display
/// errors in an editor, so that it can open the file.
path: GTWPath,
/// Workspace files map. It associated the absolute path with the file.
/// Note that the file doesn't necessarily is inside the workspace path,
/// as editors can open files outside the workspace.
files: GTWFiles,
}

impl<'a> GTWorkspace {
pub fn new() -> GTWorkspace {
GTWorkspace {
pub fn try_new(path_str: &String) -> Result<GTWorkspace> {
let path = GTWPath::try_new(path_str, None)?;

Ok(GTWorkspace {
path,
files: Arc::new(Mutex::new(IndexMap::new())),
}
})
}

pub fn load_file(&self, path: &String, processing: Arc<Mutex<HashSet<GTWPath>>>) -> Result<()> {
let path = GTWPath::new(path)?;
pub fn load_file(
&self,
path_str: &String,
processing: Arc<Mutex<HashSet<GTWPath>>>,
) -> Result<()> {
let path = GTWPath::try_new(path_str, Some(&self.path))?;

// Check if the file is already processing
{
Expand All @@ -49,7 +62,7 @@ impl<'a> GTWorkspace {
}

// Load the file
let file = GTWFile::load(&path, &source)?;
let file = GTWFile::load(&path, source)?;

// Update the files map
{
Expand Down

0 comments on commit bf1a876

Please sign in to comment.