Skip to content

Commit

Permalink
added source finder
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 8, 2024
1 parent 2e03242 commit e7aae14
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
38 changes: 34 additions & 4 deletions src/compiler/compiler_source.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::path::PathBuf;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use clap::builder::Str;

pub(crate) trait CompilerSource {
fn read(&self) -> String;
fn read(&self) -> Result<String, std::io::Error>;

fn path(&self) -> PathBuf;
}
Expand All @@ -23,8 +25,36 @@ impl StringCompilerSource {
}

impl CompilerSource for StringCompilerSource {
fn read(&self) -> String {
return self.content.to_string();
fn read(&self) -> Result<String, std::io::Error> {
return Ok(self.content.clone())
}

fn path(&self) -> PathBuf {
return self.path.clone();
}
}

pub(crate) struct FileCompilerSource {
path: PathBuf,
}

impl FileCompilerSource {

pub(crate) fn new(path: PathBuf) -> FileCompilerSource {
return FileCompilerSource {
path: path,
};
}
}

impl CompilerSource for FileCompilerSource {

fn read(&self) -> Result<String, std::io::Error> {
let mut p = File::open(self.path.clone())?;
let mut buff = String::new();
p.read_to_string(&mut buff)?;

return Ok(buff);
}

fn path(&self) -> PathBuf {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ mod compiler_context;
mod compiler;
mod compiler_source;
mod compilation_result;
mod source_finder;

pub(crate) use compiler_context::HexoCompilerContext;
pub(crate) use compiler::HexoCompiler;
pub(crate) use compiler_source::{CompilerSource, StringCompilerSource};
pub(crate) use compiler_source::{CompilerSource, StringCompilerSource, FileCompilerSource};
pub(crate) use compilation_result::CompilationResult;


Expand Down
23 changes: 23 additions & 0 deletions src/compiler/source_finder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::path::PathBuf;
use crate::compiler::{CompilerSource, FileCompilerSource, StringCompilerSource};

pub(crate) trait SourceFinder {

fn find(&self, path: PathBuf) -> Option<impl CompilerSource>;
}

struct FileSourceFinder {
root_dir: PathBuf
}

impl SourceFinder for FileSourceFinder {

fn find(&self, path: PathBuf) -> Option<impl CompilerSource> {
let path = self.root_dir.join(path);

let source = FileCompilerSource::new(path);
Some(
source
)
}
}

0 comments on commit e7aae14

Please sign in to comment.