| 1 |
use std::fmt; |
| 2 |
|
| 3 |
use crate::{Ident, Render}; |
| 4 |
|
| 5 |
/// Enable the union of a world with another world |
| 6 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 7 |
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 8 |
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))] |
| 9 |
pub struct Use { |
| 10 |
target: Ident, |
| 11 |
use_names_list: Vec<(Ident, Option<Ident>)>, |
| 12 |
} |
| 13 |
|
| 14 |
impl Use { |
| 15 |
pub fn new(use_target: impl Into<Ident>) -> Self { |
| 16 |
Self { |
| 17 |
target: use_target.into(), |
| 18 |
use_names_list: vec![], |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
pub fn target(&self) -> &Ident { |
| 23 |
&self.target |
| 24 |
} |
| 25 |
|
| 26 |
pub fn set_target(&mut self, target: Ident) { |
| 27 |
self.target = target; |
| 28 |
} |
| 29 |
|
| 30 |
// `alias` is a concrete type because of https://github.com/rust-lang/rust/issues/36887 |
| 31 |
pub fn item(&mut self, id: impl Into<Ident>, alias: Option<Ident>) { |
| 32 |
self.use_names_list |
| 33 |
.push((id.into(), alias.map(|s| s.into()))); |
| 34 |
} |
| 35 |
|
| 36 |
pub fn use_names_list(&self) -> &[(Ident, Option<Ident>)] { |
| 37 |
&self.use_names_list |
| 38 |
} |
| 39 |
|
| 40 |
pub fn use_names_list_mut(&mut self) -> &mut Vec<(Ident, Option<Ident>)> { |
| 41 |
&mut self.use_names_list |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl Render for Use { |
| 46 |
fn render(&self, f: &mut fmt::Formatter<'_>, opts: &crate::RenderOpts) -> fmt::Result { |
| 47 |
let len = self.use_names_list.len(); |
| 48 |
|
| 49 |
write!(f, "{}use {}.{{ ", opts.spaces(), self.target)?; |
| 50 |
for (i, (id, alias)) in self.use_names_list.iter().enumerate() { |
| 51 |
if let Some(alias) = alias { |
| 52 |
write!(f, "{id} as {alias}")?; |
| 53 |
} else { |
| 54 |
write!(f, "{id}")?; |
| 55 |
} |
| 56 |
if i < len - 1 { |
| 57 |
write!(f, ", ")?; |
| 58 |
} |
| 59 |
} |
| 60 |
write!(f, " }};\n")?; |
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
} |