Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: embed data for address to prevent panic on missing files #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/Cargo.lock
/.idea
/.vscode
13 changes: 11 additions & 2 deletions src/mock/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::fs::File;
use std::io::BufReader;

pub const CN_ADDRESS_DATA: Lazy<Result<Address, Box<dyn Error>>> =
Lazy::new(|| Address::from_path("./src/data/cn_address.json", AddressType::Cn));
Lazy::new(|| Address::from_string(include_str!("../data/cn_address.json"), AddressType::Cn));
pub const EN_ADDRESS_DATA: Lazy<Result<Address, Box<dyn Error>>> =
Lazy::new(|| Address::from_path("./src/data/en_address.json", AddressType::En));
Lazy::new(|| Address::from_string(include_str!("../data/en_address.json"), AddressType::En));

#[derive(Debug, Default, Clone)]
pub struct MockZipFn;
Expand Down Expand Up @@ -53,6 +53,15 @@ pub struct Address {
}

impl Address {
fn from_string(content: &str, r#type: AddressType) -> Result<Self, Box<dyn Error>> {
let mut v = serde_json::Deserializer::from_str(content);
let address_items = AddressItems::deserialize(&mut v)?;
Ok(Self {
r#type,
address_items,
})
}

pub fn from_path(path: &str, r#type: AddressType) -> Result<Self, Box<dyn Error>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
Expand Down