From e7aae140b4a1fdad5928e9f3989feb8b14f1e9f9 Mon Sep 17 00:00:00 2001 From: Aleksey Yakovlev Date: Wed, 8 May 2024 11:17:53 +0700 Subject: [PATCH] added source finder --- src/compiler/compiler_source.rs | 38 +++++++++++++++++++++++++++++---- src/compiler/mod.rs | 3 ++- src/compiler/source_finder.rs | 23 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/compiler/source_finder.rs diff --git a/src/compiler/compiler_source.rs b/src/compiler/compiler_source.rs index 5044476..2ddda4d 100644 --- a/src/compiler/compiler_source.rs +++ b/src/compiler/compiler_source.rs @@ -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; fn path(&self) -> PathBuf; } @@ -23,8 +25,36 @@ impl StringCompilerSource { } impl CompilerSource for StringCompilerSource { - fn read(&self) -> String { - return self.content.to_string(); + fn read(&self) -> Result { + 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 { + 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 { diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index a5450a6..b2dbe99 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -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; diff --git a/src/compiler/source_finder.rs b/src/compiler/source_finder.rs new file mode 100644 index 0000000..38c1371 --- /dev/null +++ b/src/compiler/source_finder.rs @@ -0,0 +1,23 @@ +use std::path::PathBuf; +use crate::compiler::{CompilerSource, FileCompilerSource, StringCompilerSource}; + +pub(crate) trait SourceFinder { + + fn find(&self, path: PathBuf) -> Option; +} + +struct FileSourceFinder { + root_dir: PathBuf +} + +impl SourceFinder for FileSourceFinder { + + fn find(&self, path: PathBuf) -> Option { + let path = self.root_dir.join(path); + + let source = FileCompilerSource::new(path); + Some( + source + ) + } +} \ No newline at end of file