Skip to content

Commit

Permalink
[esrlabs#1966] Add the BuildConfiguration struct
Browse files Browse the repository at this point in the history
The BuildConfiguration struct will hold the build configuration loaded
from the configuration file. For the moment it just loads the file
contents into a string and handles failures.
  • Loading branch information
sergio-bobillier committed Dec 6, 2023
1 parent d59df6f commit caffa9c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions build-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/*
39 changes: 39 additions & 0 deletions build-cli/src/build_configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use core::fmt;
use std::fs;

const DEFAULT_BUILD_CONFIG_FILE:&str = "build.toml";

pub struct ReadError {
pub file_name: String,
pub message: String
}

impl fmt::Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.file_name, self.message)
}
}

pub struct BuildConfiguration {
pub source_file: String,
text: String
}

impl BuildConfiguration {
pub fn from_file(file_name: Option<String>) -> Result<Self, ReadError> {
let actual_file_name = file_name.unwrap_or(DEFAULT_BUILD_CONFIG_FILE.to_string());
let result = fs::read_to_string(&actual_file_name);

match result {
Err(error) => {
Err(ReadError {
file_name: actual_file_name,
message: format!("Unable to read file: {}", error)
})
},
Ok(text) => {
Ok(Self { source_file: actual_file_name, text })
}
}
}
}
2 changes: 2 additions & 0 deletions build-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod build_configuration;

fn main() {
println!("Hello, world!");
}

0 comments on commit caffa9c

Please sign in to comment.