-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
46 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
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
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
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 |
---|---|---|
@@ -1,29 +1,48 @@ | ||
use super::types::Mapper; | ||
use crate::error::Error; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::path::Path; | ||
|
||
// A struct representing a static mapper using a HashMap of String keys to Values. | ||
pub struct BandStaticMapper { | ||
mapping: HashMap<String, Value>, | ||
} | ||
|
||
impl BandStaticMapper { | ||
pub fn new(source: String) -> Self { | ||
// Read the JSON file content | ||
let mut file = File::open(format!("resources/{}.json", source)).unwrap(); | ||
// Constructor to create a new BandStaticMapper from a pre-existing mapping. | ||
pub fn new(mapping: HashMap<String, Value>) -> Self { | ||
Self { mapping } | ||
} | ||
|
||
// Constructor to create a BandStaticMapper from a source file. | ||
pub fn from_source(source: &str) -> Result<Self, Error> { | ||
let path = format!("resources/{}.json", source.to_lowercase()); | ||
Self::from_path(path) | ||
} | ||
|
||
// Constructor to create a BandStaticMapper from a file path. | ||
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, Error> { | ||
// Attempt to open the file at the specified path. | ||
let mut file = File::open(&path)?; | ||
|
||
// Read the file content into a String. | ||
let mut content = String::new(); | ||
file.read_to_string(&mut content).unwrap(); | ||
file.read_to_string(&mut content)?; | ||
|
||
// Deserialize the JSON content into a Vec<Mapping> | ||
let mapping = serde_json::from_str(&content).unwrap(); | ||
// Deserialize the JSON content into a HashMap<String, Value>. | ||
let mapping = serde_json::from_str(&content)?; | ||
|
||
Self { mapping } | ||
Ok(Self { mapping }) | ||
} | ||
} | ||
|
||
// Implementing the Mapper trait for BandStaticMapper. | ||
impl Mapper for BandStaticMapper { | ||
fn get_mapping(&self) -> &HashMap<String, Value> { | ||
&self.mapping | ||
// Retrieve the mapping as a reference, wrapped in a Result. | ||
fn get_mapping(&self) -> Result<&HashMap<String, Value>, Error> { | ||
Ok(&self.mapping) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use crate::error::Error; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
|
||
pub trait Mapper { | ||
fn get_mapping(&self) -> &HashMap<String, Value>; | ||
fn get_mapping(&self) -> Result<&HashMap<String, Value>, Error>; | ||
} |