| 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 Include { |
| 10 |
use_path: Ident, |
| 11 |
include_names_list: Vec<(String, String)>, |
| 12 |
} |
| 13 |
|
| 14 |
impl Include { |
| 15 |
pub fn new(use_path: impl Into<Ident>) -> Self { |
| 16 |
Self { |
| 17 |
use_path: use_path.into(), |
| 18 |
include_names_list: vec![], |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
pub fn use_path(&self) -> &Ident { |
| 23 |
&self.use_path |
| 24 |
} |
| 25 |
|
| 26 |
pub fn set_use_path(&mut self, use_path: impl Into<Ident>) { |
| 27 |
self.use_path = use_path.into(); |
| 28 |
} |
| 29 |
|
| 30 |
pub fn include_names_list(&self) -> &[(String, String)] { |
| 31 |
&self.include_names_list |
| 32 |
} |
| 33 |
|
| 34 |
pub fn include_names_list_mut(&mut self) -> &mut Vec<(String, String)> { |
| 35 |
&mut self.include_names_list |
| 36 |
} |
| 37 |
|
| 38 |
pub fn with(&mut self, id: &str, alias: &str) { |
| 39 |
self.include_names_list |
| 40 |
.push((id.to_string(), alias.to_string())); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl Render for Include { |
| 45 |
fn render(&self, f: &mut fmt::Formatter<'_>, opts: &crate::RenderOpts) -> fmt::Result { |
| 46 |
match self.include_names_list.len() { |
| 47 |
0 => write!(f, "{}include {};\n", opts.spaces(), self.use_path)?, |
| 48 |
len => { |
| 49 |
write!(f, "{}include {} with {{ ", opts.spaces(), self.use_path)?; |
| 50 |
for (i, (id, alias)) in self.include_names_list.iter().enumerate() { |
| 51 |
if i == len - 1 { |
| 52 |
write!(f, "{id} as {alias}")?; |
| 53 |
} else { |
| 54 |
write!(f, "{id} as {alias}, ")?; |
| 55 |
} |
| 56 |
} |
| 57 |
write!(f, " }};\n")?; |
| 58 |
} |
| 59 |
} |
| 60 |
Ok(()) |
| 61 |
} |
| 62 |
} |