pdb_bindgen_codegen/config/
policy.rs1use super::{ConfigError, SymbolServerErrorKind};
2use std::path::{Path, PathBuf};
3
4const MICROSOFT_SYMBOL_SERVER: &str = "https://msdl.microsoft.com/download/symbols";
5const DEFAULT_CACHE_RELATIVE_PATH: &str = "pdb-bindgen/cache";
6
7#[derive(Clone, Debug, Eq, PartialEq)]
9pub struct CachePolicy {
10 location: CacheLocation,
11}
12
13impl CachePolicy {
14 pub(super) const fn out_dir() -> Self {
15 Self {
16 location: CacheLocation::OutDir,
17 }
18 }
19
20 pub(super) fn set_directory(&mut self, path: PathBuf) {
21 self.location = CacheLocation::Directory(path);
22 }
23
24 pub(super) fn reset_out_dir(&mut self) {
25 self.location = CacheLocation::OutDir;
26 }
27
28 #[must_use]
30 pub const fn is_out_dir_relative(&self) -> bool {
31 matches!(&self.location, CacheLocation::OutDir)
32 }
33
34 #[must_use]
36 pub fn out_dir_relative_path(&self) -> Option<&Path> {
37 match &self.location {
38 CacheLocation::OutDir => Some(Path::new(DEFAULT_CACHE_RELATIVE_PATH)),
39 CacheLocation::Directory(_) => None,
40 }
41 }
42
43 #[must_use]
45 pub fn directory(&self) -> Option<&Path> {
46 match &self.location {
47 CacheLocation::OutDir => None,
48 CacheLocation::Directory(path) => Some(path),
49 }
50 }
51}
52
53#[derive(Clone, Debug, Eq, PartialEq)]
54enum CacheLocation {
55 OutDir,
56 Directory(PathBuf),
57}
58
59#[derive(Clone, Debug, Eq, PartialEq)]
61pub struct SymbolServer {
62 source: SymbolServerSource,
63}
64
65impl SymbolServer {
66 pub(super) const fn microsoft() -> Self {
67 Self {
68 source: SymbolServerSource::Microsoft,
69 }
70 }
71
72 pub(super) fn set_custom(&mut self, url: String) {
73 self.source = SymbolServerSource::Custom(url);
74 }
75
76 pub(super) fn reset_microsoft(&mut self) {
77 self.source = SymbolServerSource::Microsoft;
78 }
79
80 pub(super) fn disable(&mut self) {
81 self.source = SymbolServerSource::Disabled;
82 }
83
84 #[must_use]
86 pub fn url(&self) -> Option<&str> {
87 match &self.source {
88 SymbolServerSource::Microsoft => Some(MICROSOFT_SYMBOL_SERVER),
89 SymbolServerSource::Custom(url) => Some(url),
90 SymbolServerSource::Disabled => None,
91 }
92 }
93
94 #[must_use]
96 pub const fn is_microsoft(&self) -> bool {
97 matches!(&self.source, SymbolServerSource::Microsoft)
98 }
99
100 #[must_use]
102 pub const fn is_disabled(&self) -> bool {
103 matches!(&self.source, SymbolServerSource::Disabled)
104 }
105}
106
107#[derive(Clone, Debug, Eq, PartialEq)]
108enum SymbolServerSource {
109 Microsoft,
110 Custom(String),
111 Disabled,
112}
113
114pub(super) fn validate_symbol_server(url: &str) -> Result<(), ConfigError> {
115 let remainder = if let Some(remainder) = url.strip_prefix("https://") {
116 remainder
117 } else if let Some(remainder) = url.strip_prefix("http://") {
118 remainder
119 } else {
120 return Err(ConfigError::InvalidSymbolServer {
121 url: url.to_owned(),
122 reason: SymbolServerErrorKind::Scheme,
123 });
124 };
125
126 if remainder.is_empty() || remainder.starts_with('/') {
127 return Err(ConfigError::InvalidSymbolServer {
128 url: url.to_owned(),
129 reason: SymbolServerErrorKind::MissingAuthority,
130 });
131 }
132 if url
133 .chars()
134 .any(|character| character.is_ascii_whitespace() || character.is_control())
135 || url.contains(['?', '#'])
136 {
137 return Err(ConfigError::InvalidSymbolServer {
138 url: url.to_owned(),
139 reason: SymbolServerErrorKind::InvalidCharacter,
140 });
141 }
142 Ok(())
143}