use std::fmt::{self, Write};
use crate::{Block, Doc, Formatter, MethodParam, Type, Visibility};
#[derive(Debug, Clone)]
pub struct Method {
name: String,
visibility: Visibility,
doc: Option<Doc>,
params: Vec<MethodParam>,
ret: Type,
is_static: bool,
is_inline: bool,
is_virtual: bool,
is_pure: bool,
is_override: bool,
is_const: bool,
is_inside: bool,
body: Block,
}
impl Method {
pub fn new(name: &str, ret: Type) -> Self {
Self::with_string(String::from(name), ret)
}
pub fn with_string(name: String, ret: Type) -> Self {
Self {
name,
doc: None,
visibility: Visibility::Private,
params: Vec::new(),
ret,
is_static: false,
is_inline: false,
is_virtual: false,
is_pure: false,
is_override: false,
is_const: false,
is_inside: false,
body: Block::new(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn to_type(&self) -> Type {
panic!("needs to implement a corresponding type.")
}
pub fn doc_str(&mut self, doc: &str) -> &mut Self {
if let Some(d) = &mut self.doc {
d.add_text(doc);
} else {
self.doc = Some(Doc::with_str(doc));
}
self
}
pub fn add_doc(&mut self, doc: Doc) -> &mut Self {
self.doc = Some(doc);
self
}
pub fn set_visibility(&mut self, vis: Visibility) -> &mut Self {
self.visibility = vis;
self
}
pub fn is_public(&self) -> bool {
self.visibility == Visibility::Public
}
pub fn is_protected(&self) -> bool {
self.visibility == Visibility::Protected
}
pub fn is_private(&self) -> bool {
self.visibility == Visibility::Private || self.visibility == Visibility::Default
}
pub fn set_public(&mut self) -> &mut Self {
self.set_visibility(Visibility::Public)
}
pub fn set_protected(&mut self) -> &mut Self {
self.set_visibility(Visibility::Protected)
}
pub fn set_private(&mut self) -> &mut Self {
self.set_visibility(Visibility::Private)
}
pub fn push_param(&mut self, arg: MethodParam) -> &mut Self {
self.params.push(arg);
self
}
pub fn new_param(&mut self, name: &str, ty: Type) -> &mut MethodParam {
self.push_param(MethodParam::new(name, ty));
self.params.last_mut().unwrap()
}
pub fn param_by_name(&self, name: &str) -> Option<&MethodParam> {
self.params.iter().find(|f| f.name() == name)
}
pub fn param_by_name_mut(&mut self, name: &str) -> Option<&mut MethodParam> {
self.params.iter_mut().find(|f| f.name() == name)
}
pub fn param_by_idx(&self, idx: usize) -> Option<&MethodParam> {
self.params.get(idx)
}
pub fn param_by_idx_mut(&mut self, idx: usize) -> Option<&mut MethodParam> {
self.params.get_mut(idx)
}
pub fn toggle_override(&mut self, val: bool) -> &mut Self {
self.is_override = val;
self
}
pub fn set_override(&mut self) -> &mut Self {
self.toggle_override(true)
}
pub fn toggle_const(&mut self, val: bool) -> &mut Self {
self.is_const = val;
self
}
pub fn set_const(&mut self) -> &mut Self {
self.toggle_const(true)
}
pub fn toggle_virtual(&mut self, val: bool) -> &mut Self {
if !val {
self.is_pure = false;
}
self.is_virtual = val;
self
}
pub fn set_virtual(&mut self) -> &mut Self {
self.toggle_virtual(true)
}
pub fn toggle_pure(&mut self, val: bool) -> &mut Self {
if val {
self.body.clear();
self.is_virtual = true
}
self.is_pure = val;
self
}
pub fn set_pure(&mut self) -> &mut Self {
self.toggle_pure(true)
}
pub fn toggle_static(&mut self, val: bool) -> &mut Self {
self.is_static = val;
self
}
pub fn set_static(&mut self) -> &mut Self {
self.toggle_static(true)
}
pub fn toggle_inline(&mut self, val: bool) -> &mut Self {
self.is_inline = val;
self
}
pub fn set_inline(&mut self) -> &mut Self {
self.toggle_inline(true)
}
pub fn toggle_inside_def(&mut self, val: bool) -> &mut Self {
self.is_inside = val;
self
}
pub fn set_inside_def(&mut self) -> &mut Self {
self.toggle_inside_def(true)
}
pub fn set_body(&mut self, body: Block) -> &mut Self {
if !body.is_empty() {
self.is_pure = false;
}
self.body = body;
self
}
pub fn body(&mut self) -> &mut Block {
&mut self.body
}
pub fn do_fmt(&self, fmt: &mut Formatter<'_>, decl_only: bool) -> fmt::Result {
if !self.body.is_empty() | self.doc.is_some() {
writeln!(fmt)?;
}
if let Some(ref docs) = self.doc {
docs.fmt(fmt)?;
}
if self.is_static && decl_only {
write!(fmt, "static ")?;
}
if self.is_inline {
write!(fmt, "inline ")?;
}
if self.is_virtual && decl_only {
write!(fmt, "virtual ")?;
}
self.ret.fmt(fmt)?;
if decl_only {
write!(fmt, " {}", self.name)?;
} else {
fmt.write_scoped_name(self.name.as_str())?;
}
if self.params.is_empty() {
write!(fmt, "(void)")?;
} else {
write!(fmt, "(")?;
for (i, arg) in self.params.iter().enumerate() {
if i != 0 {
write!(fmt, ", ")?;
}
arg.fmt(fmt)?;
}
write!(fmt, ")")?;
}
if self.is_const && decl_only {
write!(fmt, " const")?;
}
if self.is_override && decl_only {
write!(fmt, " override")?;
}
if self.body.is_empty() && self.is_pure && decl_only {
return write!(fmt, " = 0;");
}
if self.body.is_empty() || (decl_only && !(self.is_inside || self.is_inline)) {
return writeln!(fmt, ";");
}
fmt.block(|f| self.body.fmt(f))?;
writeln!(fmt)
}
pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.do_fmt(fmt, false)
}
pub fn fmt_decl(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
self.do_fmt(fmt, true)
}
pub fn fmt_def(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
if self.is_inline || self.is_inside {
return Ok(());
}
self.do_fmt(fmt, false)
}
}