altium/prj/
parse.rs

1use std::borrow::ToOwned;
2
3use ini::Properties;
4
5use crate::{
6    common::UniqueId,
7    error::{AddContext, ErrorKind},
8    parse::FromUtf8,
9    Error,
10};
11
12/// Parse a string or default to an empty string
13pub fn parse_string(sec: &Properties, key: &str) -> String {
14    sec.get(key).map(ToOwned::to_owned).unwrap_or_default()
15}
16
17/// Parse an integer or default to 0
18pub fn parse_int(sec: &Properties, key: &str) -> i32 {
19    sec.get(key)
20        .and_then(|v| v.parse().ok())
21        .unwrap_or_default()
22}
23
24/// Parse a boolean value
25pub fn parse_bool(sec: &Properties, key: &str) -> bool {
26    sec.get(key).map(|v| v == "1").unwrap_or_default()
27}
28
29/// Extract a `UniqueId` from a buffer
30pub fn parse_unique_id(sec: &Properties, key: &str) -> Result<UniqueId, Error> {
31    sec.get(key)
32        .ok_or_else(|| ErrorKind::MissingSection(key.to_owned()))
33        .and_then(|v| UniqueId::from_utf8(v.as_bytes()))
34        .map_err(|e| e.context("parse_unique_id"))
35}