orion-accessor 0.6.0

Unified accessor layer for HTTP, Git, and local resources with redirect rules, proxy control, and env-aware templating.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! 地址配置验证模块
//!
//! 提供地址配置验证功能,确保地址格式正确且可访问

use crate::addr::constants;
//use crate::prelude::*;
use std::path::Path;
use url::Url;

use super::{Address, GitRepository, HttpResource, LocalPath};

/// 地址验证结果
pub type ValidationResult = Result<(), Vec<ValidationError>>;

/// 验证错误类型
#[derive(Debug, Clone, PartialEq)]
pub struct ValidationError {
    /// 错误字段
    pub field: String,
    /// 错误描述
    pub message: String,
    /// 错误代码
    pub code: String,
}

impl ValidationError {
    pub fn new(field: &str, message: &str, code: &str) -> Self {
        Self {
            field: field.to_string(),
            message: message.to_string(),
            code: code.to_string(),
        }
    }
}

/// 地址验证trait
pub trait Validate {
    /// 验证地址配置
    fn validate(&self) -> ValidationResult;

    /// 验证地址是否可访问
    fn is_accessible(&self) -> bool;
}

impl Validate for Address {
    fn validate(&self) -> ValidationResult {
        match self {
            Address::Git(repo) => repo.validate(),
            Address::Http(resource) => resource.validate(),
            Address::Local(path) => path.validate(),
        }
    }

    fn is_accessible(&self) -> bool {
        match self {
            Address::Git(repo) => repo.is_accessible(),
            Address::Http(resource) => resource.is_accessible(),
            Address::Local(path) => path.is_accessible(),
        }
    }
}

impl Validate for GitRepository {
    fn validate(&self) -> ValidationResult {
        let mut errors = Vec::new();

        // 验证仓库地址
        if self.repo().is_empty() {
            errors.push(ValidationError::new(
                "repo",
                "仓库地址不能为空",
                "EMPTY_REPO",
            ));
        } else if !is_valid_git_url(self.repo()) {
            errors.push(ValidationError::new(
                "repo",
                "无效的Git仓库地址格式",
                "INVALID_GIT_URL",
            ));
        }

        // 验证SSH密钥路径
        if let Some(ssh_key) = &self.ssh_key()
            && !Path::new(ssh_key).exists()
        {
            errors.push(ValidationError::new(
                "ssh_key",
                &format!("SSH密钥文件不存在: {ssh_key}",),
                "SSH_KEY_NOT_FOUND",
            ));
        }

        // 验证认证配置
        if self.token().is_some() && self.username().is_none() {
            errors.push(ValidationError::new(
                "username",
                "使用Token认证时必须提供用户名",
                "MISSING_USERNAME",
            ));
        }

        // 验证版本标识符
        let version_count = [
            self.tag().as_ref(),
            self.branch().as_ref(),
            self.rev().as_ref(),
        ]
        .iter()
        .filter(|x| x.is_some())
        .count();

        if version_count > 1 {
            errors.push(ValidationError::new(
                "version",
                "不能同时指定tag、branch和rev中的多个",
                "CONFLICTING_VERSIONS",
            ));
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    fn is_accessible(&self) -> bool {
        // 简化的可访问性检查
        // 实际实现可能需要网络连接测试
        is_valid_git_url(self.repo())
    }
}

impl Validate for HttpResource {
    fn validate(&self) -> ValidationResult {
        let mut errors = Vec::new();

        // 验证URL格式
        if self.url().is_empty() {
            errors.push(ValidationError::new("url", "URL不能为空", "EMPTY_URL"));
        } else if let Err(e) = Url::parse(self.url()) {
            errors.push(ValidationError::new(
                "url",
                &format!("无效的URL格式: {e}"),
                "INVALID_URL",
            ));
        }

        // 验证认证信息
        if self.username().is_some() && self.password().is_none() {
            errors.push(ValidationError::new(
                "password",
                "提供用户名时必须提供密码",
                "MISSING_PASSWORD",
            ));
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    fn is_accessible(&self) -> bool {
        // 简化的可访问性检查
        Url::parse(self.url()).is_ok()
    }
}

impl Validate for LocalPath {
    fn validate(&self) -> ValidationResult {
        let mut errors = Vec::new();

        // 验证路径格式
        let path_str = self.path();
        if path_str.is_empty() {
            errors.push(ValidationError::new(
                "path",
                "本地路径不能为空",
                "EMPTY_PATH",
            ));
        } else {
            let path = Path::new(path_str);

            // 检查路径是否包含非法字符
            if path_str.contains("\\") && cfg!(not(target_os = "windows")) {
                errors.push(ValidationError::new(
                    "path",
                    "在非Windows系统上使用了反斜杠路径分隔符",
                    "INVALID_PATH_SEPARATOR",
                ));
            }

            // 检查相对路径
            if path.is_relative() && !path_str.starts_with("./") && !path_str.starts_with("../") {
                errors.push(ValidationError::new(
                    "path",
                    "相对路径应以./或../开头",
                    "INVALID_RELATIVE_PATH",
                ));
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    fn is_accessible(&self) -> bool {
        Path::new(self.path()).exists()
    }
}

/// 验证Git URL格式
fn is_valid_git_url(url: &str) -> bool {
    // HTTPS格式
    if url.starts_with(constants::git::HTTPS_PREFIX) && url.ends_with(".git") {
        return Url::parse(url).is_ok();
    }

    // SSH格式 (git@host:repo.git)
    if url.starts_with(constants::git::SSH_PREFIX) && url.contains(':') && url.ends_with(".git") {
        return true;
    }

    // Git协议格式
    if url.starts_with(constants::git::GIT_PROTOCOL) && url.ends_with(".git") {
        return Url::parse(url).is_ok();
    }

    // 简化的GitHub/GitLab等格式
    url.contains("github.com") || url.contains("gitlab.com") || url.contains("gitea.com")
}

/// 批量验证多个地址
pub fn validate_addresses(addresses: &[Address]) -> ValidationResult {
    let mut all_errors = Vec::new();

    for (index, addr) in addresses.iter().enumerate() {
        if let Err(errors) = addr.validate() {
            for error in errors {
                all_errors.push(ValidationError::new(
                    &format!("address[{}].{}", index, error.field),
                    &error.message,
                    &error.code,
                ));
            }
        }
    }

    if all_errors.is_empty() {
        Ok(())
    } else {
        Err(all_errors)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_git_repository_validation() {
        let repo = GitRepository::from("https://github.com/user/repo.git");
        assert!(repo.validate().is_ok());

        let invalid_repo = GitRepository::from("");
        assert!(invalid_repo.validate().is_err());

        let ssh_repo = GitRepository::from("git@github.com:user/repo.git");
        assert!(ssh_repo.validate().is_ok());
    }

    #[test]
    fn test_http_resource_validation() {
        let resource = HttpResource::from("https://example.com/file.zip");
        assert!(resource.validate().is_ok());

        let invalid_resource = HttpResource::from("invalid-url");
        assert!(invalid_resource.validate().is_err());

        let empty_resource = HttpResource::from("");
        assert!(empty_resource.validate().is_err());
    }

    #[test]
    fn test_local_path_validation() {
        let path = LocalPath::from("./relative/path");
        assert!(path.validate().is_ok());

        let absolute_path = LocalPath::from("/absolute/path");
        assert!(absolute_path.validate().is_ok());

        let invalid_path = LocalPath::from("");
        assert!(invalid_path.validate().is_err());
    }

    #[test]
    fn test_address_validation() {
        let git_addr = Address::Git(GitRepository::from("https://github.com/user/repo.git"));
        assert!(git_addr.validate().is_ok());

        let http_addr = Address::Http(HttpResource::from("https://example.com/file.zip"));
        assert!(http_addr.validate().is_ok());

        let local_addr = Address::Local(LocalPath::from("./path"));
        assert!(local_addr.validate().is_ok());
    }

    #[test]
    fn test_batch_validation() {
        let addresses = vec![
            Address::Git(GitRepository::from("https://github.com/user/repo.git")),
            Address::Http(HttpResource::from("https://example.com/file.zip")),
        ];

        assert!(validate_addresses(&addresses).is_ok());

        let invalid_addresses = vec![
            Address::Git(GitRepository::from("")),
            Address::Http(HttpResource::from("invalid-url")),
        ];

        assert!(validate_addresses(&invalid_addresses).is_err());
    }

    #[test]
    fn test_is_valid_git_url() {
        assert!(is_valid_git_url("https://github.com/user/repo.git"));
        assert!(is_valid_git_url("git@github.com:user/repo.git"));
        assert!(is_valid_git_url("git://github.com/user/repo.git"));
        assert!(is_valid_git_url("https://gitlab.com/user/repo.git"));
        assert!(!is_valid_git_url("invalid-url"));
        assert!(!is_valid_git_url(""));
    }

    #[test]
    fn test_git_repository_authentication_and_version_conflicts() {
        // Test GitRepository with valid SSH key path (mocked test since we can't create files)
        let repo_with_ssh_key = GitRepository::from("https://github.com/user/repo.git")
            .with_ssh_key("/path/to/ssh/key");

        // SSH key validation will check if file exists - in test environment it will fail
        // but we can test that the validation logic runs
        let result = repo_with_ssh_key.validate();
        assert!(result.is_err() || result.is_ok()); // Either is acceptable in test

        // Test token authentication without username (should fail)
        let repo_with_token_only =
            GitRepository::from("https://github.com/user/repo.git").with_token("test-token");
        let result = repo_with_token_only.validate();
        assert!(result.is_err());
        if let Err(errors) = result {
            assert!(errors.iter().any(|e| e.code == "MISSING_USERNAME"));
        }

        // Test token authentication with username (should pass)
        let repo_with_token_and_username = GitRepository::from("https://github.com/user/repo.git")
            .with_token("test-token")
            .with_username("test-user");
        assert!(repo_with_token_and_username.validate().is_ok());

        // Test version identifier conflicts (tag and branch together)
        let repo_with_conflicting_versions =
            GitRepository::from("https://github.com/user/repo.git")
                .with_tag("v1.0")
                .with_branch("main");
        let result = repo_with_conflicting_versions.validate();
        assert!(result.is_err());
        if let Err(errors) = result {
            assert!(errors.iter().any(|e| e.code == "CONFLICTING_VERSIONS"));
        }

        // Test valid single version identifier
        let repo_with_valid_tag =
            GitRepository::from("https://github.com/user/repo.git").with_tag("v1.0");
        assert!(repo_with_valid_tag.validate().is_ok());

        let repo_with_valid_branch =
            GitRepository::from("https://github.com/user/repo.git").with_branch("main");
        assert!(repo_with_valid_branch.validate().is_ok());

        let repo_with_valid_rev =
            GitRepository::from("https://github.com/user/repo.git").with_rev("abc123");
        assert!(repo_with_valid_rev.validate().is_ok());
    }

    #[test]
    fn test_http_resource_authentication_edge_cases() {
        // Test HTTP resource with valid credentials
        let valid_http = HttpResource::from("https://example.com/file.zip")
            .with_credentials("username", "password");
        assert!(valid_http.validate().is_ok());

        // Test HTTP resource with username but no password
        let mut http_with_username_only = HttpResource::from("https://example.com/file.zip");
        // Set username without password using setter method
        http_with_username_only.set_username(Some("username".to_string()));
        let result = http_with_username_only.validate();
        assert!(result.is_err());
        if let Err(errors) = result {
            assert!(errors.iter().any(|e| e.code == "MISSING_PASSWORD"));
        }

        // Test various invalid URL formats (only those that fail to parse)
        let invalid_urls = vec!["://missing-protocol", "http//missing-colon.com"];

        for url in invalid_urls {
            let resource = HttpResource::from(url);
            let result = resource.validate();
            assert!(result.is_err(), "URL {url} should be invalid");
        }

        // Test URLs that parse as valid but aren't HTTP/HTTPS
        // Current validation only checks if URL can be parsed, not the scheme
        let valid_but_non_http_urls = vec!["ftp://unsupported-protocol.com"];

        for url in valid_but_non_http_urls {
            let resource = HttpResource::from(url);
            // These will pass validation because they parse as valid URLs
            // even though they're not HTTP/HTTPS
            assert!(
                resource.validate().is_ok(),
                "URL {url} should pass validation (parses as valid)",
            );
        }

        // Test URL parsing edge cases
        let edge_case_urls = vec![
            "https://example.com/",
            "https://example.com/path/",
            "https://example.com/path/to/file.txt",
            "https://example.com/path/to/file.txt?param=value",
            "https://example.com/path/to/file.txt#fragment",
        ];

        for url in edge_case_urls {
            let resource = HttpResource::from(url);
            assert!(resource.validate().is_ok(), "URL {url} should be valid");
        }
    }

    #[test]
    fn test_local_path_advanced_validation() {
        // Test Windows path separator on non-Windows systems
        let windows_style_path = LocalPath::from("C:\\windows\\path");
        if cfg!(not(target_os = "windows")) {
            let result = windows_style_path.validate();
            assert!(result.is_err());
            if let Err(errors) = result {
                assert!(errors.iter().any(|e| e.code == "INVALID_PATH_SEPARATOR"));
            }
        }

        // Test invalid relative paths (missing ./ or ../ prefix)
        let invalid_relative_path = LocalPath::from("relative/path");
        let result = invalid_relative_path.validate();
        assert!(result.is_err());
        if let Err(errors) = result {
            assert!(errors.iter().any(|e| e.code == "INVALID_RELATIVE_PATH"));
        }

        // Test valid relative paths with different prefixes
        let valid_relative_paths = vec![
            "./relative/path",
            "./relative/path/file.txt",
            "../relative/path",
            "../relative/path/file.txt",
            "./",
            "../",
        ];

        for path in valid_relative_paths {
            let local_path = LocalPath::from(path);
            assert!(local_path.validate().is_ok(), "Path {path} should be valid",);
        }

        // Test absolute paths on different systems
        let absolute_paths = if cfg!(target_os = "windows") {
            vec![
                "C:\\absolute\\path",
                "C:\\absolute\\path\\file.txt",
                "\\\\network\\path",
            ]
        } else {
            vec!["/absolute/path", "/absolute/path/file.txt", "/absolute"]
        };

        for path in absolute_paths {
            let local_path = LocalPath::from(path);
            assert!(local_path.validate().is_ok(), "Path {path} should be valid",);
        }

        // Test empty path
        let empty_path = LocalPath::from("");
        let result = empty_path.validate();
        assert!(result.is_err());
        if let Err(errors) = result {
            assert!(errors.iter().any(|e| e.code == "EMPTY_PATH"));
        }
    }
}