-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDocumentFormat.cs
More file actions
91 lines (80 loc) · 2.9 KB
/
DocumentFormat.cs
File metadata and controls
91 lines (80 loc) · 2.9 KB
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
/*
* _____ ______
* /_ / ____ ____ ____ _________ / __/ /_
* / / / __ \/ __ \/ __ \/ ___/ __ \/ /_/ __/
* / /__/ /_/ / / / / /_/ /\_ \/ /_/ / __/ /_
* /____/\____/_/ /_/\__ /____/\____/_/ \__/
* /____/
*
* Authors:
* 钟峰(Popeye Zhong) <zongsoft@qq.com>
*
* Copyright (C) 2020-2025 Zongsoft Studio <http://www.zongsoft.com>
*
* This file is part of Zongsoft.Web.OpenApi library.
*
* The Zongsoft.Web.OpenApi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License,
* or (at your option) any later version.
*
* The Zongsoft.Web.OpenApi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Zongsoft.Web.OpenApi library. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace Zongsoft.Web.OpenApi;
public sealed class DocumentFormat : IEquatable<DocumentFormat>
{
#region 静态字段
public static readonly DocumentFormat Json = new(nameof(Json), "application/json;charset=utf-8");
public static readonly DocumentFormat Yaml = new(nameof(Yaml), "text/plain+yaml;charset=utf-8");
#endregion
#region 私有构造
private DocumentFormat(string name, string type)
{
if(string.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
if(string.IsNullOrEmpty(type))
throw new ArgumentNullException(nameof(type));
this.Name = name.ToLowerInvariant();
this.Type = type;
}
#endregion
#region 公共属性
public string Name { get; }
public string Type { get; }
#endregion
#region 静态方法
public static bool TryParse(ReadOnlySpan<char> text, out DocumentFormat format)
{
if(text.Equals(Json.Name, StringComparison.OrdinalIgnoreCase))
{
format = Json;
return true;
}
if(text.Equals(Yaml.Name, StringComparison.OrdinalIgnoreCase) ||
text.Equals("yml", StringComparison.OrdinalIgnoreCase))
{
format = Yaml;
return true;
}
format = null;
return false;
}
#endregion
#region 符号重写
public static bool operator ==(DocumentFormat left, DocumentFormat right) => left is null ? right is null : left.Equals(right);
public static bool operator !=(DocumentFormat left, DocumentFormat right) => !(left == right);
#endregion
#region 重写方法
public bool Equals(DocumentFormat other) => other is not null && string.Equals(this.Name, other.Name);
public override bool Equals(object obj) => this.Equals(obj as DocumentFormat);
public override int GetHashCode() => this.Name.GetHashCode();
public override string ToString() => this.Name;
#endregion
}