dstv/lib.rs
1mod bend;
2mod border;
3mod cut;
4mod dstv;
5mod dstv_element;
6mod dstv_element_type;
7mod header;
8mod hole;
9mod numeration;
10mod part_face;
11mod slot;
12
13use std::str::FromStr;
14
15use prelude::ParseDstvError;
16
17/// Re-export all the modules
18pub mod prelude {
19 pub use crate::bend::*;
20 pub use crate::border::*;
21 pub use crate::cut::*;
22 pub use crate::dstv::*;
23 pub use crate::dstv_element::*;
24 pub use crate::dstv_element_type::*;
25 pub use crate::header::*;
26 pub use crate::hole::*;
27 pub use crate::numeration::*;
28 pub use crate::part_face::*;
29 pub use crate::slot::*;
30}
31
32/// Validate if flange is either u v o or h
33/// other values are not allowed
34/// # arguments
35/// * `flange` - flange code
36/// # return
37/// * `bool` - true if flange is valid
38/// # example
39/// ```
40/// use dstv::validate_flange;
41/// assert_eq!(validate_flange("u"), true);
42/// assert_eq!(validate_flange("v"), true);
43/// assert_eq!(validate_flange("o"), true);
44/// assert_eq!(validate_flange("h"), true);
45/// assert_eq!(validate_flange("x"), false);
46/// ```
47pub fn validate_flange(flange: &str) -> bool {
48 part_face::PartFace::from_str(flange).is_ok()
49}
50
51/// Get f64 from string and strips it of any non numeric characters
52/// # arguments
53/// * `line` - line to parse
54/// * `name` - name of the element
55/// # return
56/// * `f64` - parsed f64
57/// # example
58/// ```
59/// use dstv::get_f64_from_str;
60/// assert_eq!(get_f64_from_str(Some("1.0s"), "test"), Ok(1.0));
61/// assert_eq!(get_f64_from_str(Some("1.0u"), "test"), Ok(1.0));
62/// assert_eq!(get_f64_from_str(Some("1.0o"), "test"), Ok(1.0));
63/// assert_eq!(get_f64_from_str(Some("1.0"), "test"), Ok(1.0));
64/// assert_eq!(get_f64_from_str(None, "test"), Ok(0.0));
65/// ```
66pub fn get_f64_from_str(line: Option<&str>, name: &str) -> Result<f64, ParseDstvError> {
67 match line {
68 Some(x) => x
69 .replace("s", "")
70 .replace("w", "")
71 .replace("l", "")
72 .replace("u", "")
73 .replace("o", "")
74 .parse::<f64>()
75 .map_err(|_| ParseDstvError::new(format!("`{name}` not a f64: got `{x}`"))),
76 None => Ok(0.0),
77 }
78}