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.is_file().unwrap_or(false) && tdf_bin.is_file().unwrap_or(false)
16        {
17            return Ok(Self { uri, tdf, tdf_bin });
18        }
19        match uri.parent() {
20            Some(parent) => Self::new(parent.as_ref())
21                .map_err(|_| TDFPathError::UnknownType(uri.to_string())),
22            None => Err(TDFPathError::UnknownType(uri.to_string())),
23        }
24    }
25
26    pub fn tdf(&self) -> &Uri {
27        &self.tdf
28    }
29
30    pub fn tdf_bin(&self) -> &Uri {
31        &self.tdf_bin
32    }
33
34    pub fn uri(&self) -> &Uri {
35        &self.uri
36    }
37}
38
39impl AsRef<str> for TDFPath {
40    fn as_ref(&self) -> &str {
41        self.uri.as_ref()
42    }
43}
44
45pub trait TDFPathLike: AsRef<str> {
46    fn to_timstof_path(&self) -> Result<TDFPath, TDFPathError>;
47}
48
49impl<T: AsRef<str>> TDFPathLike for T {
50    fn to_timstof_path(&self) -> Result<TDFPath, TDFPathError> {
51        TDFPath::new(self)
52    }
53}
54
55#[derive(Debug, thiserror::Error)]
56pub enum TDFPathError {
57    #[error("No valid type found for {0}")]
58    UnknownType(String),
59}