Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic configuration w/Freight.toml file
This commit adds the beginning of a configuration file for Freight. Eventually we'll make it more robust with parsing when we can add serde and the like as a dependency but we're a long ways off from that. With this we can offload configuration from being hardcoded inside of Freight to a config file that users can use to tailor to their project. It also lets us get rid of the janky check for ".git" in root_dir and instead look for a manifest to work with, in this case Freight. Later on we'll need better toml support so that we can build Cargo projects
- Loading branch information
Showing
3 changed files
with
55 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
name = "freight" | ||
edition = "2021" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use super::Edition; | ||
use super::Result; | ||
use std::error::Error; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
pub struct Manifest { | ||
pub crate_name: String, | ||
pub edition: Edition, | ||
} | ||
|
||
impl Manifest { | ||
pub fn parse_from_file(path: impl AsRef<Path>) -> Result<Self> { | ||
let path = path.as_ref(); | ||
let mut crate_name = None; | ||
let mut edition = None; | ||
|
||
for line in fs::read_to_string(path)?.lines() { | ||
let mut split = line.split('='); | ||
let field = split.next().unwrap().trim(); | ||
let value = split.next().unwrap().trim(); | ||
|
||
match field { | ||
"name" => crate_name = Some(value.replace('"', "")), | ||
"edition" => { | ||
edition = Some(match value.replace('"', "").parse()? { | ||
2015 => Edition::E2015, | ||
2018 => Edition::E2018, | ||
2021 => Edition::E2021, | ||
edition => return Err(format!("Edition {edition} is unsupported").into()), | ||
}) | ||
} | ||
field => return Err(format!("Field {field} is unsupported").into()), | ||
} | ||
} | ||
|
||
Ok(Self { | ||
crate_name: crate_name.ok_or::<Box<dyn Error>>("name is a required field".into())?, | ||
edition: edition.ok_or::<Box<dyn Error>>("edition is a required field".into())?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters