mattyoho / tnetstring-rb

A Ruby implementation of tagged netstrings, a simple data interchange format better suited to low-level network communication than JSON.
http://tnetstrings.org/
MIT License
18 stars 4 forks source link

yard, multibyte encodings, and macruby #9

Open ttilley opened 13 years ago

ttilley commented 13 years ago

Hello!

I recently found myself in need of an ObjC tagged netstring library and since it's insanely pleasant to prototype in MacRuby and also a massive waste of time to start from scratch, I've been mercilessly bending tnetstring-rb to that goal. Much of what I'm doing is probably of no interest to you, but there are a few bits and pieces that might be.

The one that jumps out the most is UTF8 string support that's compatible with the python reference implementation (despite the spec page forbidding unicode, the reference code in python handles it just fine :TM:). First, a failing spec: https://github.com/ttilley/tnetstring-rb/commit/486a49d3a205a9240191d4ccab478a36174015b0

Then the (ZOMG MESSY) implementation: https://github.com/ttilley/tnetstring-rb/commit/5a75cab1b4f7287913b2cc989702b4326a18229a

I went with defining it that way so that implementation specific code is selected at load of the ruby file, not each time the method is dispatched. A better approach might be to have a core extension. :shrug:

Note that I converted the project from using rspec to bacon, which has a mostly compatible syntax. The reason for this is that mac_bacon provides additional apple flavored goodness when used to test MacRuby/ObjC code.

Another item that you might care about is converting inline docs from rdoc to yard: https://github.com/ttilley/tnetstring-rb/commit/567c84b4d8f2140a9b71b7082c888e1b04a7b009

Despite your distaste for adding methods like 'to_tnetstring', it's going to be a lot less painful to implement that way, at least partially, for MacRuby/ObjC. Each class that can be serialized responds to TNSData, which returns an NSData object that essentially encapsulates a bag of bytes. TNetstring.dump() takes this and converts it to an NSString. ANYWAYS, part one of that: https://github.com/ttilley/tnetstring-rb/blob/f99cce7925d7bfc40175c0cc0ad8e4389ec3d381/lib/tnetstring/macruby.rb

Thank you for writing such a well tested library. =]

mattyoho commented 13 years ago

Hi! This looks like some interesting stuff, particularly the encoding handling and yard conversion, and I want to look through and get back to you ASAP. Sorry for not replying sooner. I'm doing some international travel this week and have been busy prepping and whatnot. Hopefully I'll be able to look through your commits on the flight and respond in the next couple of days. I'm curious about your approach on the MacRuby-specific implementation and what advantages that brings on the MacRuby/ObjC side.

Thanks for your feedback!

ttilley commented 13 years ago

i actually did a non-forward push to delete the commit where i added the macruby bits. a bit of a hindsight thing. Not everything in C/ObjC is an object, so how do you encode/decode BOOL or NSUInteger? You need to encapsulate primitive types in an NSValue/NSNumber/etc in order to add them to a collection class like NSArray/NSDictionary, but it was enough to make me rethink my approach.

...due to the way git works the history isn't actually gone though, and won't get GC'd for (I think) about 3 months, unless GitHub has changed the defaults. So that link to that blob is still valid and will show what was there.

Enjoy your flight. I'm glad to give you some hopefully interesting reading. ;)

ttilley commented 13 years ago

...i think a fetch would also retrieve that object despite the branch pointer being rewound.