cargo_swift/
lib_type.rs

1use std::{fmt::Display, str::FromStr};
2
3use clap::ValueEnum;
4use thiserror::Error;
5
6#[derive(ValueEnum, Debug, PartialEq, Eq, Clone, Copy)]
7#[value()]
8pub enum LibType {
9    Static,
10    Dynamic,
11}
12
13impl LibType {
14    /// The identifier used in the crate-type field in Cargo.toml
15    pub fn identifier(&self) -> &str {
16        match self {
17            LibType::Static => "staticlib",
18            LibType::Dynamic => "cdylib",
19        }
20    }
21
22    pub fn file_extension(&self) -> &str {
23        match self {
24            LibType::Static => "a",
25            LibType::Dynamic => "dylib",
26        }
27    }
28}
29
30impl Display for LibType {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            Self::Static => write!(f, "static"),
34            Self::Dynamic => write!(f, "dynamic"),
35        }
36    }
37}
38
39#[derive(Debug, Error)]
40pub struct VariantError {
41    input: String,
42}
43
44impl Display for VariantError {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        f.write_fmt(format_args!(
47            "Unsupported variant for crate-type: {}",
48            &self.input
49        ))
50    }
51}
52
53impl FromStr for LibType {
54    type Err = VariantError;
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        match s {
58            "staticlib" => Ok(Self::Static),
59            "cdylib" => Ok(Self::Dynamic),
60            _ => Err(VariantError {
61                input: String::from(s),
62            }),
63        }
64    }
65}