diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 518b5f07d699dd1f3800407424add0738a7c0751..c7c4ba07d617734fe1aaab99fcbe75c5634f550b 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -46,11 +46,6 @@ pub enum ComponentCreationError { InSync, } -impl std::fmt::Debug for ProtocolDescription { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "(An opaque protocol description)") - } -} impl ProtocolDescription { pub fn parse(buffer: &[u8]) -> Result { let source = InputSource::new(String::new(), Vec::from(buffer)); @@ -217,3 +212,41 @@ pub trait RunContext { fn performed_fork(&mut self) -> Option; // None if not yet forked fn created_channel(&mut self) -> Option<(Value, Value)>; // None if not yet prepared } + +pub struct ProtocolDescriptionBuilder { + parser: Parser, +} + +impl ProtocolDescriptionBuilder { + pub fn new() -> Self { + return Self{ + parser: Parser::new(), + } + } + + pub fn add(&mut self, filename: String, buffer: Vec) -> Result<(), ParseError> { + let input = InputSource::new(filename, buffer); + self.parser.feed(input)?; + + return Ok(()) + } + + pub fn compile(mut self) -> Result { + self.parser.parse()?; + + let modules: Vec = self.parser.modules.into_iter() + .map(|module| Module{ + source: module.source, + root_id: module.root_id, + name: module.name.map(|(_, name)| name) + }) + .collect(); + + return Ok(ProtocolDescription { + modules, + heap: self.parser.heap, + types: self.parser.type_table, + pool: Mutex::new(self.parser.string_pool), + }); + } +}