Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/Subscription/SubscriptionRecord.swift
T
Qiu Yuzhou 856d6b4272 refactor(domain): 按主题重组 Domain/ 源码目录
Domain/ 根目录原有 62 个平铺源文件,无法从目录读出主题划分。

按「目录即主题」重组为 11 个目录(Rules/Custom 为嵌套子目录):Catalog /
Subscription / Rules / Runtime / Activation / Settings / SystemProxy /
Plugin / Credentials / LegacyImport / Diagnostics。纯移动:类型名、模块名
与 import 均未改动,Domain/ 根下不再有散落文件。目录口径取自
GLOSSARY.md 的章节划分(配置与订阅、代理规则、代理控制与网络、设置、
凭据与诊断),而非新造分类。

配套改动:

- project.yml:wrapper 与 helper 两个 target 按文件引用的 15 条显式路径
  随目录改名,并补注释固定这一约定——这 15 条指向不存在的文件时,
  xcodegen generate 以 "missing source directory" 直接失败(实测,不静默)。
- SkeletonTests 新增 DomainLayoutTests:断言 Domain/ 根下无平铺 .swift、
  树下只有 .swift。后者防的是与 App/ 同源的坑——Domain/ 整目录是 app 与
  Core 的 source path,任何非源码文件都会被 XcodeGen 收进 Resources 阶段
  并封进签名后的 bundle。
- README 工程结构说明同步;docs/design 与 docs/research 属历史文档不改。

验证:xcodegen generate 后 app 164 / Core 163 源文件数与整理前一致;Agent
恰好 9 个、Helper 恰好 6 个 Domain 文件(无缺失无多出);app 与 Core 各覆盖
全部 62 个。clean build 0 error;task test 1024 用例全通过(整理前 1022,
+2 为新增守卫);task format 未改写任何被移动文件(证明纯移动);task lint
与整理前同为 21 条内容型 warning;packaging-gate.sh ALL CHECKS PASSED。

布局守卫已做变异验证:临时投放松散 .swift 与非 .swift 文件时两个用例均
失败并点名违规文件,清理后复验为绿。
2026-10-05 20:13:35 +08:00

120 lines
4.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
/// 订阅源记录(spec #21 D4,issue #9/#35):持久不透明的订阅 UUID + 固定分组
/// 身份 + 订阅 URL 凭据引用 + 最近刷新状态。URL 是敏感信息,权威副本在凭据
/// 存储(D5),目录文档只持引用;编辑 URL 保留订阅与固定分组身份。
struct SubscriptionRecord: Codable, Equatable, Sendable, Identifiable {
/// 订阅源身份;删除后新建即新身份(GLOSSARY.md「Subscription」)。
let id: NodeID
/// 固定订阅分组的稳定身份(客户端所有,跨刷新与 URL 编辑不变)。
let groupID: NodeID
var urlRef: CredentialReference
var status: SubscriptionRefreshStatus
/// 最近成功提交的白名单资料;成功响应省略时清除,失败或取消保留。
var information: SubscriptionInformation?
/// 独立于最近尝试状态,旧文件缺失时不猜测。
var lastSucceededAt: Date?
}
/// 最近一次刷新结果。失败只保存白名单事实,不保存订阅 URL、秘密、凭据引用
/// 或任意错误 description(D5 脱敏)。
enum SubscriptionRefreshStatus: Codable, Equatable, Sendable {
case never
// swiftlint:disable:next identifier_name
case succeeded(at: Date)
// swiftlint:disable:next identifier_name
case failed(at: Date, failure: SubscriptionRefreshFailure)
private enum CodingKeys: String, CodingKey {
case never
case succeeded
case failed
}
private struct SucceededPayload: Codable {
// swiftlint:disable:next identifier_name
let at: Date
}
private struct FailedPayload: Codable {
// swiftlint:disable:next identifier_name
let at: Date
let failure: SubscriptionRefreshFailure?
let reason: String?
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if container.contains(.never) {
self = .never
return
}
if let payload = try container.decodeIfPresent(SucceededPayload.self, forKey: .succeeded) {
self = .succeeded(at: payload.at)
return
}
if let payload = try container.decodeIfPresent(FailedPayload.self, forKey: .failed) {
// v4 uses typed facts. v1-v3 may contain only a rendered reason; loading it
// preserves failed status while deliberately discarding the old detail.
self = .failed(at: payload.at, failure: payload.failure ?? .legacy)
return
}
throw DecodingError.dataCorruptedError(
forKey: .never, in: container, debugDescription: "unknown refresh status")
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .never:
try container.encode([String: String](), forKey: .never)
case .succeeded(let at): // swiftlint:disable:this identifier_name
try container.encode(SucceededPayload(at: at), forKey: .succeeded)
case .failed(let at, let failure): // swiftlint:disable:this identifier_name
try container.encode(FailedPayload(at: at, failure: failure, reason: nil), forKey: .failed)
}
}
}
/// 配置目录持久化文档的完整形态:结构树 + 订阅源记录。两者一次原子落盘,
/// 保证「订阅记录 ↔ 固定分组子树」跨文档一致。
struct CatalogDocument: Equatable, Sendable {
var catalog: ConfigurationCatalog
var subscriptions: [SubscriptionRecord]
init(
catalog: ConfigurationCatalog = ConfigurationCatalog(),
subscriptions: [SubscriptionRecord] = []
) {
self.catalog = catalog
self.subscriptions = subscriptions
}
}
/// 已解析为目录形态的订阅快照:身份已按订阅作用域限定、凭据引用已分配
/// (服务层在提交前把明文密码写入凭据存储并构造 `ServerFields`)。
/// `root` 是固定分组的内容来源;固定分组自身身份由应用入口的 `groupID`
/// 决定(客户端所有),`root.id` 不参与挂载。
struct CatalogSubscriptionSnapshot: Equatable, Sendable {
struct ServerLeaf: Equatable, Sendable {
let id: NodeID
var fields: ServerFields
}
/// 有序子引用:顺序即快照(远端权威)的用户可见顺序。
enum Child: Equatable, Sendable {
case group(Group)
case server(ServerLeaf)
}
struct Group: Equatable, Sendable {
let id: NodeID
let name: String
var children: [Child]
}
/// 固定分组显示名:远端根分组名,扩展缺失时为 URL host。
let name: String
let root: Group
}