Skip to main content

timsrust_tdf/
timstof.rs

1use timsrust_core::io::Uri;
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4pub struct TDFPath {
5    uri: Uri,
6    tdf: Uri,
7    tdf_bin: Uri,
8}
9
10impl TDFPath {
11    pub fn new(path: impl AsRef<str>) -> Result<Self, TDFPathError> {
12        let uri = Uri::from(path.as_ref());
13        let tdf = uri.join("analysis.tdf");
14        let tdf_bin = uri.join("analysis.tdf_bin");
15        if tdf.probe_is_file() && tdf_bin.probe_is_file() {
16            return Ok(Self { uri, tdf, tdf_bin });
17        }
18        match uri.parent() {
19            Some(parent) => Self::new(parent.as_ref())
20                .map_err(|_| TDFPathError::UnknownType(uri.to_string())),
21            None => Err(TDFPathError::UnknownType(uri.to_string())),
22        }
23    }
24
25    pub fn tdf(&self) -> &Uri {
26        &self.tdf
27    }
28
29    pub fn tdf_bin(&self) -> &Uri {
30        &self.tdf_bin
31    }
32
33    pub fn uri(&self) -> &Uri {
34        &self.uri
35    }
36}
37
38impl AsRef<str> for TDFPath {
39    fn as_ref(&self) -> &str {
40        self.uri.as_ref()
41    }
42}
43
44pub trait TDFPathLike: AsRef<str> {
45    fn to_timstof_path(&self) -> Result<TDFPath, TDFPathError>;
46}
47
48impl<T: AsRef<str>> TDFPathLike for T {
49    fn to_timstof_path(&self) -> Result<TDFPath, TDFPathError> {
50        TDFPath::new(self)
51    }
52}
53
54#[derive(Debug, thiserror::Error)]
55pub enum TDFPathError {
56    #[error("No valid type found for {0}")]
57    UnknownType(String),
58}