Skip to content

Commit

Permalink
Merge pull request #237 from mahkoh/jorth/next
Browse files Browse the repository at this point in the history
Jorth/next
  • Loading branch information
mahkoh authored Jul 25, 2024
2 parents 086f3eb + 084fe50 commit 6fff9be
Show file tree
Hide file tree
Showing 55 changed files with 629 additions and 597 deletions.
10 changes: 5 additions & 5 deletions build/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
anyhow::{bail, Context, Result},
bstr::{BStr, BString, ByteSlice},
bstr::{BString, ByteSlice},
};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -54,14 +54,14 @@ pub struct Token<'a> {

#[derive(Debug, Eq, PartialEq)]
pub enum TokenKind<'a> {
Ident(&'a BStr),
Ident(&'a str),
Num(u32),
Tree {
delim: TreeDelim,
body: Vec<Token<'a>>,
},
Symbol(Symbol),
String(BString),
String(String),
}

impl TokenKind<'_> {
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<'a> Tokenizer<'a> {
{
c.pos += 1;
}
TokenKind::Ident(c.s[b_pos..c.pos].as_bstr())
TokenKind::Ident(c.s[b_pos..c.pos].as_bstr().to_str()?)
}
b'0'..=b'9' => {
c.pos -= 1;
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<'a> Tokenizer<'a> {
bail!("Unterminated string in line {}", self.line);
}
c.pos += 1;
TokenKind::String(res.into())
TokenKind::String(BString::from(res).to_string())
}
_ => bail!("Unexpected byte {:?} in line {}", b as char, self.line),
};
Expand Down
78 changes: 59 additions & 19 deletions build/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ impl<'a> Parser<'a> {
});
let safe_name = match name {
"move" => "move_",
"type" => "type_",
"drop" => "drop_",
"id" => "id_",
_ => name,
};
Ok(Lined {
Expand Down Expand Up @@ -717,18 +720,50 @@ fn write_message<W: Write>(f: &mut W, obj: &str, message: &Message) -> Result<()
Ok(())
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum RequestHandlerDirection {
Request,
Event,
}

fn write_request_handler<W: Write>(
f: &mut W,
camel_obj_name: &str,
messages: &ParseResult,
messages: &[Lined<Message>],
direction: RequestHandlerDirection,
) -> Result<()> {
let snake_direction;
let camel_direction;
let parent;
let parser;
let error;
let param;
writeln!(f)?;
match direction {
RequestHandlerDirection::Request => {
snake_direction = "request";
camel_direction = "Request";
parent = "crate::object::Object";
parser = "crate::client::Client";
error = "crate::client::ClientError";
param = "req";
}
RequestHandlerDirection::Event => {
snake_direction = "event";
camel_direction = "Event";
parent = "crate::wl_usr::usr_object::UsrObject";
parser = "crate::wl_usr::UsrCon";
error = "crate::wl_usr::UsrConError";
param = "ev";
writeln!(f, " #[allow(dead_code)]")?;
}
}
writeln!(
f,
" pub trait {camel_obj_name}RequestHandler: crate::object::Object + Sized {{"
" pub trait {camel_obj_name}{camel_direction}Handler: {parent} + Sized {{"
)?;
writeln!(f, " type Error: std::error::Error;")?;
for message in &messages.requests {
for message in messages {
let msg = &message.val;
let lt = match msg.has_reference_type {
true => "<'_>",
Expand All @@ -737,34 +772,31 @@ fn write_request_handler<W: Write>(
writeln!(f)?;
writeln!(
f,
" fn {}(&self, req: {}{lt}, _slf: &Rc<Self>) -> Result<(), Self::Error>;",
" fn {}(&self, {param}: {}{lt}, _slf: &Rc<Self>) -> Result<(), Self::Error>;",
msg.safe_name, msg.camel_name
)?;
}
writeln!(f)?;
writeln!(f, " #[inline(always)]")?;
writeln!(f, " fn handle_request_impl(")?;
writeln!(f, " fn handle_{snake_direction}_impl(")?;
writeln!(f, " self: Rc<Self>,")?;
writeln!(f, " client: &crate::client::Client,")?;
writeln!(f, " client: &{parser},")?;
writeln!(f, " req: u32,")?;
writeln!(
f,
" parser: crate::utils::buffd::MsgParser<'_, '_>,"
)?;
writeln!(f, " ) -> Result<(), crate::client::ClientError> {{")?;
if messages.requests.is_empty() {
writeln!(f, " ) -> Result<(), {error}> {{")?;
if messages.is_empty() {
writeln!(f, " #![allow(unused_variables)]")?;
writeln!(
f,
" Err(crate::client::ClientError::InvalidMethod)"
)?;
writeln!(f, " Err({error}::InvalidMethod)")?;
} else {
writeln!(f, " let method;")?;
writeln!(
f,
" let error: Box<dyn std::error::Error> = match req {{"
)?;
for message in &messages.requests {
for message in messages {
let msg = &message.val;
write!(f, " {} ", msg.id)?;
if let Some(since) = msg.attribs.since {
Expand Down Expand Up @@ -793,13 +825,10 @@ fn write_request_handler<W: Write>(
}
writeln!(
f,
" _ => return Err(crate::client::ClientError::InvalidMethod),"
" _ => return Err({error}::InvalidMethod),"
)?;
writeln!(f, " }};")?;
writeln!(
f,
" Err(crate::client::ClientError::MethodError {{"
)?;
writeln!(f, " Err({error}::MethodError {{")?;
writeln!(f, " interface: {camel_obj_name},")?;
writeln!(f, " method,")?;
writeln!(f, " error,")?;
Expand Down Expand Up @@ -832,7 +861,18 @@ fn write_file<W: Write>(f: &mut W, file: &DirEntry) -> Result<()> {
for message in messages.requests.iter().chain(messages.events.iter()) {
write_message(f, &camel_obj_name, &message.val)?;
}
write_request_handler(f, &camel_obj_name, &messages)?;
write_request_handler(
f,
&camel_obj_name,
&messages.requests,
RequestHandlerDirection::Request,
)?;
write_request_handler(
f,
&camel_obj_name,
&messages.events,
RequestHandlerDirection::Event,
)?;
writeln!(f, "}}")?;
Ok(())
}
Expand Down
18 changes: 9 additions & 9 deletions build/wire_dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ enum Type {

#[derive(Debug)]
struct Field {
name: BString,
name: String,
ty: Type,
}

#[derive(Debug)]
struct Function {
name: BString,
name: String,
in_fields: Vec<Field>,
out_fields: Vec<Field>,
}

#[derive(Debug)]
struct Property {
name: BString,
name: String,
ty: Type,
}

#[derive(Debug)]
struct Signal {
name: BString,
name: String,
fields: Vec<Field>,
}

Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a> Parser<'a> {
res.with_context(|| format!("While parsing field starting at line {}", line))
}

fn expect_ident(&mut self) -> Result<(u32, &'a BStr)> {
fn expect_ident(&mut self) -> Result<(u32, &'a str)> {
self.not_eof()?;
let token = &self.tokens[self.pos];
self.pos += 1;
Expand Down Expand Up @@ -468,7 +468,7 @@ fn write_type2<W: Write>(f: &mut W, lt: &str, ty: &Type) -> Result<()> {
fn write_message<W: Write>(
f: &mut W,
el: &Element,
msg_name: &BStr,
msg_name: &str,
name: &str,
indent: &str,
fields: &[Field],
Expand Down Expand Up @@ -614,7 +614,7 @@ fn write_signal<W: Write>(f: &mut W, element: &Element, sig: &Signal, indent: &s
write_message(
f,
element,
sig.name.as_bstr(),
&sig.name,
&name,
indent,
&sig.fields,
Expand All @@ -640,7 +640,7 @@ fn write_function<W: Write>(
write_message(
f,
element,
fun.name.as_bstr(),
&fun.name,
&in_name,
indent,
&fun.in_fields,
Expand All @@ -650,7 +650,7 @@ fn write_function<W: Write>(
write_message(
f,
element,
fun.name.as_bstr(),
&fun.name,
&out_name,
indent,
&fun.out_fields,
Expand Down
Loading

0 comments on commit 6fff9be

Please sign in to comment.