import SwiftUI

public extension View {
    /// Round specific corners of a view
    ///
    /// Example:
    /// ```swift
    /// Text("Hello")
    ///     .cornerRadius(8, corners: [UIRectCorner.topLeft, UIRectCorner.topRight])
    /// ```
    func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
        clipShape(RoundedCorner(radius: radius, corners: corners))
    }
}

struct RoundedCorner: Shape {
    var radius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners

    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}
