adguard_flm/manager/models/configuration/mod.rs
1//! Configuration-related objects for [`crate::FilterListManager`]
2pub mod filter_list_type;
3pub mod locale;
4pub mod request_proxy_mode;
5
6pub use self::filter_list_type::FilterListType;
7pub use self::locale::Locale;
8pub use self::request_proxy_mode::RequestProxyMode;
9use std::cmp::max;
10
11/// Expires value shouldn't be less than this constant. In seconds
12const MINIMAL_EXPIRES_VALUE: i32 = 3600;
13
14/// Default https?-requests timeout in ms.
15const DEFAULT_REQUEST_TIMEOUT_MS: i32 = 60000;
16
17/// Default `! Expires` value for downloaded filter lists.
18/// Will be used, if value is not set in filter's metadata in seconds
19const DEFAULT_EXPIRES_VALUE_FOR_FILTERS: i32 = 86400;
20
21/// Configuration object
22pub struct Configuration {
23 /// Type of filter lists to manage
24 pub filter_list_type: FilterListType,
25 /// Absolute path for library working directory.
26 /// This will be used for database operating.
27 /// if value is [`None`] `cwd` will be used
28 pub working_directory: Option<String>,
29 /// [`Locale`] is the locale that needs to be used to extract localized names and descriptions.
30 /// Locale `en-GB` will be normalized to internal `en_GB` representation.
31 /// Default value: en
32 pub locale: Locale,
33 /// Default period for expires in seconds (unless specified in "Expires",
34 /// or its value is too small).
35 /// Default value: 86400
36 /// Values < 3600 will be clamped to 3600
37 pub default_filter_list_expires_period_sec: i32,
38 /// List of literal constants for filters conditional compilation
39 pub compiler_conditional_constants: Option<Vec<String>>,
40 /// URL of the index (filters.json) file
41 pub metadata_url: String,
42 /// URL of the locales (filters_i18n.json) file
43 pub metadata_locales_url: String,
44 /// Requests timeouts in milliseconds. Default value 60000
45 pub request_timeout_ms: i32,
46 /// Requests proxy mode
47 pub request_proxy_mode: RequestProxyMode,
48 /// “Uplifting” a database is a set of measures that brings the database up to date:
49 /// * Database creation
50 /// * Filling with schema
51 /// * Creation of service tables and entities
52 /// * Migrating between versions of a given library
53 ///
54 /// If you want to disable this option, you will need to manually call `flm.lift_up_database()`
55 /// when you update the library in your application.
56 pub auto_lift_up_database: bool,
57 /// Client app name
58 pub app_name: String,
59 /// Client app version
60 pub version: String,
61}
62
63/// Normalized locales delimiter
64pub(crate) const LOCALES_DELIMITER: &str = "_";
65
66impl Configuration {
67 /// Normalizing configuration before we can work with it
68 pub(crate) fn normalized(&mut self) {
69 self.locale = Configuration::normalize_locale_string(&self.locale);
70 }
71
72 /// Normalize locale string
73 pub(crate) fn normalize_locale_string(locale: &Locale) -> Locale {
74 locale.replace('-', LOCALES_DELIMITER)
75 }
76
77 /// We shouldn't propagate values less than 3600
78 ///
79 /// * `filter_expires` - `filter.expires` value
80 pub(crate) fn resolve_right_expires_value(&self, filter_expires: i32) -> i32 {
81 if filter_expires < MINIMAL_EXPIRES_VALUE {
82 max(
83 self.default_filter_list_expires_period_sec,
84 MINIMAL_EXPIRES_VALUE,
85 )
86 } else {
87 filter_expires
88 }
89 }
90}
91
92impl Default for Configuration {
93 fn default() -> Self {
94 Self {
95 filter_list_type: FilterListType::STANDARD,
96 working_directory: None,
97 locale: "en".to_string(),
98 default_filter_list_expires_period_sec: DEFAULT_EXPIRES_VALUE_FOR_FILTERS,
99 compiler_conditional_constants: None,
100 metadata_url: String::new(),
101 request_proxy_mode: RequestProxyMode::UseSystemProxy,
102 metadata_locales_url: String::new(),
103 request_timeout_ms: DEFAULT_REQUEST_TIMEOUT_MS,
104 auto_lift_up_database: true,
105 app_name: String::new(),
106 version: String::new(),
107 }
108 }
109}