rust_bert/common/resources/
local.rs

1use crate::common::error::RustBertError;
2use crate::resources::{Resource, ResourceProvider};
3use std::path::PathBuf;
4
5/// # Local resource
6#[derive(PartialEq, Eq, Debug, Clone)]
7pub struct LocalResource {
8    /// Local path for the resource
9    pub local_path: PathBuf,
10}
11
12impl ResourceProvider for LocalResource {
13    /// Gets the path for a local resource.
14    ///
15    /// # Returns
16    ///
17    /// * `PathBuf` pointing to the resource file
18    ///
19    /// # Example
20    ///
21    /// ```no_run
22    /// use rust_bert::resources::{LocalResource, ResourceProvider};
23    /// use std::path::PathBuf;
24    /// let config_resource = LocalResource {
25    ///     local_path: PathBuf::from("path/to/config.json"),
26    /// };
27    /// let config_path = config_resource.get_local_path();
28    /// ```
29    fn get_local_path(&self) -> Result<PathBuf, RustBertError> {
30        Ok(self.local_path.clone())
31    }
32
33    /// Gets a wrapper around the path for a local resource.
34    ///
35    /// # Returns
36    ///
37    /// * `Resource` wrapping a `PathBuf` pointing to the resource file
38    ///
39    /// # Example
40    ///
41    /// ```no_run
42    /// use rust_bert::resources::{LocalResource, ResourceProvider};
43    /// use std::path::PathBuf;
44    /// let config_resource = LocalResource {
45    ///     local_path: PathBuf::from("path/to/config.json"),
46    /// };
47    /// let config_path = config_resource.get_resource();
48    /// ```
49    fn get_resource(&self) -> Result<Resource, RustBertError> {
50        Ok(Resource::PathBuf(self.local_path.clone()))
51    }
52}
53
54impl From<PathBuf> for LocalResource {
55    fn from(local_path: PathBuf) -> Self {
56        Self { local_path }
57    }
58}
59
60impl From<PathBuf> for Box<dyn ResourceProvider + Send> {
61    fn from(local_path: PathBuf) -> Self {
62        Box::new(LocalResource { local_path })
63    }
64}