Pin.cpp (3379B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "winget/Pin.h" 5 6 using namespace AppInstaller::Utility; 7 8 namespace AppInstaller::Pinning 9 { 10 using namespace std::string_view_literals; 11 12 namespace 13 { 14 // Source ID to use for the installed source; it does not match any installed source. 15 // This does match with the actual ID of the source, but it doesn't really matter 16 // as it is handled specially when we see it. 17 constexpr std::string_view s_installedSourceId = "*PredefinedInstalledSource"sv; 18 } 19 20 PinType ConvertToPinTypeEnum(std::string_view in) 21 { 22 if (Utility::CaseInsensitiveEquals(in, "Blocking"sv)) 23 { 24 return PinType::Blocking; 25 } 26 else if (Utility::CaseInsensitiveEquals(in, "Pinning"sv)) 27 { 28 return PinType::Pinning; 29 } 30 else if (Utility::CaseInsensitiveEquals(in, "Gating"sv)) 31 { 32 return PinType::Gating; 33 } 34 else if (Utility::CaseInsensitiveEquals(in, "PinnedByManifest"sv)) 35 { 36 return PinType::PinnedByManifest; 37 } 38 else 39 { 40 return PinType::Unknown; 41 } 42 } 43 44 std::string_view ToString(PinType type) 45 { 46 switch (type) 47 { 48 case PinType::Blocking: 49 return "Blocking"sv; 50 case PinType::Pinning: 51 return "Pinning"sv; 52 case PinType::Gating: 53 return "Gating"sv; 54 case PinType::PinnedByManifest: 55 return "PinnedByManifest"sv; 56 case PinType::Unknown: 57 default: 58 return "Unknown"; 59 } 60 } 61 62 bool IsStricter(PinType first, PinType second) 63 { 64 return first > second; 65 } 66 67 PinType Stricter(PinType first, PinType second) 68 { 69 return IsStricter(first, second) ? first : second; 70 } 71 72 std::string PinKey::ToString() const 73 { 74 std::stringstream ss; 75 ss << "Package=[" << PackageId << "] Source=[" << SourceId << "]"; 76 return ss.str(); 77 } 78 79 PinKey PinKey::GetPinKeyForInstalled(std::string_view systemReferenceString) 80 { 81 return { systemReferenceString, s_installedSourceId }; 82 } 83 84 bool PinKey::IsForInstalled() const 85 { 86 return SourceId == s_installedSourceId; 87 } 88 89 std::string Pin::ToString() const 90 { 91 std::stringstream ss; 92 ss << m_key.ToString() << " Type=[" << Pinning::ToString(m_type) << ']'; 93 94 if (m_type == PinType::Gating) 95 { 96 ss << " GatedVersion=[" << m_gatedVersion.ToString() << ']'; 97 } 98 99 return ss.str(); 100 } 101 102 Pin Pin::CreateBlockingPin(PinKey&& pinKey) 103 { 104 return { PinType::Blocking, std::move(pinKey) }; 105 } 106 107 Pin Pin::CreatePinningPin(PinKey&& pinKey) 108 { 109 return { PinType::Pinning, std::move(pinKey) }; 110 } 111 112 Pin Pin::CreateGatingPin(PinKey&& pinKey, GatedVersion&& gatedVersion) 113 { 114 return { PinType::Gating, std::move(pinKey), std::move(gatedVersion) }; 115 } 116 117 bool Pin::operator==(const Pin& other) const 118 { 119 return m_type == other.m_type && 120 m_key == other.m_key && 121 m_gatedVersion == other.m_gatedVersion; 122 } 123 }