genco/lang/swift.rs
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
//! Specialization for Swift code generation.
//!
//! # String Quoting in Swift
//!
//! Swift uses UTF-8 internally, string quoting is with the exception of escape
//! sequences a one-to-one translation.
//!
//! ```rust
//! use genco::prelude::*;
//!
//! # fn main() -> genco::fmt::Result {
//! let toks: swift::Tokens = quote!("start π 😊 \n \x7f ÿ $ end");
//! assert_eq!("\"start π 😊 \\n \\u{7f} ÿ $ end\"", toks.to_string()?);
//! # Ok(())
//! # }
use core::fmt::Write as _;
use alloc::collections::BTreeSet;
use crate::fmt;
use crate::tokens::ItemStr;
/// Tokens container specialization for Rust.
pub type Tokens = crate::Tokens<Swift>;
impl_lang! {
/// Swift token specialization.
pub Swift {
type Config = Config;
type Format = Format;
type Item = Import;
fn write_quoted(out: &mut fmt::Formatter<'_>, input: &str) -> fmt::Result {
// From: https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html
for c in input.chars() {
match c {
'\0' => out.write_str("\\0")?,
'\\' => out.write_str("\\\\")?,
'\t' => out.write_str("\\t")?,
'\n' => out.write_str("\\n")?,
'\r' => out.write_str("\\r")?,
'\'' => out.write_str("\\'")?,
'"' => out.write_str("\\\"")?,
c if !c.is_control() => out.write_char(c)?,
c => {
write!(out, "\\u{{{:x}}}", c as u32)?;
}
};
}
Ok(())
}
fn format_file(
tokens: &Tokens,
out: &mut fmt::Formatter<'_>,
config: &Self::Config,
) -> fmt::Result {
let mut imports = Tokens::new();
Self::imports(&mut imports, tokens);
let format = Format::default();
imports.format(out, config, &format)?;
tokens.format(out, config, &format)?;
Ok(())
}
}
Import {
fn format(&self, out: &mut fmt::Formatter<'_>, _: &Config, _: &Format) -> fmt::Result {
out.write_str(&self.name)
}
}
}
/// Format state for Swift code.
#[derive(Debug, Default)]
pub struct Format {}
/// Configuration for formatting Swift code.
#[derive(Debug, Default)]
pub struct Config {}
/// The import of a Swift type `import UIKit`.
///
/// Created through the [import()] function.
#[derive(Debug, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Import {
/// Module of the imported name.
module: ItemStr,
/// Name imported.
name: ItemStr,
}
impl Swift {
fn imports(out: &mut Tokens, tokens: &Tokens) {
use crate as genco;
use crate::quote_in;
let mut modules = BTreeSet::new();
for import in tokens.walk_imports() {
modules.insert(&import.module);
}
if !modules.is_empty() {
for module in modules {
quote_in! { *out => $['\r']import $module}
}
}
out.line();
}
}
/// The import of a Swift type `import UIKit`.
///
/// # Examples
///
/// ```
/// use genco::prelude::*;
///
/// let toks = quote!($(swift::import("Foo", "Debug")));
///
/// assert_eq!(
/// vec![
/// "import Foo",
/// "",
/// "Debug",
/// ],
/// toks.to_file_vec()?
/// );
/// # Ok::<_, genco::fmt::Error>(())
/// ```
pub fn import<M, N>(module: M, name: N) -> Import
where
M: Into<ItemStr>,
N: Into<ItemStr>,
{
Import {
module: module.into(),
name: name.into(),
}
}