1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Error};
use encoding::all::ISO_8859_1;
use encoding::{DecoderTrap, Encoding};
use glob::glob;
pub fn find_file_in_basedir<T: AsRef<str>>(dir: T, pat: &str) -> Result<Option<PathBuf>, Error> {
let dir = dir.as_ref();
if !PathBuf::from(dir).exists() {
bail!("No se ha localizado el directorio {}", dir);
}
let pattern = [dir, pat]
.iter()
.collect::<PathBuf>()
.to_string_lossy()
.into_owned();
let globiter = glob(&pattern)?;
match globiter.map(|r| r).next() {
Some(p) => Ok(Some(p?)),
None => Ok(None),
}
}
pub fn find_first_file(pattern: &str) -> Result<Option<PathBuf>, Error> {
let globiter = glob(pattern)?;
match globiter.map(|r| r).next() {
Some(p) => Ok(Some(p?)),
None => Ok(None),
}
}
pub fn read_latin1_file<T: AsRef<Path>>(path: T) -> Result<String, Error> {
let buf = {
let mut buf = Vec::new();
BufReader::new(File::open(path.as_ref())?)
.read_to_end(&mut buf)
.with_context(|| {
format!(
"No se ha podido leer el archivo {}",
path.as_ref().display()
)
})?;
buf
};
match ISO_8859_1.decode(&buf, DecoderTrap::Replace) {
Ok(utf8buf) => Ok(utf8buf),
_ => bail!(
"Error de codificación del archivo {}",
path.as_ref().display()
),
}
}
pub fn read_file<T: AsRef<Path>>(path: T) -> anyhow::Result<String> {
let mut buf = String::new();
BufReader::new(File::open(path.as_ref())?)
.read_to_string(&mut buf)
.with_context(|| {
format!(
"No se ha podido leer el archivo {}",
path.as_ref().display()
)
})?;
Ok(buf)
}
pub fn fround2(val: f32) -> f32 {
(val * 100.0).round() / 100.0
}
pub fn fround3(val: f32) -> f32 {
(val * 1000.0).round() / 1000.0
}
pub fn normalize(value: f32, start: f32, end: f32) -> f32 {
let width = end - start;
let offset = value - start;
(offset - (f32::floor(offset / width) * width)) + start
}
pub fn orientation_bdl_to_52016(azimuth: f32) -> f32 {
normalize(180.0 - azimuth, -180.0, 180.0)
}
pub fn angle_name(angle: f32) -> String {
let angle = normalize(angle, 0.0, 360.0);
let name = if angle < 18.0 {
"S"
} else if angle < 69.0 {
"SE"
} else if angle < 120.0 {
"E"
} else if angle < 157.5 {
"NE"
} else if angle < 202.5 {
"N"
}
else if angle < 240.0 {
"NW"
}
else if angle < 291.0 {
"W"
}
else if angle < 342.0 {
"SW"
}
else {
"S"
};
name.to_string()
}
pub fn uuid_from_obj(obj: &impl std::fmt::Debug) -> String {
let h = format!("{:x}", md5::compute(format!("{:?}", obj).as_bytes()));
format!(
"{}-{}-{}-{}-{}",
&h[0..8],
&h[8..12],
&h[12..16],
&h[16..20],
&h[20..32]
)
}