From b22c348d8f49e9c4617c8b63914fde307c9ffff9 Mon Sep 17 00:00:00 2001 From: NotAFile Date: Sun, 23 Jan 2022 22:51:39 +0100 Subject: [PATCH] use package system --- src/main.rs | 5 ++++- src/package.rs | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 83c05af..e58f95c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,10 @@ struct Opt { fn main() { let opt = Opt::from_args(); - let mut infile = File::open(opt.input).expect("could not open file"); + // let mut infile = File::open(opt.input).expect("could not open file"); + let packages = package::PackageRegistry::new(); + let mut infile = packages.get("builtins").expect("no package").open().expect("could not open file"); + let mut input = String::new(); infile .read_to_string(&mut input) diff --git a/src/package.rs b/src/package.rs index 6d9e840..ad4a1d0 100644 --- a/src/package.rs +++ b/src/package.rs @@ -1,17 +1,25 @@ use std::collections::BTreeMap; use std::path::PathBuf; +use std::fs::File; -struct Package { +pub struct Package { name: String, path: PathBuf, } -struct PackageRegistry { +impl Package { + pub fn open(&self) -> Result { + let filepath = self.path.with_file_name("main.hyd"); + File::open(filepath) + } +} + +pub struct PackageRegistry { packages: BTreeMap } impl PackageRegistry { - fn new() -> Self { + pub fn new() -> Self { let mut packages = BTreeMap::new(); packages.insert("builtins".to_string(), Package { name: "builtins".to_string(), @@ -19,4 +27,8 @@ impl PackageRegistry { }); Self { packages } } + + pub fn get(&self, name: &str) -> Option<&Package> { + self.packages.get(name) + } }